0

We know that calling inline function is replaced by its function body in the preprocess procedure before compiling. However, if the inline function is just declared in head file while defined in a cpp file, does the compiler know how to replace? Does the cimpiler only know the inline function's declaration but not know the definition right now?

Thanks.

coinsyx
  • 643
  • 2
  • 7
  • 13

2 Answers2

3

Inline functions are handled entirely by the compiler, not the preprocessor.

An inline function must be defined in every translation unit where it is odr-used (§3.2/3).

In other words, if there's a call to an inline function in a file, then the definition (not just a declaration) of that inline function must appear in the pre-processed version of that file.

No diagnostic is (currently) required for breaking this rule. The compiler could reject the code outright, or it could (for example) continue to compile and treat that function as a normal (not inline) function. You're pretty much at the mercy of the compiler though--you're clearly breaking the rules of the language, so the only real question is whether the compiler will really enforce the rules, or possibly let you get away with breaking them in this particular case.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • As far as I know, `inline` is more like a _hint_ to the compiler. It does not say - "inline this function whatever it takes", so it won't produce any errors, but instead, the function will be treated as a "normal" one. – Kiril Kirov Feb 13 '14 at 07:14
  • @KirilKirov: yes and no. You're right that `inline` doesn't force inline expansion of the function's code--but it does affect the one-definition rule, and those effects are not optional. So yes, it could be treated as a non-inline function, or the code could be rejected, or the compiler could just produce bad code. – Jerry Coffin Feb 13 '14 at 07:17
  • Absolutely true, I didn't think about the one-definition rule. +1 – Kiril Kirov Feb 13 '14 at 07:37
  • I think the second sentence is misleading, at least it's at odds with the following explanation. I believe the explanation is correct and a definition is needed everywhere. Now a definition is also a declaration so the rule in the second sentence is necessary but insufficient. – MSalters Feb 13 '14 at 10:25
  • @MSalters: Oops--thanks for pointing it out. The second sentence was just plain wrong. – Jerry Coffin Feb 13 '14 at 15:12
0

However, if the inline function is just declared in head file while defined in a cpp file, does the compiler know how to replace?

No, it doesn't. You need to include the inline function definition in the header file; if you do not, then you will get linker errors when you compile files other than the one that defines the function (but which include the header and try to call the function).

Also, note that inlining is done by the compiler and not the preprocessor.

harmic
  • 28,606
  • 5
  • 67
  • 91
  • The liner has nothing to do with inline functions, `inline` is important for the compiler, as it replaces the function calls with the function bodies. You can't get linker error for function, declared inline, but defined in a `cpp` file. Unless I'm terribly wrong. – Kiril Kirov Feb 13 '14 at 07:16
  • @KirilKirov what is the liner you refer to? Also refer to this http://stackoverflow.com/questions/9648671/why-i-cant-define-inline-member-function-in-another-file for an example of someone getting a linker error in that case. Also note that compiler does not have to inline the function (it may use normal calls if it wants) and that g++ at least seems to insert a normal function call if it does not have the definition (thus the linker error) – harmic Feb 13 '14 at 07:23
  • I meant "linker", not "liner", just a typo, sorry. Yes, I agree with you, but I was talking about a bit different situation - `inline` in the declaration and not the definition. But I haven't tried that soon, so it could produce the same error. – Kiril Kirov Feb 13 '14 at 07:39