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

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 :)


Solution 1:

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 2:

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 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).