C + + learning path: about inline functions

Source: Internet
Author: User

(i) inline function (excerpt from the third edition of C + + primer)

adding the keyword inline to the function declaration or definition in the function return type specifies the 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 point of the call. and non-inline
function is different, the inline function must be defined in each text file that invokes the function. Of course, for the same program
Different files, and if the inline function appears, the definition must be the same. For compute of two files. C and DRAW.C program, programmers can not define such a min () function, it is in the compute. C Middle finger one thing,
and in draw. C Middle finger another thing. If the two definitions are not identical, 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 invocation of the inline function, the
The header file is included in the file. This approach guarantees that there is only one definition for each inline function, and the programmer does not need to copy the code, and
It is not possible to cause unintentional mismatches in the life of the program.

(ii) The programming style of the inline function (excerpted from the high Quality C++/C Programming Guide)

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 .

The following style of function Foo cannot be an inline function:
inline void Foo (int x, int y); Inline is only placed with function declarations
void Foo (int x, int y)
{
}
The following style of function foo becomes an inline function:
void Foo (int x, int y);
inline void Foo (int x, int y)//inline is placed with function definition body
{
}
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 in most textbooks
The inline keyword is prepended to the declaration and definition body of the Union function, but I don't think inline should appear in the function
's declaration. Although this detail does not affect function functions, it embodies the high quality C++/C program design style
A basic principle: Declaration and definition can not be confused, the user does not need, and should not know whether the function needs
to inline.


member functions defined within a class declaration will automatically become inline functions , such as
Class A
{
Public
void Foo (int x, int y) {}//automatically becomes an inline function
}
Putting the definition of a member function in a class declaration can be convenient for writing, but not a good programming
Style, the above example should be changed to:
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, thus improving the function's
Execution efficiency. If the time to execute the function body code is greater than the cost of the function call, the efficiency
will be scarce. On the other hand, every call to the inline function will copy the code, which increases the total code size of the program.
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 wary of constructors and destructors
The function may hide some behavior, such as "secretly" executing the constructor and destructor of a base class or member object.
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 definition of the function (this further explains
Inline should not appear in the declaration of the function).

C + + learning path: about inline functions

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.