Reference and mobile semantics in C ++

Source: Internet
Author: User

C ++ references are divided into left reference and right reference. In fact, it is easy to understand that the Left value in the left value reference refers to the value that can appear on the left of the equal sign (the variable with the name, the pointer with the * sign, and other types of data ), the program can reference such a left value to obtain its address. the right value in the right value reference refers to the value displayed on the right of the equal sign (constant number, or the return value of the function (cannot be the reference type of the Left value), the return value of the formula, and other anonymous data that cannot be accessed), the program can reference such a right value to obtain its address. I do not know if it is correct. It can be simply understood that all values that can be assigned are left values, and all values that cannot be assigned are right values. One of the purposes of introducing the right value reference is to implement the moving semantics. The introduction of mobile semantics aims to transfer ownership of the dynamically applied memory space directly during big data replication without the need to move a large amount of data, it not only saves space but also improves efficiency. To implement the mobile semantics, the compiler must know when to copy and when to move the semantics. This is where the right value reference works. Moving semantics may modify the value of the right value. Therefore, the reference parameter of the right value cannot be const. Replication and mobile semantics are achieved by constructing a replication constructor and a mobile constructor. The const & reference is used for the replication constructor, while the mobile constructor uses non-const & reference. The ownership of the data in the mobile semantics is handed over. In order not to parse the same data zone twice, the ownership data must be directed to the nullptr pointer assigned to the dynamically applied memory, that is, the NULL pointer. It is valid to execute delete [] on the NULL pointer. The compiler determines whether the constructor is the left or right value, and then calls the corresponding copy constructor or move constructor to construct the data. Forced movement means that the Left value uses the moving constructor to force the ownership to be handed over. The Utility file declares the std: move () function. Summary: using anonymous variables to hand over ownership to avoid data replication can improve program efficiency. Therefore, if a temporary variable is no longer needed, you can force it to move semantics, in this way, the program no longer needs to replicate a large amount of data, especially when the vector is used as the return value.

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.