[C++] Inline functions③ (C++ compilers do not necessarily allow inline requests for inline functions | Advantages and disadvantages of inline functions | Comparison between inline functions and macro code fragments)





1. Inline functions are not necessarily successful




1. Advantages and disadvantages of inline functions


"inline function" is not called at runtime,

"Inline function" is to embed the CPU instruction corresponding to the function body directly into the place where the function is called when compiling.

Thereby reducing the overhead of function calls and improving the execution efficiency of the program;


The disadvantage of the inline function is also obvious, that is, it will increase the size of the code, how many times the inline function is called, and how many times the code instruction of the inline function must be copied to the calling place;

Use "inline functions" carefully to avoid unnecessary overhead and code bloat;


2. The C++ compiler does not necessarily allow inline requests for inline functions


Since "inline function" will cause unnecessary overhead and code expansion , the C++ compiler does not necessarily guarantee the success of the inline request;

Use the inline keyword to request the C++ compiler to inline the function, but the compiler does not necessarily accept this request;


Weigh the pros and cons: Whether the "inline function" is successfully inlined depends on the implementation and optimization strategy of the C++ compiler;

  • The advantage of inline function is that it can reduce the overhead of function calling and improve the execution efficiency of the program;
  • The disadvantage of inline functions is that it will increase the size of the code and reduce the performance of the program;

Therefore, when the compiler decides whether to inline an "inline function", it will weigh the performance improvement brought by inlining and the overhead of code size increase;


3. It is up to the compiler to decide whether to inline


It is up to the compiler to decide whether to inline: in the C++ language, the inline keyword is just a suggestion to the compiler, and the compiler can freely decide whether to inline the function according to its own optimization strategy ;

Add the inline keyword before the declaration and definition of ordinary functions ,

  • It just recommends that the C++ compiler inline the function, and does not force the compiler to inline the function,
  • If the compiler decides not to inline the function, the inline function acts like a normal function;

Whether the final inline function is successfully inlined is determined by the compiler;





2. Comparison of inline functions and macro code fragments




1. Inline functions


The essence of "inline function" is a function, which is a special function;


"Inline functions" have the characteristics of ordinary functions, namely:

  • When defined, there are function name, parameter list, return value type, and function body;
  • There are parameter checks and return value type checks during execution;

The inline keyword of an inline function just requests the C++ compiler to inline the function. Whether C++ agrees or not depends on the optimization strategy of the C++ compiler, which may or may not be agreed;

If the C++ compiler does not agree to the inline request, the inline function is an ordinary function and is called as an ordinary function;


2. Macro code fragment


"Macro code fragment" is essentially a macro definition;

The macro code fragment is processed by the preprocessor, and the operation performed is simple text replacement;

The macro code fragment is not a function, it is not as powerful as a function, and the type checking of parameters and return values ​​will not be performed during execution, and there is no compilation process;

Guess you like

Origin blog.csdn.net/han1202012/article/details/132644064