7

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:

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?

Community
  • 1
  • 1
Aberrant
  • 3,423
  • 1
  • 27
  • 38
  • 1
    You're confusing descriptions of file scope with descriptions of program scope. When you say "more than once", you are being ambiguous. You need to say, "more than once in the same ". – David Schwartz Jun 11 '14 at 19:02
  • C++ is economical with keywords, which leads to the same keyword meaning many different things. – juanchopanza Jun 11 '14 at 19:03

1 Answers1

10

inline is easy to explain. inline basically is a hint to the compiler that the given function should be copied (inlined) into any function that calls it, rather than doing a normal function call. For short, simple functions, this eliminates function call overhead.

The compiler is not required to obey inline: it can choose to generate a normal function if it decides the inlining isn't worth the extra code bulk (which can cause cache misses and increase code size).

inline functions are only available in the source file that declares them. Therefore, inline functions intended for wide reuse are often placed in header files.


static has a bunch of different uses in C++:

  • A static class method or member belongs essentially to the class rather than any particular instance of the class, like Java's static. This applies to both static methods and static variables. Note that there is only ever one copy of a static class member, even if you put the declaration in a shared header file; the definition must be in only one file (except for static const members)
  • A static global function or variable is accessible only to the compilation unit (source file) in which it is defined. In this way, it is a bit like an inlined function, and compilers may choose to inline a static function. Static functions that are meant to be shared are also placed in header files.
  • A static local variable inside a function is a variable which exists across all invocations of that function, but which is accessible only inside that function (unlike local variables, which are private to a particular invocation of the function).
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • So does this mean my original thoughts at the top of my question were correct for class scope methods, but not global scope functions? – Aberrant Jun 11 '14 at 19:17
  • @Aberrant: Essentially. For global scope functions, actually it is the non-`static` functions which only exist once (multiple definitions are prohibited by the linker). – nneonneo Jun 11 '14 at 19:19
  • There is one caveat with static local variables: when they are inside class methods they implicitly (and *silently*) behave as static class members. – Casey Jun 12 '14 at 15:07
  • @Casey: I'm not sure I see the distinction. How does the behaviour of a static local variable change if it's inside a class method? – nneonneo Jun 12 '14 at 16:36
  • Instead of behaving as an instance variable local to the method that saves its previous value between method calls and which other class instances may have their own value-saving copy, local method variables declared `static` behave as members declared at class scope. I.e., *all* class instances retain the most recent value. (See the first bullet point) It makes sense to do so, but uninformed programmers (myself included, before I discovered the problem) may think that variables declared `static` at method scope behave just like variables declared `static` at non-class function scope. – Casey Jun 12 '14 at 19:25
  • (continued) The way to solve the issue is to create a class-level member variable that is non-static and as such can then be instanced and behave as expected. – Casey Jun 12 '14 at 19:27
  • @Casey: Ah. In this sense it's behaving exactly like a static local variable in any other method or function: it's globally static. I see where the confusion is, if one initially assumes it is static w.r.t the instance. – nneonneo Jun 12 '14 at 19:27