If the function is small (the likelihood that you change it often is often small), and if the function can be placed in the header without including a myriad of other headers (because your function depends on them), this is absolutely true for Do it. If you declare them extern inline, then the compiler must provide it with the same address for each compilation unit:
headera.h:
inline string method() { return something; }
Member functions are implicit built-in if defined inside their class. The same is true for them: if they can be placed in the headline without the hassle, you really can do it.
Since the function code is placed in the header and visible, the compiler can embed calls into them, that is, put the function code directly on the call site (not so much because you inserted it before it, but more because the compiler solves this way, though. Entering inline only hints for the compiler regarding this). This can lead to increased performance, because the compiler now sees where the arguments correspond to variables local to this function, and where the argument is not aliased to each other - and, last but not least, the assignment of function frames is no longer required.
As far as I understand, when the compilation is complete, the compiler will expand the header file and place it where it is included. It is right?
Yes, it's right. The function will be defined in all places where you include the header. The compiler will take care of placing only one instance in the resulting program, excluding the rest.
Johannes Schaub - litb Jan 17 '09 at 14:44 2009-01-17 14:44
source share