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

Source: Internet
Author: User

4.5 Inline FunctionsThe following are possible implementations of one of the addition operators of 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 more "clean" approach is to use the inline function set and get functions to complete.
void point::x (float new_x) {_x = new_x;} Float point::x () {return _x;} New_pt.x (lhs.x () + rhs.x ());
because the constraints can only directly access the _x in the two functions above, it minimizes the impact of changes to the data members that might later occur (such as moving up or down in the inheritance system) .Assuming that these access functions are declared inline, you can continue to maintain the high efficiency of direct access to the members-at the same time we also take into account the encapsulation of the function. In addition, the addition operator no longer needs to be declared as a friend of point.
However, it is not actually possible to force any function to become inline. keyword inline(or definition of member function or friend function in class declaration)It 's just a request. Assuming that the request is accepted, the compiler must feel that it can reasonably extend the function with an expression.
The compiler believes it can reasonably extend an inline function, meaning that at a certain level, its operating costs are lower than that of normal function calls and return mechanisms. Cfront has a complex test method that is often 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".
assuming that a function is inferred as not being inline because of its complexity or construction, it is converted to a static function and produces the corresponding function definition within the "compiled module".
2. The true inline function extension operation is at the point of invocation, which leads to the evaluation of the parameters and the management of the transient objects.
The same is on the extension point, and the compiler will determine whether the call is "not inline". In Cfront, the inline function assumes that there is only one expression, then its second or subsequent invocation operation:
New_pt.x (lhs.x () + rhs.x ());
is not extended, it is due to the fact that 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 you can do about it is to rewrite its contents:
New_pt.x (lhs._x + rhs._x);

Formal references (formal Arguments)What really happened during the inline expansion? Yes, each form parameter is replaced by the corresponding actual reference. Assuming that there is a side effect, that is, 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 for the actual number of references.In general, it is often necessary to introduce temporary objects in the face of the "actual number of possible side effects".Other words assuming that the actual parameter is a constant expression, it is possible to complete the evaluation operation before replacing it, and the subsequent inline substitution allows the constants to be "tied" directly. Assuming that neither a constant expression nor an expression with a side effect, replace it directly.
For example, if you have the following simple inline function:
inline int min (int i, int j) {return i < J I:j;}
The following are three invocation actions:
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:
Direct replacement of 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 raises the side effect of the parameter, which requires importing a temporary object to avoid repeated 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)Suppose you change the definition slightly and add a local variable to the inline definition:
inline int min (int i, int j) {int minval = i < J I:j;return Minval;}
Does this local variable require additional support or processing? The following call operation is provided:
{int Local_var;int minval;//... minval = min (va1, val2);}
When inline is extended, it may become so 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. Assuming that the inline function expands multiple times with a single expression, each extension requires its own set of local variables. Assuming inline Functions are extended multiple times with separate formulas, so you can use them repeatedly with just a set of local variables.
The local variables in the inline function, coupled with the number of side effects, can result in a large number of transient objects. In particular, it is assumed that it has been 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:
Generates a temporary variable int __min_lv_minval_00;int __min_lv_minval_01;//for the local variable to produce a 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 large number of #define used in C programs (pre-processing macros) A safe alternative--especially assuming that the parameters in the macro have side effects,An inline function, however, assumes that it has been called too many times to generate a large number of expansion codes, which can cause the program to skyrocket in size.
The arguments have side effects, or multiple invocations in a single expression, or multiple local variables in the inline function. will produce temporary objects ., the compiler might be able to remove them. In addition, inline in the inline may make a seemingly mediocre-looking inline less extensible due to its cascading complexity. Such a situation could occur in the constructors of the complexity class system, Or some seemingly incorrect chain of inline call locks in the object system-each of them runs a small set of operations and then makes a request for an 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.

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.