Inline Usage Analysis in c ++

Source: Internet
Author: User

In the function declaration or definition, add the keyword inline before the function return type to specify min () as inline.
Inline int min (int first, int secend ){/****/};
The inline function must be visible to the compiler so that it can expand the function within the call point. Non-inline
Different functions, 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 a program consisting of two files: compute. C and draw. C, a programmer cannot define such a min () function. It specifies one thing in compute. C,
Another thing in draw. C. If the two definitions are different, the program will have undefined behavior:

To ensure that this will not happen, we recommend that you put the inline function definition in the header file. In each
File contains this header file. This method ensures that each inline function has only one definition, and the programmer does not need to copy the code, and
It is impossible to cause unintentional mismatch in the life of the program.
(2) programming style of inline functions (from the high quality C ++/C Programming Guide)
The keyword inline must be put together with the function definition body to make the function inline. It does not work unless you put inline before the function declaration.
The following function Foo cannot be an inline function:

Copy codeThe Code is as follows: inline void Foo (int x, int y); // inline is only put together with the function declaration
Int main (){

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

The following function Foo becomes an inline function:Copy codeCode: void Foo (int x, int y)
Int main (){

}
Inline void Foo (int x, int y) // put inline together with the function definition body
{
}

Therefore, inline is a "keyword used for implementation", rather than a "keyword used for Declaration ".
Generally, you can read the declaration of a function, but cannot see the definition of a function. Although in most textbooks
The inline keyword is added before the declaration and definition body of a function, but I don't think inline should appear in the function.
. Although this detail does not affect function functions, it reflects the high quality C ++/C programming style.
A basic principle of: Declarations and definitions cannot be confused. users do not need to know whether a function is required or not.
Inline.
The member functions defined in the class declaration will automatically become inline functions, for exampleCopy codeThe Code is as follows: class
{
Public:
Void Foo (int x, int y) {}// automatically becomes an inline function
}

Putting the definition body of a member function in the class declaration makes writing easy, but it is not a good programming.
Style, which should be changed:Copy codeThe Code is as follows: // header file
Class
{
Public:
Void Foo (int x, int y );
}
// Definition file
Inline void A: Foo (int x, int y)
{
}

Use inline with caution
Inner join can improve the execution efficiency of functions. Why not define all functions as inline functions?
If all functions are inline functions, do you still need the keyword "inline?
Inline is at the cost of code expansion (replication). It only saves the overhead of function calls and improves the Function
Execution efficiency. If the execution time of the Code in the function body is greater than the overhead of the function call, it is more efficient.
Rarely. On the other hand, every call to an inline function must copy the code, which will increase the total amount of code in the program,
More memory is consumed. Inline is not recommended in the following cases:
(1) If the code in the function body is long, using inline will cause high memory consumption.
(2) If there is a loop in the function body, the execution time of the Code in the function body is longer than the overhead of the function call.
Class constructor and destructor are easy to misunderstand that using inline is more effective. Be careful with constructors and destructor
Functions may hide some behaviors. For example, they secretly execute constructors and destructor of base classes or member objects.
Therefore, do not place the constructor and destructor definitions in the class declaration.
A good compiler will automatically cancel inlining that is not worthwhile based on the definition body of the function (this further description
The inline should not appear in the declaration of the function ).

Related Article

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.