inline function in C + + _c language

Source: Internet
Author: User

This paper mainly records the inline function in C + +, that is, the inline function, mainly recorded the following problems:

Why does C + + introduce inline function?

The main purpose is to use it instead of the macro definition in the form of expression in C language to solve the efficiency problem of function invocation in the program.

Macro definition in C language: #define EXPRESSIONNAME (VAR1,VAR2) (VAR1+VAR2) * (VAR1-VAR2) This macro definition, it uses the preprocessor implementation, without the parameters of the stack, code generation, such as a series of operations, and therefore highly efficient. But the disadvantages are as follows:

Simply make a simple substitution in the preprocessor symbol table, so it is not possible to detect the validity of the parameters, and can not enjoy the benefit of the strict type checking of C + + compiler.
In addition, its return value cannot be cast to the appropriate type that can be converted.
Also, C + + introduces class and class access control so that if an action or an expression involves a protected member of the class or a private member, the macro definition cannot be implemented (because the this pointer cannot be placed in the right place).

Two, why inline can be a good substitute for the expression form of the predefined?

inline defines the inline function of a class, where the code for the function is put into the symbol table and replaced directly (as a macro expands) without the overhead of the call, and the efficiency is high.
The inline function of a class is also a true function. When the compiler calls an inline function, it first makes a series of detections (the type of the argument)
The inline function can act as a member function of a class, where it can be used to protect members and private members of the class.

Third, the use of inline function of the occasion

Sample code:

It is known from the above example code that: the 2 member functions of Class A are all inline functions, the readtest () function is defined within the class, so it is automatically converted to the inline function, and the settest () function is defined outside the class, so the inline keyword must be added. Members of a class are often defined as protected and private and cannot be accessed directly by the outside world, so there must be a member interface function to access them. These interface functions are defined as inline functions, which can achieve better results. Therefore, the inline function is often used to define access functions (code brevity), and the inline function is highly efficient.

Why not define all the functions as inline functions?

Inline is the expansion of the code (copy) to see, only to save the cost of function calls, thereby improving the execution efficiency of the function. If the time to execute a function's body code is greater than the overhead of a function call, then efficiency gains will be minimal. On the other hand, every call to the inline function replicates the code, which increases the total code volume of the program and consumes more memory space.
So:

When the code for a function is large, using the inline function can make memory consumption more expensive.
Loop in the body of the function, the time to execute the function is greater than the cost of the function call.
In addition, the constructors and destructors of classes are easily misunderstood to be more efficient than using inline. Be aware that constructors and destructors may hide some behavior, such as "secretly executing constructors and destructors of base classes or member objects." ”
A good compiler automatically cancels the unsuitable inline function according to the function body. (Note that inline should not appear inside the class, and the declaration of the function)

Five, inline function and macro difference?

Inline is expanded at compile time, and macros are expanded at precompiled time.
At compile time, the inline function can be nested directly into the target code, while the macro simply replaces the text
The inline function can complete types and statements correctly, and macros do not have such functionality.
The inline function is a function, and a macro is not a function.
When defining a macro, be careful with the processing of the parameters (generally enclosing the arguments in parentheses), otherwise it can cause ambiguity, and the inline function does not worry about ambiguity.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.