inline

Contents[Show]

Thanks to inline the compiler can replace the function call by the function body. There are two reasons to use inline functions: performance and safety.

 

My primary goal was it to write in this post about performance. Fortunately a further great benefit of inline came in my mind. inline makes macros as function replacement superfluous.

Macro must go

Macros are only the poor man means to replace text. Macros have no understanding of the C++ syntax. Therefore, a lot can go wrong.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// macro.cpp

#include <iostream>

#define  absMacro(i) ( (i) >= 0 ? (i) : -(i) )

inline int absFunction(int i){
  return i >= 0 ? i : -i;
}

int func(){ 
  std::cout << "func called" << std::endl;
  return 0;
}


int main(){
  
  std::cout << std::endl;
  
  auto i(0);
  auto res = absMacro(++i);   
  std::cout << "res: " << res << std::endl;
  absMacro(func());
  
  std::cout << std::endl;
  
  i=0;
  res= absFunction(++i);     
  std::cout << "res: " << res << std::endl;
  absFunction(func());
  
  std::cout << std::endl;
  
}

 

inline

As well the macro in line 5 as the inline function in line 7 - 9 return the absolute value of its arguments. I invoke the function with the argument ++i. i is 0. The result should be 1. Should be because the macro increments the expression i two times. Consequently, the result is 2 instead of 1. The function func shows it explicitly. When I use the function func as argument, the function will be two times invoked in case of the macro but only one time in case of the inline function.

What's happening if I use an inline function?

inline

At first, all behaves not like it seems. The compiler will interpret it only as a recommendation if I declare a function as inline. The compiler is free to ignore my recommendation. But it will also work the other way around. Modern compiler like Microsoft Visual C++, gcc, or clang can inline a function if it makes sense from a performance perspective.

Now I have to write in the conjunctive. We have to assume the compiler will accept my recommendation and applies the inline keyword in the exchange function.

inline void exchange(int& x, int& y){
  int temp= x;
  x= y;
  y= temp;
}

 

What's happening at the function invocation?

...
auto a(2011);
auto b(2014);
exchange(a,b);
...

 

The compiler substitutes the function call by the function body.

...
auto a(2011);
auto b(2014);
int temp= a;
a= b;
b= temp;
...

 

The small example shows the advantages and disadvantages to inline a function.

Advantages

  • No function call necessary.
  • Variables have not to pushed onto or removed from the function stack.
  • The function needs no return value.
  • The instruction cache can be used optimally. New instructions have not to be loaded but can be immediately performed.

Disadvantages

  • The size of the executable grows.

Although I only mentioned one disadvantages that should not be a judging. The usage of the keyword inline is a balance between performance versus size of the executable. That was the simple rule. The details are a lot more complicated. The executable may become faster or slower, bigger or smaller by the usage of inline. inline can cause or prevent the crash of your program. inline case increase or decrease the number of cache misses of your program. Who wants to be confused should read the FAQ about inline functions at isocpp.org.

Until this point I only wrote about functions. Of course you can also declare methods as inline.

inline methods

A method can become implicitly and explicitly inline. Implicitly if you define the method in the class body. Explicitly if you define the methods outside the class body and use the keyword inline.

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// inline.cpp

class MyClass{
public:
  void implicitInline(){};
  void explicitInline();
  inline void notInline();
};

inline void MyClass::explicitInline(){}
void MyClass::notInline(){}

int main(){

  MyClass cl;
  cl.implicitInline();
  cl.explicitInline();
  cl.notInline();

}

 

Therefore, the method implicitInline (line 5) is inline because I defined it in the class boy. Therefore, the method explicitInline (line 6) is inline because I used the keyword inline at the point of the method definition. I want to stress one point. If I use only the keyword inline at the point of the method declaration, I will not get an inline function. This error happened to me with the method notInline (line 7).

General recommendation

Good advice is expensive. Should you use the keyword inline ever or never? Of course, the answer is not so simple. You should use inline if you have a function that is time critical and you invoke this function not to often. In this case the performance advantages will dominate the size disadvantages.

But we have to keep the big picture in our mind. The Working Group WG 21 wrote 2006 the paper ISO/IEC TR 18015 about C++ performance.  The chapter 5.3.4 of the paper is explicitly about the keyword inline on five popular C++ compilers. They compare in this chapter functions, inline functions, and macros.  The conclusion in the paper is that inline function calls are about 2-17 time faster than function calls and that inline function calls and macros are in the same performance range. 

If this rule of thumb is to simple for you, you should measure the performance of your program. This is in particular true for embedded systems that have stronger resource concerns.

 

After getting much attention at Reddit for missing the main point about inline functions I will add a few words about ODR.

ODR

ODR stand for the One Definition Rule and says in the case of a function.

  • A function can have not more the one definition in any translation unit.
  • A function can have not more the one definition in the program.
  • Inline functions with external linkage can be defined in more than one translation. The definitions has to satisfy the requirement that each definition has to be the same.

In modern compilers the keyword inline is not about inlining functions any more. Modern compiler almost completely ignore it. The more or less use-case for inline is to mark functions for ODR correctness. In my opinion, the name inline is nowadays quite misleading. 

C versus C++

Sorry, the confusion will not end here. I want to explicitly stress that point.inline function by default have external linkage in C++. This is different from C. In C inline functions by default have internal linkage. You can read the details in the article Linkage of inline functions.

What's next?

This was a post about classical C++. In the next post I will write about C++11. C++11 has the keyword constexpr. You can use constexpr for values, functions, and user-defined data types. By constexpr declared constant expression can be evaluated at compile time. They offer a lot of benefits. Which? You will see in the next post.

 

 

 

 

 

 

 

 

title page smalltitle page small Go to Leanpub/cpplibrary "What every professional C++ programmer should know about the C++ standard library".   Get your e-book. Support my blog.

 

Tags: inline

Comments   

-1 #1 Denis 2016-11-12 21:31
Great article!
Small note:
>>> "Inline functions with external linkage"
As far as I know functions declared with inline have internal linkage. Generally, everything you wrote is fine, just this phrase is misleading IMO.
Quote
0 #2 Rainer Grimm 2016-11-13 09:18
Quoting Denis:
Great article!
Small note:
>>> "Inline functions with external linkage"
As far as I know functions declared with inline have internal linkage. Generally, everything you wrote is fine, just this phrase is misleading IMO.

I added a few sentences in the paragraph C versus C++.
Quote
0 #3 wzrok 2016-12-03 23:06
Hello There. I discovered yоur weblog tҺᥱ usage
of msn. Thiis iѕ ann extremely welⅼ written article.
I'll make syre too bookmark it and return tо learn extra of
ʏour սseful info. Ꭲhank уou fоr the post.
I will dеfinitely comeback.
Quote
0 #4 Branden 2017-03-24 03:37
whoah this blog is magnificent i like studying your articles.
Keep up the goiod work! You understand, a lot of
people are looking round for this information, yyou could
aid them greatly.
Quote

Add comment


Support my blog by buying my E-book

Latest comments

Modernes C++

Subscribe to the newsletter

Including two chapters of my e-book
Introduction and Multithreading

Blog archive

Source Code

Visitors

Today 506

All 219776

Currently are 66 guests and no members online