Suddenly see C++primer, for a vector loop, the phrase: (example code I=v.begin () is not very normative, although not wrong, Sir please look at)
[CPP] view plain copy for (int i=v.begin (); I<v.size (); i++) {...} The call to size () is actually inline. Think of the past seems to see "High quality C/s + + programming," mentioned, in the loop, you can use variables to save the value of V.size (), to reduce the call cost per cycle. So decided a search, incidentally summed up.
1, inline of the derivation
Consider the following min () function (examples from C++primer Third edition, page 303)
[CPP] view plain copy int min (int v1, int v2) {return (V1 < v2 << v1:v2); The benefits of defining a function for such a small operation are:
A. If a section of code contains a call to min (), it is much more readable to read such code and explain its meaning than to read an instance of a conditional operator.
B. It's much easier to change a localized implementation than to change the 300 occurrences in an application
C. Semantics are unified, and each test guarantees the same way to achieve
D. Functions can be reused without rewriting the code for other applications
However, there is a serious drawback to writing min () as a function: calling a function is much slower than directly calculating the condition operator. How does that take into account the above advantages and efficiency. C + + provides solutions for inline (inline) functions
2, the principle of inline: code substitution
At compile time, the compiler replaces the invocation expression of the inline function that appears in the program with the function body of the inline function.
For example, if a function is specified as the inline function, it will be expanded inline at each call point in the program, for example
[CPP] view plain copy int minVal2 = min (i, j); is expanded at compile time to
[CPP] view plain copy int minVal2 = i < J << I:j; The extra execution cost of writing min () into a function is eliminated.
3, the use of inline
Let a function become an inline function, implicitly define the function in the class, and explicitly precede the function with the inline keyword description.
4, the use of inline some points of attention
A. From the principle of inline, we can see that the principle of inline, is the use of space in exchange for Time , is based on the Code expansion (replication) as the cost, only omitted the cost of function calls, so as to improve the efficiency of function execution. If the time of executing the code in the function body is greater than the overhead of the function call, then there will be little efficiency gains. Therefore, if the function body code is too long or the function weight has circular statements, if statements or switch statements or recursion, it is not appropriate to use inline
B. Keyword inline must be placed with the function definition body to make the function inline, and it does not have any effect until the inline is placed in front of the function declaration. Must be declared before an inline function call. An example of high quality C + + programming. [CPP] view plain copy inline void Foo (int x, int y); Inline only with function declarations the void Foo (int x, int y) {...} The above code cannot be an inline function, and the following can be
[CPP] view plain copy void Foo (int x, int y); inline void Foo (int x, int y)//inline with the function definition body {...} So, inline is a "keyword for implementation", not a "keyword for declaration". For the above example, Lin Rui also suggests adding inline to the definition, rather than the declaration and definition, because it embodies a fundamental principle of high quality C++/C programming style: Declaration and definition are not to be confused.
C.inline is just a recommendation for the compiler, and the compiler can choose to ignore the recommendation. In other words, even if the inline is really written, and without any errors, the compiler will automatically optimize. So when recursion, loops, or too much code appears in the inline, the compiler automatically ignores the inline declaration, as well as the normal function call.
In summary:
The landlord feels that the inline can be understood as a function-specific macro in C + +, an improvement on the function macros of C. For Chang, C + + provides const substitution, whereas for function macros, C + + provides a solution that is inline. In C, we all know the advantages of the macro, the compiler through the duplication of macro code, eliminating the parameters of the stack, generate assembly call calls, return parameters and other operations, although there are some security risks, but in terms of efficiency, it is very desirable.
However, there are a number of shortcomings in the function macro, mainly the following:
A. When copying code, it is easy to have an unexpected marginal effect, such as the classic [CPP] view plain copy #define MAX (A, B) (a) > (b)? (a): (b) in the execution of the statement: [CPP] view plain Copy result = MAX (i, J) + 2; , it will be interpreted as
[CPP] view Plain