C + + inline functions

Source: Internet
Author: User

Calling a function requires a certain amount of time and space overhead. C + + provides a way to improve efficiency by replacing function calls at compile-time with function bodies, similar to macro expansion in C. This function, which is directly embedded in the function body at the function call, is called an inline function (inline function), also known as an inline or built-in function.

The method of specifying an inline function is simple, just to add the inline keyword when defining a function.

Note: The inline keyword is added when the function is defined, not when the function is declared. Adding inline key while function declaration is not an error, but it has no effect

The inline keyword does not work at the function declaration:

The inline keyword should be put together with the function body:

void Swap (int&a,int&b), inline void Swap (INT&A,INT&B) {int temp = A;    A = b;    b = temp;}

The use of inline functions can effectively avoid the overhead of function calls, and program execution is more efficient. The disadvantage of using inline functions is that if the body of the function declared as an inline function is very large, the executable code of the compiler will become large.

In addition, if there is a loop or other complex control structure in the function body, it takes much longer to deal with these complex control structures than the time spent by the function call, so if such a function is declared as an inline function, it will make the compiled executable code grow longer.

Usually during the programming process, we declare some short functions that are frequently called as inline functions.

It should be stated that the inline declaration of a function is just a suggestion made by the programmer to compile the system, that is to say, it is a suggestion, not a prescriptive one. Not once specified as inline, the compiled system must do so. The compilation system will decide whether to do so depending on the situation.

A complete example:

#include <iostream>usingnamespace std;int Max (int,int,int);//function declaration, left side can also add Inlineint main () {int i=10, j=20, k=30, M ; M =max (I, J, K);cout<< "max=" <<M<<ENDL;RETURN0;} inline int max (int a,int b,int c)//define Max as inline function {if (b>a) a=b;if (c>a) A=c;return A;}

  

Operation Result:
Max=30

Since it is specified as a built-in function when defining a function, the compilation system uses the code of the Max function body instead of "Max (I,j, K)" When it encounters the function call "Max (I, J, K)" and replaces the argument with the actual parameter. Thus, the 6th line of the program "M=max (i, J, K);" is replaced by:

if (j>i) i=j;if (k>i) i=k;m=i;

  

C + + inline functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.