2

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?

vladon
  • 8,158
  • 2
  • 47
  • 91
  • 18
    Or neither, which is the answer. – StoryTeller - Unslander Monica Jan 11 '18 at 09:13
  • If you declare and define a static member function inside a class, then the function is implicitly inline. If you declare a static non-member function, then you should consider using unnamed namespace instead of `static`. – cpplearner Jan 11 '18 at 09:15
  • @cpplearner I don't think that's correct. An "inline member function" is not the same as an "inline function", which is covered in [\[dcl.inline\]](https://timsong-cpp.github.io/cppwp/dcl.inline#:inline_function). You cannot use the `inline` keyword on a member function for example. – user167921 Jan 11 '18 at 09:18
  • 2
    @cpplearner Defining *any* member function inside a class definition makes it implicitly `inline`. It doens't matter if it's `static` or not. Instead `static` member functions means something completely different and is still unrelated to the "inline-ness" of a member function. – Some programmer dude Jan 11 '18 at 09:24
  • 4
    @user167921 `You cannot use the inline keyword on a member function` why not? It seems to work on my compilers. – UKMonkey Jan 11 '18 at 09:24
  • If you do declare a member function `inline`, it technically has no effect within the standard, but the compiler might use it as a hint that you would like that function to be inlined. – Brian Bi Jan 11 '18 at 09:28
  • 3
    @UKMonkey - That's because your compilers are obligated to accept valid code. Yes, even `new` and `delete`... – StoryTeller - Unslander Monica Jan 11 '18 at 09:30
  • 2
    @Brian You must declare a member function inline in the class, if you provide it's implementation outside of the class, but within the header file: `struct X { inline void doIt(); }; void X::doIt() {}` – king_nak Jan 11 '18 at 09:33
  • @king_nak OK, that's true, you could do that. What I said applies to the case where you define the function within the class definition. – Brian Bi Jan 11 '18 at 09:35
  • @StoryTeller I'd like to think that compilers would reject code like `std::make_shared()->foo()` which would wrap a new & delete just to call foo on a temp object ... but sadly that's accepted too – UKMonkey Jan 11 '18 at 09:43
  • 1
    Related to [difference-between-an-inline-function-and-static-inline-function](https://stackoverflow.com/questions/12836171/difference-between-an-inline-function-and-static-inline-function) – Jarod42 Jan 11 '18 at 10:22
  • @UKMonkey - You know, that's actually not that bad. At least it's exception safe. – StoryTeller - Unslander Monica Jan 11 '18 at 12:21

3 Answers3

2

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
}

Demo

I cannot found differences between static and static inline though.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

No, inline does not imply static not vice versa. See inline nad static description.

Tomator
  • 81
  • 8
0

(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!

Gem Taylor
  • 5,381
  • 1
  • 9
  • 27