Jump to content
  • Advertisement
Sign in to follow this  
Alundra

inline virtual is a nonsense ?

This topic is 1257 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement


(also, your topic title is "inline template", but your post is about virtual functions...)

Nice catch, I have modified.

Share this post


Link to post
Share on other sites

In some sense the "inline" keyword itself is a nonsense.

 

"inline" only hint the compiler something should be inlined, but does not guarantee the compiler will do so. The compiler could also inline something else without that keyword.

 

Today's compiler think they are better than you and might completely disregard the keyword. And most of the time they are right.

Share this post


Link to post
Share on other sites

And most of the time they are right.

 

Nope, most of the time (if looking at distinct cases) they are wrong. They're still extremely dumb converters (despite all their necessary and respectable complexity). You just won't notice when they're wrong because the easy issues they solve are too easy to get wrong and the hard issues they try to solve usually don't matter.

 

If you were to build a virtual machine, you'd be surprised about the extents of compiler stupidity. They manage to completely disregard the needs of the instruction cache (at least that's what the measurements show by varying wildly between almost identical pieces of code), they don't know when inlining benefits the code and happen to be extremely conservative about it (unless it's extremely obvious, like with empty or very small functions, such as vector operators). But even if force-inlining a function, oddly enough, it sometimes performs worse than a preprocessor macro.

Edited by snake5

Share this post


Link to post
Share on other sites
The only purpose of "inline" is to allow you to break the one-definition-rule in C++. (As such, "templates" have implicitly defined "inline" as they are also allowed to break the ODR)

Compilers do not inline (or not inline) based on the presence (or absence) of the "inline" keyword. In fact, modern compilers can even inline functions defined only in C++ files at link-time. You pretty much can never control the compiler's inlining and it generally will do a much better job then you at deciding when to inline (or not inline).

To more directly answer your question, conceptually virtual calls have to have their destination chosen at runtime, so you cannot inline the code (because that would be choosing at compile time). In actuality, compilers can de-virtualize and then potentially inline as other posters have stated.

Share this post


Link to post
Share on other sites

The only purpose of "inline" is to allow you to break the one-definition-rule in C++. (As such, "templates" have implicitly defined "inline" as they are also allowed to break the ODR)

Compilers do not inline (or not inline) based on the presence (or absence) of the "inline" keyword. In fact, modern compilers can even inline functions defined only in C++ files at link-time. You pretty much can never control the compiler's inlining and it generally will do a much better job then you at deciding when to inline (or not inline).

To more directly answer your question, conceptually virtual calls have to have their destination chosen at runtime, so you cannot inline the code (because that would be choosing at compile time). In actuality, compilers can de-virtualize and then potentially inline as other posters have stated.

 

Also modern compilers do a pretty smashing job at in-lining things for you. If there is a virtual call that the compiler notices will never change, it will probably see if it can benefit from an inline or not. Kinda like noticing that you have a situation that's similar to 2^x, It will go ahead and swap that to a bitshift by x for you.

 

It's not always a guarantee that it will be inline. And usually, you will have to trust the compiler's judgement on it. It's been doing it's job longer than we have tongue.png.

No seriously, sometimes the compiler knows best.

Edited by Tangletail

Share this post


Link to post
Share on other sites


In some sense the "inline" keyword itself is a nonsense.

 

It has a completely different and very sensible purpose. Try putting a simple function or operator in a header used in multiple CPP files without the 'inline'.

Share this post


Link to post
Share on other sites




It has a completely different and very sensible purpose. Try putting a simple function or operator in a header used in multiple CPP files without the 'inline'.

 

It is still a nonsense because we only use it for its' side effect and not the original purpose. It should be something like

 

allow_duplicated_definition_and_assume_they_are_all_same void foo(){};

 

Nothing is actually inlined, why call it "inline"? The name only trick people to think it will "optymize" things, waste half a second to type and 7 bytes of precious disk space.

 

Newer languages don't even need the side effect of inline. Today's compilers are perfectly capable of knowing two definitions means exactly the same thing or not. They also don't use copy-and-paste based include so nothing is duplicated just because every source file must know about some source file.

 

Although this is the burden of an old language. It kind of surprised me that they decided the original meaning of "auto" is too silly to live in C++ anymore, but I think "inline" was used too much, and we will be stuck with it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!