1. Reasons for introducing the inline keyword
In C/C++, to solve the problem of some frequently called small functions consuming a large amount of stack space (stack memory), theinlinemodifier was specially introduced, indicating inline functions.
Stack space refers to the memory space that holds the program's local data (that is, data within functions).
Under the system, stack space is limited. If it is used frequently and in large amounts, it can cause program errors due to insufficient stack space. For example, the final result of a function's infinite recursive calls is the exhaustion of stack memory space.
Let's look at an example:
Example
The above example is the standard usage of inline functions. Theinlinebenefits brought by the modifier are not visible on the surface. In fact, internally, at everyforcall inside the loopdbtest(i)is replaced with(i%2>0)?"odd":"even", thus avoiding the consumption caused by repeatedly allocating stack memory for frequent function calls.
2. Limitations of inline usage
inlineIts use is somewhat limited. inline is only suitable for functions with simple code in the function body. It cannot contain complex structural control statements such as while or switch, and the inline function itself cannot be a directly recursive function (that is, a function that calls itself internally).
3. inline is only a suggestion to the compiler
inlineThe inline function is only a suggestion to the compiler, so whether it can actually be inlined in the end depends on the compiler. If it thinks the function is not complex and can be expanded at the call site, it will truly inline it. It is not the case that declaring inline will make it inline; declaring inline is just a suggestion.
4. It is recommended to put inline function definitions in header files
Secondly, because inline functions need to be expanded at the call site, thecompiler must be able to see the definition of the inline function everywhere. Otherwise, it becomes a call to a non-inline function. Therefore, this requires that every file that calls the inline function contains thedefinition of the inline function。
. Therefore, putting thedefinition of the inline functionin theheader fileis appropriate, saving you the trouble of implementing it once for each file.
The declaration and definition must be consistent: If the inline function is implemented once in each file, it is best to ensure that each definition is the same; otherwise, it will cause undefined behavior. If the definitions are not the same in each file, which one the compiler expands depends on the specific compiler. Therefore, it is best to put theinline function definitionin theheader file.
5. Member functions in classes and inline
Definedin a classMember functionsby default areinline. If the function definition is given inside the class when the class is defined, that is of course best. If the member function definition is not given in the class, but you want to inline the function, you need to addinline, otherwise it is considered not inline.
Although putting the member function definition body in the class declaration brings convenience in writing, it is not a good programming style. The above example should be changed to:
6. inline is a "keyword for implementation"
The keywordinlinemust be placed together with the function definition body to make the function inline. Merely puttinginlinein front of the function declaration has no effect.
A function of the following styleFoocannot become an inline function:
inline void Foo(int x, int y); // inline 仅与函数声明放在一起
void Foo(int x, int y){}
While a function of the following styleFoobecomes an inline function:
void Foo(int x, int y);
inline void Foo(int x, int y) {} // inline 与函数定义体放在一起
Therefore, inline is a "keyword for implementation", not a "keyword for declaration." Generally, users can read the function declaration, but cannot see the function definition. Although in most textbooks the inline keyword is added before both the declaration and definition body of inline functions, I thinkinline should not appear in function declarations. Although this detail does not affect the function's functionality, it reflects a basic principle of high-quality C++/C programming style:Declarations and definitions should not be confused. Users have no need to know, and should not know, whether a function needs to be inline.
7. Use inline with caution
Inline can improve the execution efficiency of functions. Why not define all functions as inline functions? If all functions were inline, would the "inline" keyword still be necessary?
Inline comes at the cost ofcode bloat (copying), and it only saves the overhead of function calls, thereby improving the execution efficiency of functions.
If the time to execute the code in the function body is large compared to the overhead of the function call, then the efficiency gain will be small. On the other hand, every call to an inline function requires copying the code, which will increase the total code size of the program and consume more memory space.
Inline should not be used in the following situations:
(1) If the code in the function body isrelatively long, using inline will lead tohigher memory consumption costs。
(2) If there is aloopin the function body, then the time to execute the code in the function body will be greater than the overhead of the function call. Class constructors and destructors can easily make people mistakenly think that using inline is more effective. Be careful thatconstructors and destructors may hide some behaviors, such as "secretly" executing thebase class or member objectconstructors and destructors. Therefore,do not casually put the definition bodies of constructors and destructors in the class declaration. A good compiler will automatically cancel inline that is not worthwhile based on the function's definition body (this further illustrates that inline should not appear in function declarations).
8. Summary
Inline functions are not a magic bullet for enhancing performance. Only whenthe function is very shortcan it achieve the effect we want; however, if the function is not very short and is called in many places, it will increase the size of the executable.
The most annoying thingis whenthe compiler refuses to inline. In old implementations, the results were quite unsatisfactory. Although new implementations have greatly improved, they are still not perfect. Some compilers are smart enough to point out which functions can be inlined and which cannot, but most compilers are not that smart, so this requires our experience to judge.If inline functions cannot enhance performance, avoid using them!
Original link: https://www.cnblogs.com/fnlingnzb-learner/p/6423917.html