Is "inline" implicit in C++ member functions defined in class definition

16,935

Solution 1

They're equivalent class definitions except for the purposes of the One Definition Rule. So the standard does not guarantee that you can compile one TU (translation unit) with one class definition and a different TU with the other, and then link them together. I doubt that this would ever actually fail on a real implementation, but that's what the standard says.

The inline keyword has approximately nothing to do with inlining. It's about whether multiple identical definitions of the function are permitted in different TUs. If someone moves the function definition elsewhere, then they should decide whether to mark it inline on the following basis:

  • If it is in a .cpp file for that class, then it's valid to mark it inline if it's called only from that TU. Then it probably makes no difference whether it is marked inline or not, but you could mark it inline as a compiler hint if you think the compiler will pay any attention to what you want.

  • If it is still in the header file, then it must be marked inline, or else you'll get multiple definition errors when linking different TUs that use the header.

Assuming that the person moving the function knows those things, I don't think they need a reminder in the class definition. If they don't know those things, then they probably have no business moving the function, but it would be safer for them to have an inline keyword to move with it.

Solution 2

The C++ ISO standard says:

A function defined within a class definition is an inline function.

But, this doesn't mean the function will necessarily be inlined: generally nowadays, it appears that the compiler will decide if inlining the function will lead to any benefits.

Solution 3

is putting the "inline" qualifier on such member function defined in the class definition completely redundant?

Yes

For code style, would it be sensible to keep the "inline" tag, so a future developer realises that function should be inlined, and does not remove the definition somewhere else and remove the inlining?

No.
The inline is for "One Definition Rule" (and therefore linking by extension). If the function is defined where inline is required and it is not provided it is a compile time error. If it is not needed then it is just extra useless fluff.

So if you don't need it remove it. If you need it put it there (if you forget the compiler will let you know).

Solution 4

The inline is optional in that case, yes. The compiler will know it's inline and not generate multiple instances of the function whenever the header is included.

As to whether its a good idea to leave it there - I don't really think so. It'd be better to give a detailed comment explaining why the function MUST be inlined (of which I can thin of only 2 reasons: either "it's a template" in which case out of lining is impossible, or "performance" in which case I'd want to see some supporting evidence, rather than the "it has to perform better because it's inline" that I've seen in some places.

Share:
16,935
Sam
Author by

Sam

merge keep

Updated on July 01, 2022

Comments

  • Sam
    Sam about 2 years

    According to the C++ specification, are the following two classes equivalently defined?

    class A
    {
       void f()
       {
       }
    };
    
    class B
    {
       inline void f()
       {
       }
    };
    

    i.e., is putting the "inline" qualifier on such member function defined in the class definition completely redundant?

    Followon question: Assuming it is redundant, for code style, would it be sensible to keep the "inline" tag, so a future developer realises that function should be inlined, and does not remove the definition somewhere else and remove the inlining?

    Thanks :)

    • Sam
      Sam over 12 years
      Note to mention - I know that "inline" is just a hint to the compiler. This question isn't about that.
    • Steve Jessop
      Steve Jessop over 12 years
      It's not just a hint. It has an important meaning. That meaning is nothing to do with inlining, it's about the ODR. It's also a hint about inlining.
    • Admin
      Admin almost 3 years
      Not sure if this was true 9 years ago but these days inline has no connection whatsoever to the function actually being inlined. That is up to the compiler.
  • WiSaGaN
    WiSaGaN about 11 years
    TU should be "translation unit" I guess. Just figured it out, and commented here for others' convenience.
  • developerbmw
    developerbmw almost 10 years
    @WiSaGaN Good idea to clarify - I only figured it out after reading it in a few different contexts
  • Praxeolitic
    Praxeolitic over 9 years
    For clang, keyword inline does in fact affect function inlining as well as ODR rule. stackoverflow.com/questions/27042935/…
  • user4815162342
    user4815162342 over 8 years
    This answer quotes the C++98 standard as saying, A member function may be defined (8.4) in its class definition, in which case it is an inline member function (7.1.2) This seems to contradict the first sentence of the answer; according to the quote from the standard, both class definitions define inline member function f, which is explicitly allowed by ODR.
  • Steve Jessop
    Steve Jessop over 8 years
    @user4815162342: Either class A {void f() {}}; or class A {inline void f() {}}; is permitted, but the ODR forbids you to use each of them in different TUs and then link those two TUs together. The ODR permits two definitions of an inline function to be linked (and for that matter two definitions of a class) only if they are identical, meaning among other things they must consist of the same sequence of tokens. Since one definition contains the token inline and the other does not, they aren't permitted to both define the same class A in the same program.
  • user4815162342
    user4815162342 over 8 years
    Thanks for the clarification, I forgot about the "the same sequence of tokens" requirement. It might be a good idea to mention that requirement in the answer next to "for the purposes of ODR", because that would be the only thing making the two classes "different" if one were to name them the same.
  • Daniel Wright
    Daniel Wright almost 6 years
    I agree with Cort. This should be the accepted answer. Just to verify, Section 7.1.2 (titled "Function Specifiers") item 3 of the ISO C++11 standard states, verbatim, what he said: "A function defined within a class definition is an inline function."
  • Andres Salas
    Andres Salas over 4 years
    Many people are unaware of this under-the-hood inlining of functions declared in a class definition. This is helpful.
  • palapapa
    palapapa about 2 years
    The inline keyword in modern C++ isn't about actually inlining anymore but about temporarily lifting the One Definition Rule as long as all definitions are the same so that header-only libraries are possible.