Inline functions are just a request to compilers that insert the complete body of the inline function in every place in the code where that function is used.
But how the compiler decides whether it should insert it or not? Which algorithm/mechanism it uses to decide?
Thanks,
Naveen
Some common aspects:
The 3rd is probably the core of your question, but that’s really “compiler specific heuristics” – you need to check the compiler docs, but usually they won’t give much guarantees. MSDN has some (limited) information for MSVC.
Beyond trivialities (e.g. simple getters and very primitive functions), inlining as such isn’t very helpful anymore. The cost of the call instruction has gone down, and branch prediction has greatly improved.
The great opportunity for inlining is removing code paths that the compiler knows won’t be taken – as an extreme example:
A good compiler would inline
Foo(false), but notFoo(true).With Link Time Code Generation,
Foocould reside in a .cpp (without ainlinedeclararion), andFoo(false)would still be inlined, so again inline has only marginal effects here.To summarize: There are few scenarios where you should attempt to take manual control of inlining by placing (or omitting) inline statements.