Is “inline” implicit in C++ member functions defined in class definition

后端 未结 4 739
无人及你
无人及你 2020-12-01 10:36

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

class A
{
   void f()
   {
   }
};

class B
{
   inline void f()
   {         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 10:54

    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.

提交回复
热议问题