In C++, you can declare a member function and immediately give it a body:
// Let's assume this is in some Utilities.h file.
struct Foo
{
void doIt() { /* Very important code */ }
};
But now you are in for some trouble, because if you #include <Utilities.h>
from multiple .cpp files, you now have multiple definitions of doIt
, which is undefined behavior, and the compiler will light your computer on fire.
I know two ways of getting around this:
The stupid one: Make doIt
a template:
template<typename Dummy = void>
void doIt() { ... }
The "Do you like giant executables?" one: Declare doIt
inline:
inline void doIt() { ... }
Is there a better way of declaring a member function with an in-line body without having it actually inlined by the compiler (as far as that's possible).