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

How to use the C++ inline functions inline and auto keywords

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "C++ inline function inline and auto keyword how to use", in the daily operation, I believe that many people in C++ inline function inline and auto keyword use problems have doubts, Xiaobian consulted all kinds of information, sorted out a simple and easy to use method of operation, I hope to answer the "C++ inline function inline and auto keyword how to use" help! Next, please follow the editor to study!

1. Introduction of the 1.1 problem of introverted function

We have all learned about functions in C language, and we know that functions need to open up stack frames in the process of calling. If we need to call a function frequently, suppose we call the Add () function 10 times, then we need to build 10 stack frames. We all know that there are a lot of things to do in the stack frame, such as saving registers, pressing parameters, pressing return values, etc., this process is very troublesome. Then in C language, we can solve this problem through macros. In C++, we introduce introverted function (inline).

1.2 the concept of inline function

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.

We still use the Add () function as an example here. This code is the Add () function that we often write. Suppose we call the Add function multiple times. In C language, we can replace it with macros. In C++, we can add inline before the function.

Int Add (int x, int y) {int z = x + y; return z;}

In C language, macros are used instead of Add functions:

# define Add (XBI y) ((x) + (y))

In C++, add inline before the function to make it an inline function

Inline int Add (int x, int y) {int z = x + y; return z;}

Then the C language has been replaced by macros, so why does C++ have inline functions?

There are two main reasons:

1. Macro is difficult to understand and difficult to control, and it is easy to make mistakes. The grammatical mechanism is not well designed.

two。 Macros do not support debugging, but introversion supports debugging under debug (not under debug, but only under release), so our understanding and mastery of the code will be greatly improved.

If you add the inline keyword before the Add function to change it to an inline function, the compiler will replace the function call with the function body during compilation. View method:

1. In release mode, check to see if call Add exists in the assembly code generated by the compiler

two。 In debug mode, the compiler needs to be set, otherwise it will not be expanded (because in debug mode, the compiler will not optimize the code by default, the following is how to set vs2019)

1.3 Properties of introverted functions

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 only 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

Inline is a method of exchanging space for time, saving the overhead of calling function amount (setting up stack frames). So long code or recursive functions are not suitable for inline functions.

How long is the code here? It is usually about 10 lines, depending on the compiler.

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.

For example, suppose we have a code that requires 10 lines, but we need to call it 1000 times. If inline is replaced, we will have 1000 instructions, and if we do not, we will have 1000 instructions. So it is up to the compiler to decide whether to replace it eventually.

Inline int Add (int x, int y) {int z = x + y; z + = x * y Return z;}

We found that although the Add function was preceded by inline, it was not expanded in the end.

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

Let's call it in f.cpp: we found that it was wrong.

Here he will have a link error:

This is because the replacement in test.h to test.cpp is found to be an inline function, and the inline function does not need to generate an address, because the place where the inline function is called is expanded, so it will not exist in the symbol table. Cannot be found when external calls are made. So don't separate the declaration from the definition.

Introduction to the 2.auto keyword 2.1 auto

In the early days, auto meant that variables modified with auto were local variables with automatic memory. In standards 11, the standards committee gave a new meaning to auto: auto is no longer a storage type indicator, but as a new type indicator to instruct the compiler, and the variables declared by auto must be derived by the compiler at compile time.

Auto in C language we have come into contact with: the most generous keywords, because local variables are auto-modified by default, so auto can be omitted, which leads to auto is often ignored. So auto has been upgraded to have a new function-automatic derivation-in Clippers 11. (note: the new auto feature is available only after Clipper 11.)

So how does auto deduce automatically?

We can use typeid to print the type of a variable.

Int main () {const int a = 10; auto b = & a; auto c = 'aura; cout

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