inline
is essentially ignored on modern mainstream compilers (it just enables the usual exemption to the ODR for the function). The optimizer will do his thing anyway, and it probably knows better than you or me (although there's usually some compiler-specific flag to force the inlining of a function).
Anyway, probably you are not seeing any inlining there because you compiled with optimizations disabled - a setting used often in debugging, where inlining and other optimizations makes impossible to match source lines to compiled code (and thus stepping through in a debugger). Enabling optimizations -O3
on my machine (g++ 4.8.4) makes the function disappear completely - the result (4
) is evaluated completely at compile time.
To sum it up: currently, the point of inline
is to allow you to supply the function definition in multiple translation units without having linker errors for multiple definitions of a function.
This is useful because if you have a good candidate for inlining you want to put it into a header file (so that compiling each .cpp
the compiler can see immediately its definition, and expand it inline right away - link time code generation is just a recent thing) but you want the linker to know that the extra non-inline
copies that arise refer all to the same thing, and can be discarded safely (marking them as static
would lead to unnecessary code duplication in each TU).