Is it true that if a function body is defined inside a class, the compiler will mark it inline? (even if not marked by the writer)
example:
class F {
public:
void func() {
std::cout << "is this inline?\n";
}
};
Is it true that if a function body is defined inside a class, the compiler will mark it inline? (even if not marked by the writer)
example:
class F {
public:
void func() {
std::cout << "is this inline?\n";
}
};
Yes.
[C++14: 9.3/2]:
A member function may be defined (8.4) in its class definition, in which case it is an inline member function (7.1.2), or it may be defined outside of its class definition if it has already been declared but not defined in its class definition. [..]
However, whether this has any observable effects beyond the associated linkage requirements is only as predictable as the inline
keyword ever is.
The reason for this rule is so that it is legal to include the class definition — member functions and all — via a header into multiple translation units. You would have multiple reference linker errors otherwise.
If you state the body of a function inside a class, it is equivalent to defining that function outside the class and prefixing it with the inline keyword, but whether or not it will be inlined is ultimately up to the compiler.