On 26 June 2015 at 10:40, Martin Jambor wrote: >> (1) Should inline member functions be implemented inside the class or outside >> the class? If inside, should they be formatted like this: > > https://gcc.gnu.org/codingconventions.html has a big section on C++ > (note that there is also a similar document on wiki which IIRC often > contradicts this one). Unfortunately, in this case, it gives two > sightly contradicting guidelines. On one hand, section > https://gcc.gnu.org/codingconventions.html#Cxx_Inlining says > > "Constructors and destructors, even those with empty bodies, are > often much larger than programmers expect. Prefer non-inline > versions unless you have evidence that the inline version is smaller > or has a significant performance impact. " > > on the other, https://gcc.gnu.org/codingconventions.html#Member_Form > explicitely states: > > "Define all members outside the class definition. That is, there are > no function bodies or member initializers inside the class > definition. " > > Since the former seems to be specific to constructors and destructors, > it probably overrides the latter. So in my book, you can have inline > constructors and destructors if they are extremely small, such as > empty, but nothing else should be inline.
It's not clear to me that they're talking about the same thing, or if they are then the terminology is confused. You can have inline functions defined outside the class body, simply by marking them 'inline'. So the first rule seems to be talking about whether or not constructors and destructors should be inline, which does not conflict with the rule to define everything outside the class body. Taken together the two rules say you should do this: struct foo { foo(); ~foo(); }; inline foo::foo() { } inline foo::~foo() { } And not: struct foo { foo() { } ~foo() { } }; Semantically these are identical, in both cases the functions are defined inline, as allowed by the first rule. Only the first form meets the second rule. (For this to work, the out-of-class function definitions must be visible to all callers of those functions, so if foo is defined in a header the functions must also be defined in the header). However, the first rule also says that unless the constructor is very small you should really do this instead: struct foo { foo(); ~foo(); }; // this is not inline foo::foo() { } // this is not inline either foo::~foo() { } (In this case the function definitions are not inline, so must not be defined in a header, but in a single .c file.)