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

Sam picture Sam · Feb 8, 2012 · Viewed 14.5k times · Source

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

Answer

craigmj picture craigmj · Feb 8, 2012

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.