C ++ FAQ Lite [9] - Inline function (update)

zhaozj2021-02-08  246

[9] Inline function (Part of C FAQ Lite, Copyright © 1991-2001, Marshall Cline, Cline@parashift.com)

体,, nicrosoft @ sunistudio.com (East day production room, East day document)

FAQS in section [9]:

[9.1] What is the inline function? [9.2] How does the inline function have been compromise on security and speed? [9.3] Why should I use the inline function? Instead of the original Clear #define macro? [9.4] How do I tell the compiler to make the non-member function become inline functions? [9.5] How do I tell the compiler to make a member function become inline functions? [9.6] Is there any other way to tell the compiler to make the member function be inline? [9.7] Is the inline function guaranteed the execution performance?

[9.1] What is the inline function?

The inline function is a function that the code is inserted into the caller code string. Like #define macro, the inline function will improve the execution efficiency by avoiding the turnover called, especially it can be optimized by calling ("Process Integration").

[TOP | BOTTOM | Previous Section | Next Section]

[9.2] How does the inline function have been compromise on security and speed?

In C, you can get a "package structure" by setting a VOID * in the structure, in which case the VOID * pointer to the actual data is unknown for the user of the structure. Therefore, the user does not know how to explain the content of the VOID * pointer, but the access function can convert VOID * to an appropriate implicit type. This gives a form of a package. Unfortunately, this is the same type of security, and it will also add a cumbersome access to each domain across the structure. (If you allow direct access to the domain, you can understand any people who can directly access the VOID * pointer is necessary; this will make it difficult to change the underlying data structure). Although the function call overhead is small, it will be accumulated. The C class allows the function call to expand inline. This allows you to get direct access when getting the security of the package. In addition, the parameter type of the inline function is checked by the compiler, which is an improvement to the #define macro of C.

[TOP | BOTTOM | Previous Section | Next Section]

[9.3] Why should I use the inline function? Instead of the original Clear #define macro?

[Recently Rewrote The Sentence ON #Define Being Evil (on 7/00). Click Here To Go To The Next Faq in The "Chain" of Recent Changes

]

Because #define macro is harmful to four: crime # 1

, Crime # 2

, Crime # 3

, And crime # 4

.

And the #define macro is not that the inline function always makes a value of the parameters only once, thus avoiding a macro mistake of the sound. In other words, calling the intraline function and calling a regular function is equivalent, the difference is simply faster:

// Return

I absolute value macro

#define unsafe (i) / ((i)> = 0? (i): - (i))

// Return

I absolute inline function

INLINE INT SAFE (INT i) {RETURN I> = 0? i: -i;} int f (); void usercode (int x) {int ANS; ANS = Unsafe (x ); // Error! X is added twice

ANS = Unsafe (f ());

// Danger! f () is called twice

ANS = SAFE (x );

/ / Correct! X is added once

ANS = SAFE (f ());

/ / Correct! F () is called once

}

Different from the macro, the parameter type of the inline function is checked, and the necessary conversion is performed correctly. Macro is harmful; non-unused.

[TOP | BOTTOM | Previous Section | Next Section]

[9.4] How do I tell the compiler to make the non-member function become inline functions?

The declaration of the inline function looks very similar to the normal function:

Void F (INT I, CHAR C);

When you define an inline function, add the inline keyword before the function definition, and put the definition into the header file:

Inline Void F (INT I, CHAR C) {

// ...

}

Note: Place the definition between the function ({...}) is forced in the header file, unless the function is only used by a single .cpp file. In particular, if you put the definition of the inline function in the .cpp file and call it in other .cpp files, the connector will give the "Unresolved External" error.

[TOP | BOTTOM | Previous Section | Next Section]

[9.5] How do I tell the compiler to make a member function become inline functions?

Disclaimer The inline member function looks very similar to the ordinary function:

Class Fred {public: Void F (INT I, CHAR C);

But when you define the inline member function, add the inline keyword before the member function definition, and put the definition into the header file:

Inline void fred :: f (int I, char c) {

// ...

}

It is usually placed in the header file in the definition of the function (part of {...}). If you put the definition of the inline function in the .cpp file and call it in other .cpp files, the connector will give the "Unresolved External" error.

[TOP | BOTTOM | Previous Section | Next Section]

[9.6] Is there any other way to tell the compiler to make the member function be inline?

Yes: Define member functions within the class:

Class Fred {public: Void F (INT I, CHAR C) {

// ...

}};

Although this is easy for people who write classes, but because it is "what" (what) and class "how to" how ", it has brought difficulties. We are usually more willing to use the inline keyword to define a member function outside of the class to avoid this mix. The understanding of this feeling is: in a reusable world, there are a lot of people using your class, and people build it only (yourself); so you should take care of most instead of a few .

[TOP | BOTTOM | Previous Section | Next Section]

[9.7] Is the inline function guaranteed the execution performance?

[Recently Explained "Code Bloat" and Also Added Lots of IF's, And's and But's (on 4/01). Click Here to Go To The Next FAQ in The "chain" of rencent changes.

Do not.

CAUTION Overweight Use Inline Functions may cause code to expand. In the page scheduling environment, it may have a negative impact on performing performance.

The code expansion term only indicates that the size of the code will increase (expand). In the context of the inline function, more concerned is that the inline function increases the size of the execution code and causes the operating system to be unstable. This means that the operating system takes most of the time from the disk from the disk. Of course, the inline function may also reduce the size of the execution code. It seems that it is actually true. In particular, the total code of the call function is sometimes greater than the total amount of code of the expanded inline function. This happens to a very small function, when the optimizer can delete a lot of redundant code - that is, when the optimizer can make long functions, it may occur in length. Therefore, conclusion is: there is no simple conclusion. You have to be affordable for local conditions. Do not make the answer like this, "Do not use inline functions" or "always use the inline function" or "when the function code is less than the N row." This type of tool cut may be very simple, but it is not the best result.

[TOP | BOTTOM | Previous Section | Next Section]

E-mail the author [C FAQ Lite | Table of Contents | Subject Index | About The Author | © | Download Your Own Copy] Revised Apr 8, 2001

转载请注明原文地址:https://www.9cbs.com/read-2043.html

New Post(0)