C + + inline function inline

Source: Internet
Author: User
Tags class definition function definition

http://blog.csdn.net/u011327981/article/details/50601800

1. Inline functions

In C + + we typically define the following function to find the maximum value of two integers:

Copy CodeThe code is as follows:
int max (int a, int b)
{
Return a > B? A:B;
}

The benefits of defining a function for such a small operation are:

① reading and understanding the invocation of the function max is much easier than reading an equivalent conditional expression and explaining its meaning.

② If you need to make any changes, it's much easier to modify the function than to find and modify each equivalent expression.

③ uses functions to ensure uniform behavior, and each test is guaranteed to be implemented in the same way

The ④ function can be reused without rewriting the code for other applications

While there are so many benefits, writing a function has a potential disadvantage: calling a function is much slower than solving an equivalent expression. On most machines, the calling function has to do a lot of work: Save the Register before the call, restore it on return, copy the arguments, and the program must move to a new location to execute

Inline functions are supported in C + + to improve the execution efficiency of functions, with the keyword inline placed in the function definition (note the definition rather than the declaration, continue below) to specify the function as an inline function, and the inline function is usually "inline" on each call point in the program. Suppose we define max as an inline function:

Copy CodeThe code is as follows:
inline int max (int a, int b)
{
Return a > B? A:B;
}

Then call: Cout<<max (A, b) <<endl;


At compile time, expand to: cout<< (a > B a:b) <<endl;

This eliminates the additional execution overhead of writing Max as a function

2. inline functions and macros

Whether it's the "Prefer Consts,enums,and inlines to #defines" clause in effective C + + or "replace macros with functions inline" in the high-quality Programming Guide--C++/C language, macros are basically obsolete in C + + , in the book "High-quality Programming Guide--C++/C language," this explains:

3. Put inline functions into the header file

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:

Copy CodeThe code is as follows:
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:

Copy CodeThe code is as follows:
void Foo (int x, int y);
inline void Foo (int x, int y)//inline is placed with function definition body
{
...
}

So, the C + + inline function 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.

The member functions defined in the class declaration will automatically become inline functions, for example:

Copy CodeThe code is as follows:
Class A
{
Public
void Foo (int x, int y) {...} Automatically becomes an inline function
}

But whether the compiler will actually inline it depends on how the Foo function is defined

Inline functions should be defined in the header file, which differs from other functions. The compiler must be able to find the definition of the inline function in order to replace the calling function with the function code, which is not sufficient for a function declaration in the header file, when the code for the expansion function is inline with the call point.

Of course the inline function definition can also be placed in the source file, but at this time only the definition of the source file can be used, and must be a copy of each source file definition (that is, each source file definition must be identical), of course, even in the header file, but also a copy of each definition, It's just a compiler doing this for you. But rather than being placed in a source file, placing it in a header file ensures that the calling function is the same definition, and that the function definition can be found at the call point to complete the inline (replace).

But you'll be surprised how many times you repeat the definition, and you don't create a link error?

Let's look at an example:

A.h:

Copy CodeThe code is as follows:
Class A
{
Public
A (int a, int b): A (a), B (b) {}
int Max ();

Private
int A;
int b;
};

A.cpp:

Copy CodeThe code is as follows:
#include "A.h"

inline int A::max ()
{
Return a > B? A:B;
}

Main.cpp:

Copy CodeThe code is as follows:
#include <iostream>
#include "A.h"
using namespace Std;

inline int A::max ()
{
Return a > B? A:B;
}

int main ()
{
A (3, 5);
Cout<<a.max () <<endl;
return 0;
}

All normal compilation, output results: 5


If you do not define the max inline function in Main.cpp, then a link error will occur:

Error lnk2001:unresolved external symbol "Public:int __thiscall a::max (void)" ([email protected]@ @QAEHXZ) main.obj
The definition of a function cannot be found, so inline functions can be defined more than once in a program, as long as the definition of the inline function appears only once in a source file, and in all source files, the definition must be identical.

When you add or modify an inline function in a header file, all source files that use the header file must be recompiled.

4. Be cautious with inline

Although inline has its advantages, but also to use caution, the following excerpt from the "High quality Programming Guide--C++/C language":

And in the Google C + + coding specification is defined more clearly and more detailed:

Inline functions:

Tip: Define an inline function only if the function is only 10 lines or less.

Definition: When a function is declared as an inline function, the compiler expands it, rather than invoking it as a normal function call mechanism.
Advantage: When the function body is relatively small, inline the function can make the target code more efficient. For functions with access functions and other functions that are short, performance-critical, the use of inline is encouraged.
Cons: Abusing the inline will cause the program to slow down. Inline may cause the target code to increase or decrease, depending on the size of the inline function. Inline very short access functions usually reduce the size of the code, but a fairly large number of inline functions will dramatically increase the size of the code. Modern processors because of the better use of the instruction cache, small code tends to execute faster.
Conclusion: a more reasonable rule of thumb is not to inline more than 10 rows of functions. The destructor is treated with caution, and destructors tend to look longer than their surface because there are hidden members and base class destructors called!
Another practical rule of thumb: inline functions that contain loops or switch statements are often not worth the candle (unless, in most cases, these loops or switch statements are never executed).
It is important that some functions are not necessarily inline by the compiler even if they are declared inline; For example, virtual functions and recursive functions are not normally inline. In general, recursive functions should not be declared as inline functions. (The expansion of the recursive call stack is not as simple as looping, such as recursive layers may be unknown at compile time, and most compilers do not support inline recursive functions). The main reason for the virtual function inline is to put its function body within the class definition, for convenience, or as a document to describe its behavior, such as a short access function.

-inl.h file:


TIP: The definition of a complex inline function should be placed in a header file with the suffix named-inl.h.


The definition of an inline function must be placed in the header file in order for the compiler to expand the definition inline within the call point. However, the implementation code should theoretically be placed in the. cc file, and we do not want to have too much implementation code in the. h file, unless there is a clear advantage in readability and performance.

If the definition of an inline function is relatively short, the logic is simpler, and the implementation code does not have any problems with the. h file. For example, the implementation of an access function should, of course, be placed within the class definition. For the convenience of writers and callers, more complex inline functions can also be placed in the. h file, and if you think this will make the header file cumbersome, you can also extract it into a separate-inl.h. This separates the implementation from the class definition and includes the corresponding-inl.h when needed.

C + + inline function 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.