C + + object model--inline Functions (fourth chapter)

Source: Internet
Author: User

4.5 Inline FunctionsThe following is the possible implementation of an addition operator for Point class:
Class Point {friend point operator+ (const point&, const point&);};  Point operator+ (const-point &LHS, const-point-&RHS) {Point new_pt;new_pt._x = lhs._x + rhs._x;new_pt._y = lhs._y + Rhs._y;return new_pt;}
Theoretically, a "clean" approach is done using the inline function set and the Get function.
void point::x (float new_x) {_x = new_x;} Float point::x () {return _x;} New_pt.x (lhs.x () + rhs.x ());
because it is restricted to directly access the _x in the two functions above, it minimizes the impact of changes to the data members that may occur later, such as moving up or down in the inheritance system.If you declare these access functions as inline, you can continue to maintain the high efficiency of direct access to the members-and we also take into account the encapsulation of the functions. In addition, the addition operator no longer needs to be declared as a friend of point.
However, it is not really possible to force any function to be inline. keyword inline(or definition of member function or friend function in class declaration) just a request. If the request is accepted, the compiler must assume that it can reasonably extend the function with an expression.
The compiler believes it can reasonably extend an inline function, meaning that at some level its execution cost is lower than that of the normal function call and return mechanism. Cfront has a complex set of tests that are commonly used to calculate assignments,function calls, The number of operations such as virtual function calls. Each expression type has a weight, and the complexity of the inline function is determined by the sum of these operations.
Generally speaking to process an inline function, there are two stages:
1. Analyze function definitions to determine the function "intrinsic inline ability"(The intrinsic ability to inline). " The term "intrinsic" (essential, solid) here means "related to compilers".
If a function is judged not to be inline because of its complexity or construction, it is converted to a static function and the corresponding function definition is generated within the "compiled module".
2. The true inline function extension operation is at the point of invocation, which leads to the evaluation of parameters and the management of temporary objects.
Also on the extension point, the compiler will determine whether the call is "not inline". In Cfront, the inline function, if there is only one expression, its second or subsequent invocation operation:
New_pt.x (lhs.x () + rhs.x ());
Will not be extended, because in Cfront it becomes:
New_pt.x = lhs._x + X_5POINTFV (&RHS);
This simply does not bring about an improvement in efficiency! The only thing that can be done is to rewrite its contents:
New_pt.x (lhs._x + rhs._x);

Formal parameters (formal Arguments)What really happened during the inline expansion? Yes, each form parameter is replaced by the corresponding actual parameter. If there is any side effect, it is not possible to simply one by one each form parameter that appears in the plug-in program, as this will result in multiple evaluation operations on the actual parameters.In general, it is often necessary to introduce temporary objects in the face of "actual parameters that can cause side effects".Other words if the actual argument is a constant expression, you can complete the evaluation operation before replacing it, and the subsequent inline substitution allows you to "tie" the constants directly. If neither a constant expression nor an expression with a side effect, replace it directly.
For example, suppose you have the following simple inline function:
inline int min (int i, int j) {return i < J I:j;}
Here are three call operations:
inline int bar () {int Minval;int val1 = 1024;int val2 = 2048;/*1*/minval = Min (val1, val2);/*2*/minval = min (102, 2048);/* 3*/minval = min (foo (), bar () +1); return minval;}
the line labeled 1 will be expanded to:
Parameter Direct Replacement minval = Val1 < val2? Val1:val2;
the line labeled 2 will be expanded to:
After substitution, use the constant minval = 1024 directly;
the row labeled 3 throws a side effect of the parameter, and it needs to import a temporary object to avoid repeating the evaluation:
There are side effects, so import temporary object int T1;int t2;minval = (T1 = foo ()), (t2 = bar () + 1), T1 < T2? T1:t2;

Locals (local Variables)If you change the definition slightly, add a local variable to the inline definition:
inline int min (int i, int j) {int minval = i < J I:j;return Minval;}
What additional support or processing is required for this local variable? If you have the following call action:
{int Local_var;int minval;//... minval = min (va1, val2);}
When inline is extended, it may turn out to maintain its local variables (in theory the local variables in this example can be optimized and their values can be calculated directly in Minval):
{int Local_val;int minval;//The local variable of the inline function as "mangling" operation int __min_lv_minval;minval = (__min_lv_minval = Val1 < val2?) VAL1:VAL2), __min_lv_minval;}
Generally speaking each local variable in the inline function must be placed in a closed section of the function call, with a unique name. If the inline function expands multiple times with a single expression, each extension requires its own set of local variables. If inline Functions are extended multiple times with separate formulas, so you can reuse them with just a set of local variables.
The local variables in the inline function, coupled with the side-effect parameters, can result in a large number of temporary objects. Especially if it is extended multiple times with a single expression. For example, the following call operation:
minval = min (val1, val2) + min (foo (), foo () + 1);
May be extended to:
Generate temporary variable int __min_lv_minval_00;int __min_lv_minval_01;//for local variable to place side effect value and generate temporary variable int t1;int t2;minval = ((__min_lv_minval_ xx = Val1 < val2? VAL1:VAL2), __min_lv_minval_00) + ((__min_lv_minval_01 = (T1 = foo ()), (t2 = foo () + 1), T1 < T2? t1:t2), __min_lv _MINVAL_01);
The inline function provides a necessary support for encapsulation and may effectively access the nonpublic data encapsulated in class. It is also a safe alternative to the heavily used #define (pre-processing macros) in C programs-especially if the parameters in the macro have side effects, However, if an inline function is called too many times, it will generate a large number of expansion codes, which can cause the program to skyrocket in size.
Parameters have side effects, or multiple invocations in a single expression, or multiple local variables in the inline function. will produce temporary objects ., the compiler may be able to remove them. In addition, the inline in the inline may cause a seemingly mundane inline to be extended by its interlocking complexity. This can happen in the complexity class system of constructors, Or some seemingly incorrect inline call locks in the object system-each one performs a small set of operations and then makes a request to another object. The inline function provides a powerful tool for both security and efficiency programs. However, They need to be handled more carefully than the non-inline function.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + object model--inline Functions (fourth chapter)

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.