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 inline function of C++

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

Share

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

In this article, the editor introduces in detail "how to use the inline function of C++". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use the inline function of C++" can help you solve your doubts. Let's follow the editor's ideas slowly and deeply, together to learn new knowledge.

A review of constants and macros

The const constant in C++ can replace the definition of macro constant, such as:

But is there a workaround in C++ to replace the macro code snippet? Inline functions are introduced here.

Second, inline function

It is recommended to use inline functions instead of macro code snippets in C++.

Declare inline functions using the inline keyword in C++

The inline keyword must be combined with the function definition when inline function declaration, otherwise the compiler will directly ignore the inline request

The C++ compiler can compile a function inline

Functions inline compiled by C++ compiler are called inline functions

The C++ compiler directly inserts the function body into the place where the function is called.

Inline functions do not have the extra overhead of ordinary function calls (stack, jump, return)

The C++ compiler does not necessarily satisfy the inline request of the function

Let's take a look at a piece of code defined by a macro:

# include # define FUNC (a, b) ((a) < (b)? (a): (B) inline int func (int a, int b) {return a < b? A: B;} int main (int argc, char * argv []) {int a = 1; int b = 3; int c = FUNC (+ + a, b); printf ("a =% d\ n", a); printf ("b =% d\ n", b); printf ("c =% d\ n", c); return 0;}

The following is the output result, c = FUNC (+ + a, b), which is equivalent to c = ((+ a) < (b)? (+ + a): (b)), execute (+ a) < (b) first, and get a =, 2) b = 3. Because + + an is smaller than b, the output + + an is 3 at this time, so c is also equal to 3.

If you use inline functions

# include # define FUNC (a, b) ((a) < (b)? (a): (B) inline int func (int a, int b) {return a < b? A: B;} int main (int argc, char * argv []) {int a = 1; int b = 3; int c = func (+ + a, b); printf ("a =% d\ n", a); printf ("b =% d\ n", b); printf ("c =% d\ n", c); return 0;}

The output is as follows:

Let's take a look at the disassembled code in VS2012 to see if the inline is successful. You can see that the func function is called instead of extending directly to the place where it was called, and inlining is not successful. The keyword inline simply requests that the function be inlined, but it may not be successful.

In order for the compiler to allow inline requests, you can configure the compiler, as shown in the following figure

Once configured, make a breakpoint in front of int c = func (+ + a, b); and start running the code, but I made a mistake when I ran.

At this time, make the following configuration, and the error will be solved.

At this time, disassembly is being carried out, as shown in the following figure, you can see that the assembly code for calling the function is gone, indicating that the inline is successful.

Inline functions have the characteristics of ordinary functions (parameter checking, return types, etc.)

The inline request for a function may be rejected by the compiler

After the function is inline compiled, the function body is directly extended to the place where it is called.

The macro code snippet is processed by the preprocessor for simple text replacement without any compilation process, so side effects may occur.

Modern C++ compilers can compile and optimize, and some functions may be compiled inline even if there is no inline declaration

Some modern C++ compilers provide extended syntax to force inlining functions, such as:

Gmail +: _ attribute__ ((always_inline)) attribute

MSVC: _ forceinline

Matters needing attention in the use of inline functions

Restrictions on inline compilation of inline in C++:

There cannot be any form of circular statement

There cannot be too many conditional judgment statements.

The function body should not be too large.

Cannot address a function

Cannot address a function

After reading this, the article "how to use the inline function of C++" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, welcome to 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