One of the inline functions

Source: Internet
Author: User

In C, one way to protect efficiency is to use macros (m a c r o ). Macros can make them look like they don't need to be called by common functions.
Function call. Macro is implemented using a Preprocessor rather than a compiler. The pre-processor directly uses macro code instead of macro calls. Therefore, it does not take time to press the parameter stack, generate the CALL of the Assembly Language, RETURN parameters, and execute the RETURN of the assembly language.
All the work is done by the pre-processor, so it is convenient and readable to call the program without spending anything.
In C ++, there are two problems with using Preprocessor macros. The first problem also exists in C: macros look like a function call, but not always. This hides hard-to-find errors. The second problem is that the Preprocessor does not allow access to private data. This means that the Preprocessor macro is useless when used as a member function.
In order to maintain the efficiency and security of the Preprocessor macro, and to be able to access the class freely, C ++ uses inline functions

1. pre-processor Defects
The key problem with Preprocessor macros is that we may think that Preprocessor behavior is the same as that of the compiler. Of course,
Intended to make a macro look and act the same as a function call, so it is easy to be confused
When an expression is used as a parameter in macro calls, when the expression is expanded in the macro, their priority may be different from what we expect. This produces unexpected problems. However, we can avoid it manually during programming and add a bracket to each expression.
Using a macro to evaluate a parameter also produces some unexpected errors, because it cannot completely imitate the behavior of the function.
For example:
# Define band (x)> 5 & (x) <10? (X): 0)
The function is to determine whether x is between 5 and 10. If yes, the value of x is returned. Otherwise, 0 is returned.
Int a = 8; // The condition is clearly met
Cout <band (++ );
The output result is 0.
Because the macro was executed twice at the time of the expansion, the first judgment (x)> 5, at this time a = 9, the second judgment (x) <10, at this time a = 10. Therefore, the final returned results do not meet our expectations.
For C ++, since the pre-processor simply replaces the original code, therefore, you cannot process Private Members by defining a public macro as a member function in the class.

2. inline functions
In the process of solving the problem that the macro in C ++ accesses private class members, all the problems related to the pre-processor macros also disappear. This is achieved by controlling the macro by the compiler. In C ++, the macro concept is implemented as an inline function, and an inline function is a real function in any sense. The only difference is that an inline function is expanded as a macro when appropriate, so the overhead of function calling is canceled. Therefore, you should never use macros, but use inline functions only.
Any function defined in the class automatically becomes an inline function, but you can also use the inline keyword before the function defined outside the class to make it an inline function. To make it effective, the function body and declaration must be combined. Otherwise, the compiler treats it as a common function. Therefore
Inline int PlusOne (int x );
No effect, just declare the function (this does not necessarily get an inline definition at a later time ). The successful method is as follows:
Inline int PlusOne (int x) {return ++ x ;}
Note that the compiler will check whether the function parameter list is correctly used and return values (for necessary conversions ). These tasks cannot be completed by the pre-processor. If we write a Preprocessor macro for the above inline function, there will be unwanted side effects.
The Inline definition should be put in the header file. When the compiler sees this definition, it places the function type (function name + return value) and function body into the symbol table. When using a function, the compiler checks to ensure that the call is correct and the return value is
Correct use, and then replace the function call with the function body, thus eliminating the overhead. Inline code does occupy space, but if the function is small, it actually occupies less space than the code generated for A common function call (parameter pressure stack and execution of c a l.

In the header file, the inline function is an internal connection by default-that is, it is static and can only be seen in the compilation units it contains. Therefore, as long as they are not declared in the same compilation unit, using the same name between the inline function and the global function will not cause conflicts during the connection.

 


3. inline functions inside the class
Inline functions are defined within the class. The inline keyword is not required. Any function defined within the class is automatically an inline function.
Of course, because the inline functions inside the class Save the extra steps for defining the member functions externally, we certainly want to use inline functions for every part of the class declaration. However, it should be noted that the purpose of inline is to reduce the overhead of function calls. If the function is large, the amount of time spent in the function body will be greater than the amount of time spent in the function body, so the result will be smaller. In addition, a large function can replicate the code of all called parts of the function. The result Code expands, but little or no benefit is obtained in terms of speed.

 


 

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.