inline functions in C + +

Source: Internet
Author: User

inline functions: (1inline functions define and function: When a function is declared as inline, the function becomes an inline function. The inline function is usually the "inline" expansion of each call point in the program. From the definition, the inline function is not the same as the general function, the general function call is required to call the cost (such as the stack into the stack, etc.), the inline function from the definition is more like a macro, but not the same as the macro. The function of the inline function is mainly to use in some short and very frequent functions, in order to reduce the cost of function calls, in order to avoid the use of macros (cIn + +, macros are not recommended). such as inline function inlineintFuncintx) {returnX*x;} At the time of the call Cout<<func (x) <<Endl, which will be expanded at compile time to: cout<< (x*x) <<Endl; (2the differences and advantages of the inline function relative to macro: from the above analysis, it can be seen that the inline function in the form of expression and macro very similar. But the difference between an inline function and a macro is obvious. A macro is a mechanical substitution that occurs during preprocessing, and the inline is done at compile time. An inline function is a real function, but when called, it does not invoke overhead and is expanded like a macro. Inline functions perform parameter matching checks, which have good advantages over macros with parameters, and avoid some of the problems of dealing with macros. (3how to use inline functions and disallow inline: to make a function known as an inline function, there are two methods: one is to add the function to the inline keyword, and one to define the function in the Description section of the class, which is inline by default. To prevent the compiler from being inline, you can use the#pragmaAuto_inline Compile instructions or change compilation parameters.(4) Inline function considerations: (1will inline functions be expanded inline? The answer is in the negative. For inline functions, the program simply provides an "inline recommendation," which suggests that the compiler should expand the function inline, but whether it is actually inline is determined by the compiler, and for functions that are too large for the function, the compiler is generally not inline, even if it is made into an inline function. (2Inside the inline function, the loop statement and the switch statement (if or switch) are not allowed. Inline functions have loops and switches inside, and there is no error, but the compiler will treat it as a non-inline function. (3The keyword inline must be placed with the function definition body in order for the function to be truly inline, and only the inline is placed in front of the function declaration. Because inline is a keyword for implementation, it is not a keyword for declaration. The declaration of an inline function does not require the inline keyword, and the definition of the inline function must be inline (in addition to the default inline function in the definition section of the Class), although many of the book declaration definitions are added, be aware of the differences between declarations and definitions. (4an inline function defined in one file cannot be used in another file. They are usually shared in the header file. (5the definition of an inline function must precede the first call. Note that this is before the definition, not just before the declaration. For a normal function, it can be declared before the call, the call code is defined (Implementation function), but inline function to implement inline, you must first define the call, otherwise the compiler will call in the definition of the inline function as a normal function call. (6Note : The above-mentioned inline considerations, in the programming to pay attention to, because the above considerations do not follow a lot and will not cause a compilation error, it will only lead to write the function of inline is not an inline function, and the intended purpose is not the same. So many can not use the program example to explain whether the compiler is in accordance with inline or non-inline calls, perhaps the analysis of the assembly code to see, but the level is limited, there is no more analysis. (5Some reference articles about inline: http://www.cppblog.com/fwxjj/archive/2007/04/20/22352.html Inline functions for C + +http//some summaries of the www.yuanma.org/data/2007/1211/article_2916.htm inline functionhttp//blog.csdn.net/gaoxiaowei/archive/2008/09/02/2866773.aspx C + + basics-inline functionsCopyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The keyword inline must be placed with the function definition body in order for the function to be inline, and only the inline is placed before the function declaration .

So, inline is a "keyword for implementation", not a "keyword for declaration." In general, the user can read the declaration of a function, but cannot see the definition of the function. Although the inline keyword is prepended to the declaration and definition of inline functions in most textbooks, I don't think inline should appear in the declaration of a function. While this detail does not affect function functionality, it embodies a fundamental tenet of high quality C++/C program design: declarations and definitions are not to be confused, and the user does not need to know whether the function needs to be inline.

member functions defined in the class declaration will automatically become inline functions

For example

Class A

{

Public:void Foo (int x, int y) {}//automatically becomes an inline function

}

It is not a good programming style to place the definition of a member function in a class declaration, although it can bring about convenience in writing, but it should be changed to the following example:

Header file

Class A

{

Public

void Foo (int x, int y);

}

Definition file

inline void A::foo (int x, int y) {}

Using inline with caution

Inline can improve the efficiency of function execution, why not all functions are defined as inline functions? If all functions are inline functions, is it useful to "inline" the keyword? Inline is at the expense of code bloat (replication), which simply eliminates the overhead of function calls and thus increases the efficiency of function execution. If the time of executing the code in the function body is greater than the cost of the function call, then the efficiency will be harvested very little. On the other hand, the invocation of each inline function is to copy code, which increases the total code size of the program and consumes more memory space.

It is not advisable to use inline in the following situations:

(1) If the code in the function body is longer, using inline will result in a high cost of memory consumption.

(2) If there is a loop in the function body, the time to execute the function body code is greater than the cost of the function call. The constructors and destructors of a class are easily misunderstood to be more efficient for inline use. Be aware that constructors and destructors may hide behaviors such as "secretly" executing constructors and destructors for base or member objects. So don't arbitrarily place the definitions of constructors and destructors in class declarations. A good compiler will automatically cancel the unworthy inline based on the body of the function definition (this further illustrates that inline should not appear in the declaration of the function).

inline functions in C + +

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.