Rvalue reference, move semantics for c++11

Source: Internet
Author: User

Moving semantics is an important concept for c++11, and I have been indefinitely this concept for a long time. The recent turnover of information felt suddenly enlightened, so write it down. In fact, after understanding the concept is very simple, and there is no advanced place.

Let's talk about rvalue references. The right value generally refers to a temporary variable in the expression, in C + + The temporary variable is destroyed after the expression ends, and the program cannot reference the variable again. But C++11 provides a way for us to refer to this temporary variable. This method is called an rvalue reference.

So what's the use of rvalue references? Avoid memory copy!

Unlike other languages, variables in C + + are value semantics (in Java, Python variables are referential semantics). Therefore, for an assignment operation, it means a memory copy rather than a simple assignment pointer. One effect of rvalue references is that we can avoid meaningless memory copy by re-using temporary variables (rvalue).

See this example (taken from Rvalue references:c++0x Features in VC10, part 2)

1 string S0 ("My mother told me that"); 2 string S1 ("cute"); 3 string S2 ("fluffy"); 4 string S3 ("Kittens"); 5 string S4 ("essential part of a healthy diet"); 6 7 string dest = S0 + "+ S1 +" "+ S2 +" "+ S3 +" "+ S4;

The 7th line sums the string, and each call to operator+ () creates a temporary variable that creates a total of 8 temporary string objects. The real inefficiency here is that each time a string object is created, it needs to request space from the heap, copy the contents of the string, and these temporary strings are used only once.

Obviously this is inefficient, why not do it: suppose s0+ "" when the temporary variable created is TEMP_STR, we have been using this temporary variable instead of rewrite to create. The result of this expression is that there will be no change, and since the opertor+ () creates a temporary variable that is not referenced elsewhere in the program, there is no side effect. It's like we secretly did a little optimization that no one else could have noticed.

What can I do to refer to this temporary string object? The answer is an rvalue reference. With rvalue references we can reuse temporary variables in expressions and avoid memory copy with more efficient pointer swap.

One point to note is that rvalue references are optimized to avoid copy of the memory of the object in the heap space. Memory on the heap we can simply pass the pointer Exchange to transfer the ownership of the memory resources (similar to the vector swap method), and for the memory on the stack will inevitably need to copy. To give an example, I fly a kite, put on the feeling tired to give you, specifically how to pay the law? You first make a hand with the same as the handle, and then I put the wire cut short hand to you, you tie the wire to your handle, the handover is complete! The handle corresponds to the stack space, the kite corresponds to the heap space.

Starting this technique is useful, not only when dealing with temporary variables, but sometimes when we want to use the effect of transferring resources (memory), or by forcing the type to Rvalue reference (Std::move) to trigger object movement.

For example, for example, the dynamic expansion of vectors. Familiar with the implementation of the vector know that in the push_back of a vector can trigger the memory reallocation, this time need to copy the original memory objects to the newly allocated memory, and finally release the original memory. Assuming that the vector contains a string object, we copy not just the memory of sizeof (string), but we are doing a simple object assignment (called the String::operator= () method). We also copy this string internal pointer to the memory on the heap space. We can see from the beginning that we do not have to copy the memory of the internal pointer, because the original string object will be destroyed after the assignment, if we take this pointer secretly (swap), the other parts of the program will not be aware of any. To achieve this we need to do the following two things:

    1. Implement a move constructor, move assignment function on string. These functions swap the internal pointers instead of the copy operation.
    2. The std::move is converted to an rvalue reference to trigger the move assignment function. The compiler is t&& by the parameter type, only to know that the mobile version of operator= () should be used instead of the copy version of Operator= ().
1 new_stri = Std::move (OLD_STR);

To convert a old_str to an rvalue reference is because it is not a true right value, it is not a temporary variable. But because it is about to be destroyed, the effect is equivalent to a temporary variable. It is therefore safe to convert, calling the move assignment function and quietly "moving" its memory resources.

Recommended reading:

    1. Know: How to understand the move semantics in C + +? Chu Hao's answer

Resources:

    1. Visual C + + Team Blog:rvalue references:c++0x Features in VC10, part 2

Rvalue reference, move semantics for c++11

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.