The usage analysis of inline in C + + _c language

Source: Internet
Author: User
Tags function definition

Specify min () as inline, before the function return type in the function declaration or definition, plus the keyword inline.
inline int min (int-A, int secend) {/****/};
The inline function must be visible to the compiler so that it can expand the function within the call point. and non-inline
function is different, the inline function must be defined in each text file that calls the function. Of course, for the same program
Different files, if the inline function appears, its definition must be the same. For compute from two files. C and DRAW.C programs, programmers cannot define such a min () function, which is in compute. C Middle finger one thing,
and in draw. C Middle finger another thing. If two definitions are not the same, the program will have undefined behavior:

To ensure that this does not happen, it is recommended that the definition of the inline function be placed in the header file. In each call to the inline function.
The header file is included in the file. This method guarantees that there is only one definition for each inline function, and that the programmer does not need to copy the code and
It is impossible to cause unintentional mismatches in the lifetime of a program.
(ii) Programming style of inline functions (excerpted from high quality C++/C Programming Guide)
Keyword inline must be placed with the function definition body in order for the function to be inline, and it will have no effect but to place the inline in front of the function declaration.
The following style of function Foo cannot be an inline function:

Copy Code code as follows:

inline void Foo (int x, int y); Inline only with function declarations
int main () {

}
void Foo (int x, int y)
{
}

And the following style of function foo becomes an inline function:
Copy Code code as follows:

void Foo (int x, int y)
int main () {

}
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".
Generally, users can read the declaration of a function, but do not see the definition of the function. Although in most textbooks
The Declaration of the function, the definition body is preceded by the inline keyword, but I think inline should not appear in the function
's statement. This detail does not affect function, but it embodies the design style of high quality C++/C program
A basic principle: Declaration and definition can not be confused, users do not need, and should not know whether the function needs
to inline.
The member functions defined in the class declaration will automatically become inline functions, such as
Copy Code code as follows:

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

Placing the definition of a member function in a class declaration while writing is convenient, it is not a good programming
Style, the above example should be changed to:
Copy Code code as follows:

Header file
Class A
{
Public
void Foo (int x, int y);
}
Definition file
inline void A::foo (int x, int y)
{
}

careful use of inline
Inline can improve the execution efficiency of functions, why not all functions are defined as inline functions?
If all of the functions are inline functions, is it also useful to have the keyword "inline"?
Inline is the cost of code expansion (replication), which simply eliminates the overhead of function calls, thereby increasing the
Execution efficiency. If the time of executing the code in the function body is greater than the overhead of the function call, then the efficiency
will be rare. On the other hand, each call to the inline function will copy the code, increasing the total code size of the program.
Consumes more memory space. The following conditions are not appropriate to use inline:
(1) If the code in the function body is longer, using inline will result in a higher cost of memory consumption.
(2) If there is a loop in the function body, the time to execute the code in the function body is greater than the cost of the function call.
The constructors and destructors of classes are easily misunderstood to be more efficient in using inline. Be careful with constructors and destructors
A function might hide some behavior, such as "secretly" executing the constructor and destructor of a base class or member object.
So don't casually place constructors and destructor definitions in class declarations.
A good compiler will automatically cancel the unworthy inline according to the definition of the function (this further explains
Inline should not appear in the declaration of a function.

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.