C + + inline function parsing (inline)

Source: Internet
Author: User
Tags define function

First, the basic definition

Inline is a keyword in the C + + language that can be used to define inline functions in a program, and the introduction of inline makes the definition of inline functions simpler. When it comes to inline functions, here is a more common definition, the inline function is a special function in C + +, it can be called like a normal function, but not through the mechanism of function call, but by inserting the function body directly into the call, which can greatly reduce the cost of the function call. So as to improve the operation efficiency of the program. In general, inline is used to define member functions of a class.

Second, the basic use of the inline

Inline is simple to use, just add the inline keyword to the header when declaring or defining a function, in the following format

1 inline return value type  function name (function parameter) {2     // define function Body 3 } here

In general, there are two functions for inline, one is a member function defined within a class, the other is declared within a class, and a member function that is defined outside the class is somewhat different for the use of inline in both cases:

(1) Define member functions within a class

In this case, we can not add the inline keyword to the function header, because the compiler automatically declares the function defined in the class as an inline function, the code is as follows:

1 classtemp{2    Public:3      intamount;4 5   //constructor Function6Tempintamount) {7        This->amount =amount;8    }9   Ten   //normal member functions, which can be used without inline when defined within a class One   voidPrint_amount () { Acout << This-amount; -   } - } the     

As you can see from the code above, when you define a function within a class, you can do without the inline keyword, and the compiler automatically sets the function (constructor, destructor, normal member function, and so on) defined in the class to inline, with the nature of the inline function call.

(2) Declaring functions within a class, defining functions outside of the class

According to the rules of the C + + compiler, if you want to set the function as an inline function, you can declare it within a class without the inline keyword, and when you define a function outside of the class, add the inline keyword, as shown in the following code:

1 classtemp{2    Public:3      intamount;4 5   //constructor Function6Tempintamount) {7        This->amount =amount;8    }9   Ten   //Normal member function, which can be declared without inline in class One   voidPrint_amount () A } -  - //defining a function body outside of a class must precede the inline keyword theInlinevoidTemp:: Print_amount () { -cout << Amount <<Endl; - } -     

From the above code we can see that the class declaration can not add the inline keyword, but the class outside the definition of the function body must be added, so as to ensure that the compiler can recognize it as an inline function.

In addition, we can write inline when declaring and defining functions, or we can add inline only to function declarations, while defining functions without inline. As long as the inline information is communicated to the compilation system before the function is called, the compilation system will process the function call in inline function. In other words, the above mentioned methods can implement an inline function definition, according to their own needs to write.

Three, the advantages and disadvantages of the inline function:

1.inline defines an inline function of a class, where the code of the function is placed in the symbol table and replaced directly when used (like a macro), without the overhead of a call, and with high efficiency.

2. Obviously, the inline function of a class is also a true function, and when the compiler calls an inline function, it first checks the type of its arguments to ensure that the call is correct. Then perform a series of related checks, just like any real function. This eliminates its hidden dangers and limitations. (Macro substitution does not check parameter types, security risks are large)

The 3.inline function can be used as a member function of a class, the same as the ordinary member function of a class, and can access private members and protected members of a class. An inline function can be used instead of a generic macro definition, and the most important application is the definition of the access function for the class.

Disadvantages:

1. Inline functions have certain limitations, the function body of the inline function is generally not too large, if the function body of the inline function is too large, the general compiler will abandon the inline mode, and the normal way to call the function. (In other words, you use an inline function, just a request to the compiler, and the compiler can reject your request) so the inline function is as efficient as the normal function execution.

The 2.nline description is only a suggestion for the compiler, and the compiler can choose to ignore the recommendation. For example, if you specify a function as inline with more than 1000 lines, the compiler ignores the inline and restores the function to a normal function, so it is not said that defining a function as an inline function is bound to be recognized by the compiler as an inline function. Depending on the implementation of the compiler and the size of the function body.

Iv. differences between inline functions and macro definitions

The difference between an inline function and a macro is that a macro is replaced with a macro by a preprocessor, and inline functions are implemented through compiler control. and the inline function is the real function, but when needed, the inline function expands like a macro, so it cancels the function's argument stack and reduces the cost of the call. You can call inline functions just like you would call a function, without worrying about some of the problems that might arise from dealing with macros. Inline functions are compared with the macro definitions with parameters, their code efficiency is the same, but the inner function is better than the macro definition, because the inline function follows the type and scope rules, it is more similar to the general function, in some compilers, once the association on the inline extension, will be the same as the general function of the call, more convenient.  

In addition, the macro definition in use is only a simple text substitution, and does not do strict parameter checking, you can not enjoy the C + + compiler strict type checking the benefits, and its return value can not be cast to the appropriate type convertible, so that its use there is a series of hidden dangers and limitations.

C + + is proposed to completely replace the macro definition, because the inline function cancels the shortcomings of the macro definition, but also well inherited the advantages of macro definition, "effective C + +" mentioned in the use of inline instead of macro definition of the terms, Suffice to explain the effect of inline.

v. precautions for use

1. Inline functions cannot include complex control statements, such as loop statements and switch statements;

2. Inline functions cannot include complex control statements, such as loop statements and switch statements;

3. Only small size (typically 5 statements) and frequently used functions are declared as inline functions. In the case of a small function, the time overhead of a function call may be equal to or even more than the time of execution of the function itself, which is defined as an inline function, which greatly reduces program run time.

Reference Blog

Https://www.cnblogs.com/msdn1433/p/3569176.html

Https://www.cnblogs.com/sherry-best/archive/2013/03/27/2984535.html

Https://www.cnblogs.com/develop-me/p/5678897.html

C + + inline function parsing (inline)

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.