C + + function inline

Source: Internet
Author: User
1. Replace macro code with inline


The C + + language supports function inline, which is designed to improve the efficiency (speed) of function execution.

In C programs, you can use macro code to improve execution efficiency. The macro code itself is not a function, but it is used like a function. The preprocessor replaces the function call by copying the macro code, eliminating the process of parameter compaction, generating assembly language call invocation, returning parameters, and executing return, thus improving the speed. The biggest disadvantage of using macro code is error-prone, and the preprocessor often produces unintended marginal effects when copying macro code.


For example

#define MAX (A, B)         (a) > (b)? (a): (b)

Statement

result = MAX (i, J) + 2;

will be interpreted by the preprocessor as

result = (i) > (j)? (i): (j) + 2;

Because the operator ' + ' is higher precedence than the operator ': ', the above statement is not equivalent to the expected

result = ((i) > (j)? (i): (j)) + 2;

If you rewrite the macro code to

#define MAX (A, B) ((a) > (b)? (a): (b))

You can resolve errors that are caused by the priority level. But even using the modified macro code is not foolproof, such as a statement

result = MAX (i++, J);

will be interpreted by the preprocessor as

result = (i++) > (j)? (i++): (j);

For C + +, there is another drawback to using macro code: You cannot manipulate private data members of a class, which means that macro code is basically for public or global operations.


Let's take a look at how C + + 's "inline function" works. For any inline function, the compiler puts the declaration of the function in the symbol table (including first name, parameter type, return value type). If the compiler does not find an inline function error, then the code of the function is also placed in the symbol table. When an inline function is called, the compiler first checks whether the call is correct (type safety check, or automatic type conversion, of course, all functions are the same). If correct, the code for the inline function replaces the function call directly, eliminating the overhead of the function call. This process differs significantly from preprocessing because the preprocessor cannot perform type safety checks or automate type conversions. If the inline function is a member function, the address of the object (this) is placed in the appropriate place, which is not the preprocessor can do.


The function-inline mechanism of C + + language not only has the efficiency of macro code, but also increases the security, and can manipulate the data members of the class freely. So in a C + + program, you should replace all macro code with an inline function, and assert assert is probably the only exception. An assert is a macro that works only in the Debug version, and it is used to check what should not happen. In order not to cause differences in the Debug version and release version of the program, assert should not produce any side effects. If the assert is a function, because the function call will cause memory, code changes, it will cause the Debug version and release version of the difference. So assert is not a function, but a macro.


2. Programming style for inline functions


The keyword inline must be placed with the function definition body in order for the function to be inline, and only the inline is placed before the function declaration. The following style of function Foo cannot be an inline function:

inline void Foo (int x, int y);   Inline is only associated with function declarations and does not have any effect on  void Foo (int x, int y)  {  ...  }

The following style of function Foo becomes an inline function:

void Foo (int x, int y);    inline void Foo (int x, int y)//inline with function definition body put together  {  ...  }


So, inline is a "keyword for implementation", not a "keyword for declaration." In general, the user can read the declaration of a function, but cannot see the definition of the function. Although the inline keyword is prepended to the declaration and definition of inline functions in most textbooks, I don't think inline should appear in the declaration of a function. While this detail does not affect function functionality, it embodies a fundamental tenet of high quality C++/C program design: declarations and definitions are not to be confused, and the user does not need to know whether the function needs to be inline.


member functions defined within a class declaration will automatically become inline functions, such as

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


It is not a good programming style to place the definition of a member function in a class declaration, although it can bring about convenience in writing, but it should be changed to the following example:

Header file  class A  {public  :      void Foo (int x, int y);  }  //definition file  inline void A::foo (int x, int y) 
  {...  }

3. Be cautious with inline


Inline can improve the efficiency of function execution, why not all functions are defined as inline functions?


If all functions are inline functions, is it useful to "inline" the keyword?


Inline is at the expense of code bloat (replication), which simply eliminates the overhead of function calls and thus increases the efficiency of function execution. If the time of executing the code in the function body is greater than the cost of the function call, then the efficiency will be harvested very little. On the other hand, every call to the inline function will copy the code, which increases the total code size of the program.


Consumes more memory space. It is not advisable to use inline in the following situations:


(1) If the code in the function body is longer, using inline will result in a high cost of memory consumption.


(2) If there is a loop in the function body, the time to execute the function body code is greater than the cost of the function call.


The constructors and destructors of a class are easily misunderstood to be more efficient for inline use. Be aware that constructors and destructors may hide behaviors such as "secretly" executing constructors and destructors for base or member objects. So don't arbitrarily place the definitions of constructors and destructors in class declarations.

The above is the C + + function inline content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.