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

后端 未结 4 724
无人及你
无人及你 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:52

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-01 11:01

    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.

    0 讨论(0)
  • 2020-12-01 11:03

    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.

    0 讨论(0)
提交回复
热议问题