J
James Kuyper
On 29-Aug-12 03:54, Jens Gustedt wrote: ....
I think you (or perhaps your compiler) have this backwards.
"extern inline" (or just "inline", since "extern" is default for
functions) will create a function body with external linkage _and_ tell
the compiler to inline calls. So, if you use this in a header file that
is included in ten different source files, you will end up with TEN
function bodies with external linkage.
No, it does not create an external definition. The 'extern' keyword
asserts that there is an external definition, giving the compiler
permission to try to call it, but the external definition itself must be
provided separately. Where that function definition is provided, at
least one declaration of that function in the same translation unit must
omit both the 'extern' and 'inline' keywords - otherwise, it would
provide an 'inline definition' (6.7.4p7), and would therefore NOT
provide an external definition (6.9p5). The most reasonable thing is for
the declaration with this characteristic be the one that occurs at the
start of the external definition, but that's not actually required by
the standard (which seems a little odd to me).
AIUI, explicit inline directives are not an "optimization"; if your
compiler ignores them, it is not conforming.
The main defining characteristic of 'inline' keyword is that it "...
suggests that calls to the function be as fast as possible." That sounds
like a request for optimization to me. Whether or not it is an actual
optimization depends upon whether or not the compiler chooses to take
that suggestion into consideration.
It is only an "optimization" for the compiler to inline calls (and
possibly remove function bodies) when there are NOT any "inline" directives.
'inline' is a keyword, not a directive. The standard uses the word
"directive" only in the context of pre-processing.
It's an optimization, whether or not there's an 'inline' keyword,
because the compiler always has the option of either inlining a function
call, or not inlining it - the 'inline' keyword doesn't change that,
it's no more than a suggestion.
"static" suppresses the generation of (potentially redundant) function
bodies that will never be called.
No, an implementation is still free to create a separate function
bodies, whether or not they ever get called.
If your compiler ignores valid "inline" directives, then it becomes
necessary for ONE of the function definitions to be "extern", so that a
function body is generated, but the rest should be "static".
No, if it decides to ignore the 'inline' keyword, the function body it
creates must have internal linkage, not external. It does not satisfy
the requirement (that might exist due to some other translation unit in
the same program) for a function with that name and external linkage.