In the C language, we use the time to perform this function by means of a macro definition compiler optimization techniques to reduce the program, then in C ++, there is no same or better technical realization of it? The answer is yes, that is an inline function. One technique inline function as the compiler optimization means, in reducing the running time is very useful.
1. What is the inline functions
2. Why should I use inline functions
3. The advantages and disadvantages of inline function
4. When to use inline functions
These four aspects inline functions introduced.
What is the inline functions
Inline function is one of the enhanced features of C ++, the program used to reduce the running time. When the compiler inline functions received instructions to take place within the Union: the compiler will use the function definition body to replace the function call statement, this alternative behavior occurs at compile time rather than run the program phase.
It is noteworthy that, inline function inlining is only suggestion to the compiler, the compiler if you think it was taken in line with the recommendations depends on the function inline favorable conditions. How great body of the function, the compiler will ignore the inline function declaration, and the inline function as an ordinary function processing.
How to make function inlining
When you define a function, in front of the function keyword "inline" statement function, you can make the function is called inline function declaration.
inline int add (int a, int b)
return (a + b);
int add (int a, int b);
inline int A :: add (int a, int b)
return (a + b);
Why should I use inline functions
Sometimes we'll write some of the features of the specific function, the function body of these functions is not, contain very little execution statement. For example, in calculating the prime numbers from 1 to 1000 or less, we often use the square root operation makes narrow, then we will write a function:
int root (int n)
return (int) sqrt ((float) n);
The function then find within our range of primes can be written.
int prime (int n)
for (i = 2; i < = root (n); i ++)
if (n% i == 0)
Of course, the root function in the loop is not a wise choice, but imagine, within the context of a program must frequently invoke a similar root function, which calls the function of expenses will be much: When faced with when an ordinary function call instruction, the program will save the current function of the execution site, the function of local variables and address of the function onto the stack, and then calls the new function will soon be loaded into memory, which is to be subjected to copy parameter values jump the memory address of the function to the calling function of position, execute the function code, the function return values stored procedure, before the function is executed when exhausted, and then get the calling, go back to perform that function, run time overhead is simply too much.
C ++ inline functions provide an alternative function call program through inline declaration, the compiler replaces the function call first statement uses the function itself statement in the function call, and then compile the code after the replacement. Therefore, an inline function, the compiler does not need to jump to another memory address to perform a function call, no need to retain the data field function calls.
Advantages and disadvantages of inline function
These are summarized by the following advantages and disadvantages you will probably understand why you want to use more inline function:
1. It is by avoiding the overhead of a function call to improve the speed of your program.
2. When the function call occurs, it saves variable popped, push overhead.
3. It avoids the overhead of a function is executed after return to the original site.
4. To declare a function inline, you can put on the function definition in the header file.
1. Because the spreading code, inline function increases the volume of the executable program.
Expand 2.C ++ inline functions is compiled stage, which means that if you change the inline function took place, then you need to recompile the code.
3. When you put inline functions in header files, it will make your header information increases, but users do not care about these header files.
4. Sometimes inline function is not favored, such as embedded systems, embedded systems storage constraints may not allow a large volume of executable programs.
When to use inline functions
When programming required, each function can be declared as inline. Here are some tips:
1. When the program execution performance is required, then use it inline functions.
2. When you want to define a macro function, use it decisively now inline functions.
3. inside the class defined function defaults declared inline function, which is conducive to the class implementation details hidden.
1. inline declaration is only a suggestion to the compiler, the compiler whether to adopt the measures inline by the compiler to decide. Even in the compilation phase or link phase, some do not declare inline functions The compiler will also inline expansion it.
2. inlining compiler code looks like copy and paste, which is very different from preprocessor macros: macros are mandatory inline expansion, may be contaminated with all namespace code, the program will be debugging difficult.
3. All the functions defined in the class are declared as inline functions default, all we do not have to explicitly declare inline.
4. inline virtual functions are not allowed.
5. Although the template function put in a header file, but they are not necessarily inline. (Not to say that the function is defined in the header file are inline functions).