Straightforward explanation of the move semantics and right value reference

Source: Internet
Author: User

Straightforward explanation of the move semantics and right value reference

Move semantics and right value reference:

First reference: http://www.cnblogs.com/tingshuo/archive/2013/01/21/2870035.html


Temporary objects in c ++ may exist in large numbers, such as in string/STL calls. Resources (buffer or string) in the temporary object will disappear with the analysis structure of the temporary object, so they (resources) will be copied before the object analysis, instead of directly referencing these resources, otherwise the wild pointer will be used.

In fact, since it is already a temporary object, it will be destructed immediately, so it will not bring too much negative impact if you change the content before the analysis, of course, it is under good control. Based on this idea, we can safely and boldly directly reference the resource pointer in a temporary object (for example, assign a value to the pointer and reference the resource), and leave it blank (to prevent the temporary object from being released, or even worse, assign the pointer we intend to release to the pointer of the temporary object directly. With the help of the time when the temporary object will release its resources during the analysis, release what we want to release!

How subtle!

Before c11, the temporary object can only be passed in the const MyClass & my_object method, and there is a const, so the temporary object cannot be modified. However, c11 introduced the right value to reference MyClass &, so that we can modify the Temporary Variable!

If you are a function provider and you provide parameters with reference to the right value, you can boldly reference its resources and modify its resources, don't worry that it will be used again, after the function returns. Because it is a reference of the right value, the caller of this function will ensure that this parameter will not be used again.

If you are a function caller, when you find that you have a left value object my_object that will not be used any more, you can use the move syntax to convert it to the right value reference and throw it to the function, it doesn't matter if any function modifies its content, because you are sure it is no longer used (if you are not sure, you cannot use the move semantics ). Of course, if you have processed the right value (such as the function return value), it is no different from before.

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.