Originally, I thought static
and inline
meant the following for functions:
WARNING: This is what I used to think, don't assume this is correct.
A static
function exists only once. Everything that uses it uses the same function.*
An inline
function's content is, presumably, copied into the calling function. Compilers may actually ignore this but in case of definitions in non-template header files they are necessary to avoid duplicate definitions.
A static inline
function, I still haven't figured out what that should mean.
*with the added note that class templates effectively generate classes, so their static contents are completely different for each derived type.
I got this impression from the book C++ for Java Programmers (Mark Allen Weiss, ISBN 0-13-919424-X). At paragraph 2.1.6, it says:
In some situations, the overhead of making a function call can be significant. For instance, the max2 routine is trivial, and so one might be tempted to simply replace the function invocation in main with the code that max2 is logically performing: ... Of course, this would be sacrificing good programming practice for speed.
To avoid this one can use the inline directive. The inline directive suggests to the compiler that it should generate code that avoids the overhead of a function definition ...
Surprisingly, I can't find anything about static
functions. It might be there but I can't find it in the index.
But then I found this answer, which seems to claim the exact opposite:
The non-static inline function declaration refers to the same function in every translation unit (source file) that uses it.
and
If it is static then each TU has its own version of the function and hence its own copy of the static local variables.
This answer seems to take a different stance on what inline
means, agreeing with my original interpretation:
inline conveys exactly what you want: "please suppress the ODR rule for this function, so that each translation unit can (and must) supply its own copy of the function's definition".
But then proceeds to say an inline
function can either be inlined (I assume this means duplicated into every place where it's called) or merged together.
The compiler will then either inline calls to the function, or merge together the function definitions from different TU's (so that the resulting function exists once in the executable).
It then also says that declaring a function static
means an arbitrary number of the function will exist:
static, on the other hand, tells the compiler to generate the function in every translation unit where it is defined, and just not share it. So you end up with an arbitrary number of technically separate functions existing in the resulting executable.
I'm not sure, but it sounds like static
functions exist more than once and inline
functions, when the compiler actually performs the inline, exist only once. Which would be the exact opposite of what I used to think.
But to top it off, here's a bunch of questions about singletons where every getInstance function is declared static
:
- C++ Singleton design pattern
- Singleton: How should it be used
- C++ Singleton class getInstance (as java)
- C++ different singleton implementations
So if static
functions can really exist more than once, this would actually mean that there will be multiple local static
singleton objects and every C++ singleton example I ever saw would be wrong, which seems unlikely.
I don't get it anymore. Everything seems to be suggesting something else. What do static
, inline
, and, as a bonus: static inline
, really mean for functions?