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 🙂
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
inlinekeyword 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 itinlineon the following basis:If it is in a
.cppfile for that class, then it’s valid to mark itinlineif it’s called only from that TU. Then it probably makes no difference whether it is markedinlineor not, but you could mark itinlineas 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
inlinekeyword to move with it.