Inline functions

suggest change


A function defined with the inline specifier is an inline function. An inline function can be multiply defined without violating the One Definition Rule, and can therefore be defined in a header with external linkage. Declaring a function inline hints to the compiler that the function should be inlined during code generation, but does not provide a guarantee.



Usually if code generated for a function is sufficiently small then it’s a good candidate to be inlined. Why so? If a function is large and is inlined in a loop, for all the calls made, the large function’s code would be duplicated leading to the generated binary size bloat. But, how small is sufficient?

While inline functions seem to be great way to avoid function calling overhead, it’s to be noted that not all functions that are marked inline are inlined. In other words, when you say inline, it is only a hint to the compiler, not an order: the compiler isn’t obliged to inline the function, it’s free to ignore it - most of them do. Modern compilers are better at making such optimisations that this keyword is now a vestige of the past, when this suggestion of function inlining by the programmer was taken seriously by the compilers. Even functions not marked inline are inlined by the compiler when it sees benefit in doing so.

Inline as a linkage directive

The more practical use of inline in modern C++ comes from using it as a linkage directive. When defining, not declaring, a function in a header which is going to be included in multiple sources, then each translation unit will have its own copy of this function leading to a ODR (One Definition Rule) violation; this rule roughly says that there can be only one definition of a function, variable, etc. To circumvent this violation, marking the function definition inline implicitly makes the function linkage internal.


When should I write the keyword ‘inline’ for a function/method in C++?

Only when you want the function to be defined in a header. More exactly only when the function’s definition can show up in multiple compilation units. It’s a good idea to define small (as in one liner) functions in the header file as it gives the compiler more information to work with while optimizing your code. It also increases compilation time.

When should I not write the keyword ‘inline’ for a function/method in C++?

Don’t add inline when you think your code will run faster if the compiler inlines it.

When will the the compiler not know when to make a function/method inline?

Generally, the compiler will be able to do this better than you. However, the compiler doesn’t have the option to inline code if it doesn’t have the function definition. In maximally optimized code usually all private methods are inlined whether you ask for it or not.

See Also

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents