A said:
Hi,
I'm having problems completing a project in C++. I have been using
inline functions in some of my header files. I have only done so for
simple functions that only have 1 statement (eg. accessor and mutator
methods to access private data members). I would like to know if
there are any issues with using inline functions that may have
attributed to my errors before I start separting them out into
"outline functions".
As the keeper of the X-files pointed out: it is very very unlikely that from
a language point of view they would make any difference. However ther is
another point to this. If you keep chaning them and you fail to recompile
all implementation files including those inline functions you may end up
violating the ODR (One Definition Rule). It is also worth to note that
inline functions may have an out-of-line body generated by the compiler.
Depending on your linker and link order those may also mix things up unless
you really recompile everything including the header with the inline
functions.
Having said all that those are very good reasons to only start inlining when
your profiling shows the functions to be a bottleneck. Otherwise you will
create dependencies with no value.
It is also a common technique to put inline functions into a separate file
(not the header, not the implementation) and include that file into the
header (inlined) or the implementation (not inlined) depending on a
preprocessor macro you define in your <OT>makefile or the compiler command
line</OT>. You can make those macros also one per class and a major one to
make everything out-of-line. In that case if you want to check if inlines
give you trouble or you want a clean profiling again you just recompile
everything without them.
While inlines in theory will not change the meaning of your code one cannot
rule out some sort of compiler error. Also agressive optimization can
completely optimize away inline functions if all of their input is compile
time constant and all of their output is their return value (no side
effects). But then again this should not change the meaning. But if the
compiles fails and optimizes too much...
A sidenote: accessor/mutator methods usually are a sign of inferior OOD
(Object Oriented Desing) where the classes do not really represent a
coherent concept.