Analysis of the use of inline in c++

  • 2020-04-01 23:37:49
  • OfStack

Specify min () as inline by adding the keyword inline before the return type of the function in the function declaration or definition.
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. With the inline
Unlike the function, the inline function must be defined in each text file where the function is called. Of course, for the same program
The definition of the inline function must be the same if it appears. For a program consisting of two files compute.C and draw.c, the programmer cannot define such a min() function, which in compute.C refers to one thing,
A. draw b. draw c. draw d. draw If the two definitions are different, the program will have undefined behavior:

To ensure that this does not happen, it is recommended to place the definition of the inline function in the header file. On each call to the inline function
The header file is contained in the file. This method ensures 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 an unintended mismatch during the life of a program.
(2) inline function programming style (from high quality C++/C programming guide)
The keyword inline must be placed with the body of the function definition to make the function inline. Simply placing inline before the function declaration does not work.
The following style of function Foo cannot be inline:


inline void Foo(int x, int y); //Inline is placed only with the function declaration
int main() {

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

The following style of function Foo becomes inline:

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

}
inline void Foo(int x, int y) //Inline is placed with the body of the function definition
{
}

So inline is a "keyword for implementation", not a "keyword for declaration".
In general, the user can read the declaration of the function, but cannot see the definition of the function. Although in most textbooks
The inline keyword is added before the declaration and definition body of the linked function, but I don't think inline should appear in the function
In a statement. This detail does not affect the functionality of the function, but reflects the high quality C++/C programming style
A basic rule of thumb: declarations and definitions are not to be confused, and users do not need or should not know if functions are required
Inline.
A member function defined in a class declaration automatically becomes an inline function, for example

class A
{
public:
void Foo(int x, int y) { } //Automatically becomes an inline function
}

Placing the body of the definition of a member function in a class declaration is not good programming, although it makes writing easier
Style, the above example should be changed to:

//The header file
class A
{
public:
void Foo(int x, int y) ; 
}
//The definition file
inline void A::Foo(int x, int y)
{
}

Careful with inline
Inlining makes functions more efficient, so why not define all functions as inline?
If all functions are inline, do you still need the keyword "inline"?
Inlining is expensive at the expense of code bloat (replication) and only increases the overhead of function calls
Execution efficiency. If the time it takes to execute the code in the body of the function is more expensive than the function call, the efficiency is reduced
Gains are few. On the other hand, copying code for every call to an inline function increases the total amount of code in the program,
Consume more memory space. Inlining should not be used in the following situations:
(1) if the code in the function body is relatively long, using inlining will lead to high memory consumption.
(2) if there is a loop in the function body, the time to execute the code in the function body is more expensive than the function call.
Class constructors and destructors can be misinterpreted as being more efficient with inlining. Beware of constructors and destructors
Functions may hide behavior such as "stealthy" execution of constructors and destructors for base classes or member objects.
So don't just throw the body of a constructor or destructor into a class declaration.
A good compiler will automatically cancel undeserved inlining based on the body of the function definition
Inline should not appear in the declaration of the function.


Related articles: