Yes, that’s right, but only partially. Perhaps it is correctly framed as follows:
"Inline functions must be defined in each block (but not necessarily before) in which they are called. "
C11 ++ Standard: §7.1.2.4
A built-in function must be defined in each translation unit in which it is used, and have exactly the same definition in each case. [Note: a call to a built-in function can be met before its definition appears in the translation block. -end note]
Why is this justification?
When you declare an inline function, you basically tell the compiler (if possible) to replace the code to call the function with the contents of the function wherever the function is called. The idea is that the body of the function is probably small, and the call to the function is more expensive than the body of the function itself.
To do this, the compiler must see the definition when compiling the code where the function is called. This is usually done by adding a function definition to the header using the inline specifier and then including the header file in each cpp file where the function should be called.
source share