Deep Exploration of C + + object Model->2.3 program transformation semantics

Source: Internet
Author: User

One

1, the display of the initialization of the operation :

x x0;void Foo_bar () {    x x1 (x0);    X x2 = x0;    X x3 = x (x0);}

will be converted to the following steps (two-phase conversions):

  

1 void Foo_bar () 2 {  3    X x1; 4     X x2; 5     X x3; 6 7     X1. X::X (x1); 8     x2. X::x (x2); 9     x3. X::x (x3); Ten }
View Code

2 . Initialization of parameters (formal parameter):

1 void foo (X x0); // Statement 2 3 X xx; 4 foo (xx); 5 // will be converted to the actual following: 6 voif foo (x& x0); 7 8 X _temp0; 9 _temp0. X::x (xx); ten foo (_TEMP0);

Another approach is to use the "copy construction" method to direct the actual parameters to where they should be.

  3. Initialization of the return value:

  This is known as the following function definition:

X Bar () {    x xx;    Process xx    return xx;}

So how does the function return the return value? is actually a double conversion process:

①: First add an extra parameter, type is a reference of class object.

② a copy constructor call operation before the return statement so that the contents of the object to be returned are given the initial value of the new parameter described above.

The following is the converted code for the above function:

void Bar (x& _result) {    X xx;    Xx. X::x ();    Processing xx    _result. X::xx (XX);    return;}

Second, we know the above conversion process, then how to improve the efficiency will be improved?

1, at the user level to do optimization:

   The following is the style of the previous code

X bar (const t& y, const t& z) {    x xx;    return xx;}

will be replaced by the following styles:

  

X Bar (const t& y, const t& z) {    return X (y,z)}

Although the copy constructor is omitted in the above way, it can spread the constructor of special purpose.

 2, at the compiler level to do optimization :

With the so-called named Return Value (NRV) optimization, the following is the process of NRV optimization:

x Bar () {    x xx;     return xx;} // will be replaced by the following: X Bar (x& _result) {    _result. X::x ();     return ;}

Note: When class lacks copy constructor, the compiler will not be able to do NRV optimizations (for reasons unknown).

  3, NRV optimization did get the efficiency improvement, but has been criticized for the following reasons:

①: The user does not know whether NRV is really complete.

②: Once the function becomes very complex, NRV will be difficult to implement. Optimization is performed when all named return instruction statements occur at the top level of the function.

③:

  4. Copy constructor may be subject to an efficiency tax, such as the following code:  

X xx0 (1024x768= x (1014= (x)1014; // the actual process of xx0 is xx0. X::x (1024x768); // the actual process of xx1 and xx2 X _temp0;_temp0. X::x (1024x768); xx. X::x (_temp0); _temp0. X::~x ();

Three

Using memcpy () and memset () in the copy constructor is the most efficient way, but when the class contains virtual functions or contains a virtual base class, the code above will be difficult to execute correctly, Because the compiler adds some code, it's important to know the semantics of some C + + Object model in this case!

Deep Exploration of C + + object Model->2.3 program conversion semantics

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.