"C++" inline functions

  • Inline function concept

A function decorated with inline is called an inline function. When compiling C++, the compiler will expand where the inline function is called. There is no overhead of function pushing on the stack. Inline function improves the efficiency of program operation.

Does it feel similar to macros, but macros can define functions as well as constants. By the way, the advantages and disadvantages of macros are mentioned.
Advantages:
1. Enhanced code reusability
2. Improved performance
Disadvantages:
1. Inconvenient to debug macros (pre-compiled The conversion is carried out at the stage)
2. The code's readability is poor, the maintainability is poor, and it is easy to misuse
3. There is no type safety check

So it is recommended to use inline function and const instead of macro function definition and constant definition respectively

In the debug version, the inline function will not be expanded and needs to be set, and the release version will expand the function because of the compiler optimization

  • Inline function features

Inline is a practice of trading space for time, which saves the overhead of calling functions. Therefore, the code in the function is very long or there is a loop/recursion.
Inline is just a suggestion to the compiler. If the code in the function is very long or there are loops/recursion, etc., the compiler will automatically ignore the inline.
Inline is not recommended to separate declaration and definition. Separation will lead to link errors. Because inline is expanded, there is no function address, and the link will not be found.

Guess you like

Origin blog.csdn.net/NanlinW/article/details/103149530