C ++ 11 new features-Make your program more efficient (right-value reference avoids deep copy)

Source: Internet
Author: User

C ++ 11 new features-Make your program more efficient (right-value reference avoids deep copy)

For more information, see in-depth application of C ++ 11 code optimization and engineering-level application.

Today, review the right value.

Using the right value makes our C ++ program more efficient.

We can simply regard the right value as a temporary variable. Previously, no one cares about this temporary variable, but it has to pay the cost of efficiency.

The right value reference is the reference type for the right value. Remember to initialize it like the reference we usually call.

Distinguish between left and right values:

Void func (X & x); // The left value references the overloaded void func (X & x); // the right value references the overloaded X x X; X foobar (); func (x); // The parameter is the left value. Call func (X & x); func (foobar (); // The parameter is the right value, call func (X & x );

Let's take a look at this:

// lvalues:  //  int i = 42;  i = 43; // ok, i is an lvalue  int* p = &i; // ok, i is an lvalue  int& foo();  foo() = 42; // ok, foo() is an lvalue  int* p1 = &foo(); // ok, foo() is an lvalue  // rvalues:  //  int foobar();  int j = 0;  j = foobar(); // ok, foobar() is an rvalue  int* p2 = &foobar(); // error, cannot take the address of an rvalue  j = 42; // ok, 42 is an rvalue

As mentioned in the initialization list blog, using the initialization list for initialization is more efficient than initializing In the constructor. The truth is the same.

This conclusion must be correct. When you try to write code for verification using the VS compiler, you may become a mother-in-law. Isn't the fucking output the same.

I can only do that, because the compiler optimizes the program, you cannot see it. If you have to understand it, try to look at the disassembly step by step. You are satisfied with the package.

The following describes how to avoid deep copy using the right value.

At this point, you may be guilty again?
Isn't deep copy a good thing? Does it make the assignment constructor we write to achieve deep copy? It is to avoid two pointers pointing to the same thing, and to avoid deleting the same thing twice.

However, everything is constantly evolving. Because C ++ 11 introduces the right value concept, this makes the previously written value assignment constructor not so perfect. Efficiency is the main problem.

The following figure uses the mobile constructor ~ I have mentioned this before:

MyString (MyString & str) {std: cout <"Move Constructor is called! Source: "<str. _ data <std: endl; _ len = str. _ len; _ data = str. _ data; // avoid unnecessary str copies. _ len = 0; str. _ data = NULL;} MyString & operator = (MyString & str) {std: cout <"Move Assignment is called! Source: "<str. _ data <std: endl; if (this! = & Str) {_ len = str. _ len; _ data = str. _ data; // avoid unnecessary str copies. _ len = 0; str. _ data = NULL;} return * this ;}

With the right value reference and moving semantics, when designing and implementing classes, we should design copy constructors and value assignment functions for classes that require dynamic application of a large number of resources, to improve application efficiency. It should be noted that, while providing the constructor referenced by the right value, we also provide the copy constructor referenced by the Left value of the constant to ensure that the copy constructor can still be used if the movement fails.

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.