Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the inline function in C++

Shulou Source: shulou.com Published: 2022-06-01 05:20:50 10月03日 Update

This article mainly introduces what the inline function in C++ is, the article is very detailed, has a certain reference value, interested friends must read it!

1. Concept

The functions modified by inline are called inline functions. When compiling, the C++ compiler will expand where the inline functions are called. There is no overhead of function stack. Inline functions improve the efficiency of the program.

two。 Function demonstration

Let's first take a look at the common functions:

# include using namespace std; int Add (int left, int right) {return left + right;} int main () {Add (1,2); return 0;}

In the assembly code, we can see function calls and stack pressing:

Let's try adding inline:

# include using namespace std;inline int Add (int left, int right) {return left + right;} int main () {Add (1,2); return 0;}

Disassembly:

It's still the same. why?

In the debug version, the compiler will not expand inline functions because expansion cannot be debugged.

We need to set the property: (vs2019)

First, let's open the properties panel:

Next, debug:

There is no function call!

Disassembly:

3. Function characteristic

1.inline is a method of exchanging space for time, saving the overhead of calling function amount. So functions with long code or loop / recursion are not suitable to be used as inline functions.

2.inline is just a suggestion for the compiler, the compiler will automatically optimize, if the function defined as inline has a loop / recursion, etc., the compiler will ignore inlining when optimizing.

3. Inline does not recommend the separation of declaration and definition, which will lead to link errors. Because inline is expanded, there is no function address, and the link will not be found.

4. Functions can be defined instead of macros.

These are all the contents of the article "what are the inline functions in C++?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Functions compilers compilers caterpillars code content properties suggestions overhead articles links recursion loops ordinary next values practices interests just addresses Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi Microsoft Shulou Technology Shulou Information