Possible Duplicate:
When should I write the keyword 'inline' for a function/method?
So this is a question that has bugged me for a while and I can't get a definitive answer. My understanding is that a good compiler will generally realise when it is both safe and advantageous to in-line a function and, if optimisation is switched on, it will in-line all such functions weather they they are explicitly identified as in-line functions by the programmer or not. Also, a complier will recognise when it is not safe/sensible to in-line a function and will simply ignore the programmers request to in-line functions in such cases.
Thus, I would like to know what is the advantage of explicitly stating a function as in-line? As long as optimisation is switched on the compiler will in-line all the functions it deems sensible to in-line, and only those functions.
I have found some discussions around inline protecting against multiple definitions due to nested h files, but surely #ifdefine'ing the header source code is better practice and again renders the use of the key word inline void?
An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.
No, that does not matter at all. There are cases where it is appropriate to use inline in a . cpp file. E.g. applying optimizations to code that is entirely implementation specific.
Inline functions are commonly used when the function definitions are small, and the functions are called several times in a program. Using inline functions saves time to transfer the control of the program from the calling function to the definition of the called function.
Inline functions provide following advantages: 1) Function call overhead doesn't occur. 2) It also saves the overhead of push/pop variables on the stack when function is called. 3) It also saves overhead of a return call from a function.
You're spot on about the compiler optimizations. You're just wrong in your assumption of what inline
is. Despite the name inline
is not for optimization. inline
is primarily to "violate" the one definition rule with impunity. Basically, it tells the linker that many translation units can see that definition, so it should not barf on finding it on multiple translation units.
Some compilers may treat it as a hint to inline the function, but that's totally up to the compiler and perfectly valid to just ignore that hint.
Header guards only protect against multiple definitions on the same translation unit. They do not work across translation units.
Header guards don't protect against multiple definition errors.
Multiple definitions are encountered by the linker, and occur when the same definition is included into separate compilation units.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With