The inline keyword

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The basics

    /

  4. The inline keyword

The problem

Calling a function causes overhead. The additional overhead is often worth it, especially when the function contains a great amount of instructions and when it is called in many places in the code. However, while the overhead is drowned by advantages when it comes to big functions, it is very noticeable for very small functions.

A solution

A solution to that problem would be to manually take the code inside small functions and copy it to places we want to use it instead of calling them (That pretty much means not using functions...). This is not a great idea because calling functions is very convenient as it is often clearer and more concise. A better, but not the best, way would be to use preprocessor macros instead of small functions. However, macros are way more limited in what they can do than functions.

Compiler optimization of function call

To resolve the problem of function calling overhead, the compiler may, at compilation time, replace the calls of some functions by their content, when they are not worth the overhead.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream> void func() { std::cout << "C++" << std::endl; } int main() { func(); return 0; }

In the example above, the line func(); is likely going to be replaced by std::cout << "C++" << std::endl; at compilation time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream> int add(int a, int b) { return a + b; } int main() { int num1, num2; std::cin >> num1 >> num2; int num3 = add(num1, num2); return 0; }

In the code above, the line int num3 = add(num1, num2); will probably become int num3 = num1+num2; at compilation time. When the compiler replaces the call of a function by its content, the effect of the function call is similar to using a preprocessor macro.

Note that replacing a function call by its code usually increases the size of the binary code generated by the compilation process (A function call is lighter in term of number of instructions). Therefore, we sometime have to choose between higher speed or lighter executable.

The inline keyword

The inline keyword is used to ask the compiler to replace the calls of a function by its instructions. Note, however, that this is only a suggestion to the compiler. If it judges it is not worth it performance wise, it will not do it. The inline keyword is placed, in the definition/declaration of the function, before the name of the return type:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream> inline void func(); inline void func() { std::cout << "C++" << std::endl; } int main() { func(); return 0; }

The most important thing to remember from that page is that the compiler may replace the function calls by their content. Using the inline keyword can be neglected as it is likely to have no effect, since the compilers know what they are doing and they do not really need us to tell them when to inline a function or not.