14

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?

Community
  • 1
  • 1
dmon
  • 1,344
  • 1
  • 14
  • 29

2 Answers2

15

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.

Community
  • 1
  • 1
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
8

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Well, but anyway, why explicitly state "inline"? – Vlad Jul 16 '12 at 15:57
  • @Vlad: A bunch of other questions already cover that, e.g. http://stackoverflow.com/questions/3212571/usefulness-of-the-inline-feature and http://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method – Ben Voigt Jul 16 '12 at 15:58
  • @Vlad: Because stating a function as `inline` tells the linker that multiple definitions should be assumed to be the same function, rather than an error. – GManNickG Jul 16 '12 at 15:58
  • So do I understand correct: "inline" is needed only for the freestanding functions defined in header? (Freestanding = not a part of a class.) – Vlad Jul 16 '12 at 15:59
  • @Vlad: No. It's needed for all non-template functions defined in a header. (But see also implicit `inline` for class members defined inside the class body.) – Ben Voigt Jul 16 '12 at 16:00
  • @Ben: with implicit inline for class members inside the class body, it's actually more like "yes" :) – Vlad Jul 16 '12 at 16:17