Does the compiler decide when to inline my functions (in C++)?

Whether or not a fiunction is inlined is, at the end of the day, entirely up to the compiler. Typically, the more complex a function is in terms of flow, the less likely the compiler is to inline it. and some functions, such as recursive ones, simply cannot be inlined.

The major reason for not inlining a function is that it would greatly increase the overall size of the code, preventing iot from being held in the processor's cache. This would in fact be a pessimisation, rather than an optimisation.

As to letting the programmer decide to shoot himself in the foot, or elsewhere, you can inline the function yourself - write the code that would have gone in the function at what would have been the function's call site.


Yes, the final decision of whether or not to inline your code lies in the C++ compiler. The inline keyword is a suggestion, not a requirement.

Here are some details as to how this decision is processed in the Microsoft C++ Compiler

  • http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx

As many have already posted, the final decision is always up to the compiler, even if you can give firm hints such as forceinline.
Part of the rationale is that inlining is not an automatic "go faster" switch. Too much inlining can make your code much larger, and may interfere with other optimizations. See The C++ FAQ Lite about inline functions and performance.