Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the inline function in C++

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report