C + + Inline

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. Unlike a non-inline function, the inline function must be defined in each text file that invokes the function. Of course, for different files of the same program, if the inline function appears, its 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 while in draw. C Middle finger another thing. If the 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. Include the header file in each file that invokes the inline function. 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 unintended mismatches during the lifetime 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 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 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 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? The
inline is at the expense of code bloat (replication), which simply eliminates the overhead of function calls and thus increases the function's
execution efficiency. If the time of executing the code in the function body is larger than the cost of the function call, then the efficiency of the
receives very little. On the other hand, every call to the inline function copies the code, which increases the total code size of the program, and the
consumes more memory space. Inline is not appropriate for the following situations:
(1) If the code in the function body is longer, using inline will result in a higher memory consumption cost.
(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 the
class are easily misunderstood to be more efficient for inline use. Be aware that constructors and destructors
functions may hide behaviors such as "secretly" executing constructors and destructors for base classes or member objects.
so do not arbitrarily place the definition of constructors and destructors in class declarations.
A good compiler will automatically cancel the unworthy inline based on the body of the function's definition (this further explains
that inline should not appear in the declaration of the function).

The C + + language supports function inline, which is designed to improve the efficiency (speed) of function execution.
In C programs, you can use macro code to improve execution efficiency. The macro code itself is not a function, but it is used like a function.
The preprocessor replaces the function call with the way of copying the macro code, eliminating the argument stack, generating the call invocation of assembly language,
The process of returning parameters, performing a return, and so on, improves speed.

The biggest disadvantage of using macro code is error-prone, and the preprocessor often produces unintended marginal effects when copying macro code.

For C + +, there is another drawback to using macro code: You cannot manipulate private data members of a class.

Let's take a look at how C + + 's "inline function" works.
For any inline function, the compiler puts the declaration of the function in the symbol table (including first name, parameter type, return value type).
If the compiler does not find an inline function error, then the code of the function is also placed in the symbol table.
When an inline function is called, the compiler first checks that the call is correct
(Type security checks, or automatic type conversions, of course, all functions are the same).
If correct, the code for the inline function replaces the function call directly, eliminating the overhead of the function call.

This process differs significantly from preprocessing because the preprocessor cannot perform type safety checks or automate type conversions.
If the inline function is a member function, the address of the object (this) is placed in the appropriate place, which is not the preprocessor can do.

The function-inline mechanism of C + + language not only has the efficiency of macro code, but also increases the security, and can manipulate the data members of the class freely.
So in a C + + program, you should replace all macro code with an inline function, and assert assert is probably the only exception.
An assert is a macro that works only in the debug version, and it is used to check what should not happen.
In order not to cause differences in the debug version and release version of the program, assert should not produce any side effects.
If the assert is a function, because the function call will cause memory, code changes, it will cause the debug version and release version of the difference.
So assert is not a function, but a macro.

C + + Inline

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.