For free (NOT inside classes) functions:
static
impliesinline
?inline
impliesstatic
?
Or both?
Considering examples:
static void foo1() { ... };
static inline void foo2 { ... };
inline void foo3() { ... };
What the difference?
For free (NOT inside classes) functions:
static
implies inline
?inline
implies static
?Or both?
Considering examples:
static void foo1() { ... };
static inline void foo2 { ... };
inline void foo3() { ... };
What the difference?
There are differences, consider (in header):
static int foo1() { static int i = 0; return ++i; }
static inline int foo2() { static int i = 0; return ++i; }
inline int foo3() { static int i = 0; return ++i; }
and in 2 cpp "void caller[1-2]_foo[1-3]() { std::cout << foo[1-3]() << std::endl; }
".
So
int main()
{
caller1_foo1(); // 1
caller2_foo1(); // 1
caller1_foo2(); // 1
caller2_foo2(); // 1
caller1_foo3(); // 1
caller2_foo3(); // 2
}
I cannot found differences between static
and static inline
though.
(S) Static tells the compiler that the code is only used from this module. You may even get an "unused" warning if you don't use it.
(I) Inline tells the compiler to "try harder" to inline the code. It is not a guarantee, either way, but compilers have various options and pragmas to control this.
If the compiler decides to inline it in this module, then everyone is happy. If the method is also used in another (source) module, that module will decide for itself what to do.
(sI) If the compiler decides not to inline the non-static function, then it will exist as a stand-alone function.
(SI) The presence of 'static' has an effect. If it is static, the name is not emitted to the linker: another module would generate another function with the same name, perhaps from the same source, perhaps not. If it is not static then the compiler will emit the symbol to the linker. If another module also decides not to inline, then it will be emitted twice, and the linker handles this by just picking one (seemingly at random for dynamic linking)!
(Si) Use of static duplication can improve code locality, where non-static code pays for non-locality in all but 1 call. Non-locality is not really a big issue, however. This does also apply to inline tagged methods the compiler decides not to inline.
(si) Note that a non-static non-inline function that the compiler decides to inline anyway has to be emitted as a non-inlined function to the linker as well as the inlined code, in case some other module should extern to it. The linker can discard this function if it is not referenced externally (or exported)
Does it make much difference? probably not!