Explore the C + + object model in Depth--fourth chapter-function semantics

Source: Internet
Author: User

At the end of the fourth chapter, add a few things about inline (inline) functions:
Inline function parameters with side effects, or a single expression to do multiple calls, or inline function in a number of local variables, will produce a temporary object, may produce a large number of extension code, is the size of the program expansion, so the use of the inline function must be cautious:

1. Multiple invocations for a single expression:

For the following incline function:

Inline point operator+ (const point& LHS, const point& RHS)
{point
    new_pt;
    New_pt.x (lhs.x () + rhs.x ()),//x () function for the member variable _x get,set function return
    new_pt;
Since the function is just an expression, in Cfront, its second or subsequent invocation operation is not extended and becomes:

New_pt.x = lhs._x + X__5POINTFV (&RHS);
does not lead to efficiency improvements that need to be rewritten as:

New_pt.x (lhs._x + rhs._x);
2. Inline function for the processing of formal (formal) parameters.

There are the following inline functions:

inline int min (int i, int j)
{return
    i < J i:j;
}
The following three calls:
inline int bar ()
{
    int minval;
    int val1 = 1024;
    int val2 = 2048;

    minval = min (val1, val2);//case 1
    minval = min (1024, 2048);//case 2
    minval = min (foo (), bar () + 1);//case 3
    
    re Turn minval;
}
For CASE1, the call expands directly:
Minval = Val1 < val2? Val1:val2;
CASE2 Direct use of constants:

Minval = 1024;
For CASE3, the temporary object needs to be imported to avoid duplicate evaluation due to the side effects of the parameter being raised (note the use of the following comma expression):
int T1;
int T2;

Minval = (T1 = foo ()), (t2 = bar () + 1), T1 < T2? T1:t2;
3. Local variables are introduced into the inline function.
Rewrite the Min function above to introduce a local variable:
inline int min (int i, int j)
{
    int minval = i < J i:j;
    return minval;
}
For the following call:
int minval;
int val1 = 1024;
int val2 = 2048;
minval = min (val1, val2);
To maintain local variables, they are extended to:
int minval;
int val1 = 1024;
int val2 = 2048;

int __min_lv_minval;//will inline function local variable mangling
minval = (__min_lv_minval = Val1 < val2? val1:val2), __min_lv_minval ;

More complex situations, such as local variables coupled with side-effects parameters, can result in a large number of temporary objects:

minval = min (val1, val2) + min (foo (), bar () + 1);

will be extended to note the comma expression, counting the fractions from left to right, and returning the fractional value at the right end as the final value:

int __min_lv_minval_00;
int __min_lv_minval_01;

int T1;
int T2;

Minval = (__min_lv_minval_00 = Val1 < Val2 val1:val2, __min_lv_minval_00) +
(__min_lv_minval_01 = (T1 = foo (), t2 = bar () + 1, T1 < T2? T1:T2), __min_lv_minval_01);
Generates multiple temporary variables, the use of the inline function must be cautious.

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.