inline inline function
Everyone must have used function calls . A function only comes to life when it is called by another function, and the corresponding memory space is prepared for it. After the call is completed, it is cleaned and released.
We can see that each function call will bring some time and space costs. One of the functions of custom functions is to improve the reusability of code. They can be called at any time when needed and improve development efficiency. So, for a function that does not have much code and is called frequently, we should think carefully about whether it is cost-effective to do so.
Fortunately, C++ has helped us take this problem into account and provided us with an inline mechanism, that is, we still use custom functions, but during compilation, the function code is inserted into the function call, thus eliminating the need for a function call. Serial procedures, like ordinary sequentially executed code, solve this problem!
Then the usage is very simple. You only need to add the keyword inline declaration in front of the function definition, such as the following code:
#include<iostream>usingnamespacestd;inlineintMax(inta,intb){returna>b?a:b;}intmain(){cout<<Max(3,5)<<endl;cout<<Max(7,9)< <endl;return0;}
It is worth mentioning that the definition of an inline function must appear before the call, so that the compiler can understand the context and perform code replacement during compilation. In addition, the inline function is similar to the register variable . It is just a request we make to the compiler. Whether it will be actually inlined in the end is left to the compiler to choose according to the situation.