The inline specifier instructs the compiler to replace function calls with the code of the function body. This substitution is “inline expansion” (sometimes called “inlining”). Inline expansion alleviates the function-call overhead at the potential cost of larger code size.
The inline keyword tells the compiler that inline expansion is preferred. However, the compiler can create a separate instance of the function (instantiate) and create standard calling linkages instead of inserting the code inline. Two cases where this can happen are:
Note that for a function to be considered as a candidate for inlining, it must use the new-style function definition. Functions that are declared as inline and that are not class member functions have internal linkage unless otherwise specified.
Microsoft Specific
The __inline keyword is equivalent to inline.
The __forceinline keyword instructs the compiler to inline the function without performing any cost/benefit analysis. The programmer must exercise good judgement in using this keyword. Indiscriminate use of __forceinline can result in larger, and sometimes even slower, code.
Even with __forceinline, the compiler cannot inline code in all circumstances. The compiler cannot inline a function if:
If the compiler cannot inline a function declared with __forceinline, it generates a level 1 warning (4714).
END Microsoft Specific
As with normal functions, there is no defined order of evaluation of the arguments to an inline function. In fact, it could be different from the order in which the arguments are evaluated when passed using normal function call protocol.
Microsoft Specific
Recursive functions can be substituted inline to a depth specified by the inline_depth pragma. After that depth, recursive function calls are treated as calls to an instance of the function. The inline_recursion pragma controls the inline expansion of a function currently under expansion. See the Inline-Function Expansion (/Ob) compiler option for related information.
END Microsoft Specific