Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for clarification on static and inline functions

Tags:

c++

static

inline

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?

like image 234
Aberrant Avatar asked Jun 11 '14 18:06

Aberrant


1 Answers

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).
like image 167
nneonneo Avatar answered Oct 13 '22 18:10

nneonneo