Translation "c++ Rvalue References explained"c++ rvalue reference detailed PART4: Forced move semantics

Source: Internet
Author: User

For the fourth part of this document, please refer to the overview section: http://www.cnblogs.com/harrywong/p/4220233.html.

Force move semantics

As is well known, as the First Amendment to the C + + standard states: "The Commission will not establish any rules that attempt to trap the feet of C + + programmers." (TheCommittee shall make no rule that prevents C + + programmers from shooting themselves in the foot. in serious terms, when faced with the option of giving more control to programmers or reducing their chances of carelessness, C + + tends to be more likely to lead to mistakes in time, but still gives more control. It is in this spirit that c++11 allows you to use the move semantics rather than just the right value, but also the left value, which gives you full discretion. A good example is the swap method in the standard library. As before, give a class X, based on which we can overload the copy constructor and copy assignment operators to implement the move semantics on the right value.

template<class t>void swap (t& A, t& b) {   T tmp (a);   = B;    = tmp;} X A, B;swap (A, b);

There is no right value here. Therefore, the move semantics are not used in the three rows in the swap function. But we know that using the move semantics is possible: whenever a variable appears as a source in a copy constructor or an assignment statement, that variable will no longer be used or simply be the target of the assignment.

In C++11, there is a method called Std::move in the standard library to handle this situation. This function only changes his argument to the right value. Therefore, in c++11, the standard library function swap will look like this:

template<classvoid swap (t& A, t& b) {   T tmp (Std::move (a));   = Std::move (b);    = Std::move (tmp);} X A, B;swap (A, b);

All three rows in the SWAP function now use the move semantics. Remember that for types that do not implement the move semantics (that is, there is not a single copy constructor and an assignment expression type for the right value), the new swap behavior is consistent with the old version.

Std::move is a very simple function. Unfortunately, I'm not yet able to show you the implementation of it. We are going to discuss it later.

Use it in any place where you can use Std::move. As shown in the Swap function above, it brings us the following benefits:

    • For types that implement the move semantics, it is necessary to use the move semantics for the standard library algorithm and operation, and hence the potential performance gain. An important example is the in-place sort (inplace sorting): The In-place sorting algorithm is simply a swap element, nothing else is done, and now the swap process will take advantage of the move semantics provided by the type that provides the move semantics.
    • Standard Template Libraries (STL) often require some types to provide copy capability (copyability). For example, a type that can be used as a container element. Through rigorous observation, it turns out that, in many cases, mobility (moveability) is sufficient. Therefore, we can now use movable rather than replicable types (for example) in places that were not previously allowed. unique_pointer For example, these types can now be used as container types for standard template libraries.

Now that we know std::move, I need to know why it is possible to implement an overload of a copy assignment expression based on an rvalue reference, as I have shown earlier, and still have some problems. Consider the assignment between a simple variable, like this:

a = b;

What do you expect to happen here? You expect the object held by a to be replaced by a copy of B, of course, during the entire exchange, you expect the object that was held by a to be deconstructed. Now consider this line of code:

a = std::move(b);

If the move semantics are implemented as a simple interchange, then the performance will be that the objects held by a and B will be exchanged between A and B. Nothing is being deconstructed. Of course, the object that was held by a will eventually be refactored, that is, when B leaves the scope of the code. Of course, unless B becomes the object of the move, in which case the object held by a is once again obtained. Therefore, only the implementation of the copy-assignment expression is carefully considered, and we do not know when the object that was held by a is to be refactored.

So in a sense, we're going to be stuck here in the mire of uncertainty: Once a variable is allocated, but the object held by that variable is still in place. Only the destructor of that object is feasible when there are no side effects on the outside. But at some point, destructors do have this side effect. One example is releasing a lock in a destructor. Therefore, any part of the destructor that may contain side effects should be clearly represented in the Rvalue reference overload of the copy assignment operator:

x& X::operator= (x&& rhs) {  // to perform a cleanup, note those where the side effects of the destructor may occur.   // ensures that the object is in a state  that can be refactored and assignable // move semantics, exchanging the contents    of this and RHS return *this;}

Translation "c++ Rvalue References explained"c++ rvalue reference detailed PART4: Forced move semantics

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.