inline function

Convert inline function

portal

1. What is an inline function? 
inline (be careful, not online), translated as "inline" or "inline". Meaning: When the compiler finds that a certain piece of code is calling an inline function, it does not call the function, but inserts the entire code of the function into the current position. The advantage of this is that the process of calling is omitted and the program runs faster. (The calling process of the function always takes more time due to the operations such as pushing the parameters into the stack as mentioned above). The disadvantage of this is that whenever the code calls an inline function, it needs to insert a piece of code of the function directly at the calling place, so the size of the program will increase. Take the phenomenon of life as an analogy, just like the TV is broken, you will find it slow to find a repairman on the phone, so you simply raise a repairman at home. That's fast, of course, but it's going to take up a lot of space for a repairman to live in your house. The inline function is not necessary, it is just a decoration for speed. To modify a function as inline type, use the following format: 
Inline function declaration or definition is 
a simple sentence, add an inline modifier before the function declaration or definition. 
inline int max(int ​​a, int b) 

   return (a>b)? a : b; 
}

The essence of inline functions is that they save time but consume space.

Second, the rules of the inline function

(1) A function can call itself, which is called recursive call (mentioned later). Functions with recursive calls cannot be set to inline;

(2) Complex flow control statements are used: loop statements and switch statements cannot be set to inline;

(3) Due to the characteristics of inline increasing the volume, it is recommended that the code in the inline function should be very short. Preferably no more than 5 lines.

(4), inline is only used as a "request". Under certain circumstances, the compiler will ignore the inline keyword and force the function to become an ordinary function. When this happens, the compiler will issue a warning message.

(5) Before you call an inline function, the function must be declared or defined as inline. If it is declared as a normal function in the front, and is defined as an inline function after the calling code, the program can pass compiles, but the function does not implement inline. For example, the following code snippet: 
//The function is not declared as inline at the beginning: 
void foo(); 
//Then there is code to call it: 
foo(); 
//The function is defined as inline after the call: 
inline void foo () 

   ... 

code is that the foo() function ends up not implementing inline;

(6) For the convenience of debugging, when the program is in the debugging stage, all inline functions are not implemented.

3. Pay attention to the following issues when using inline functions:

(1) An inline function defined in one file cannot be used in another file. They are usually shared in header files. 
(2) The inline function should be concise, with only a few statements. If there are many statements, it is not suitable to be defined as an inline function. 
(3) In the body of an inline function, there cannot be a loop statement, an if statement or a switch statement. Otherwise, even if there is an inline keyword in the function definition, the compiler will treat the function as a non-inline function. 
(4) Inline functions are declared before the function is called. The keyword inline must be placed with the body of the function definition to make the function inline, just putting inline before the function declaration has no effect.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325914146&siteId=291194637