3

This answer writes a move constructor like this:

dumb_array(dumb_array&& other)
        : dumb_array() // initialize via default constructor, C++11 only
    {
        swap(*this, other);
    }

Note: this swap is a custom swap also defined on the class, not std::swap.

This answer suggests that use of std::move:

you should only call std::move where appropriate, but this is were personal preferences come in.

I just watched a Scott Meyers talk on move semantics where he stated that an rvalue reference as a function parameter is turned back into an lvalue by virtue of the fact that it is now named, and therefore std::move is necessary once again turn it back into an rvalue.

In other words, the code above may very well not be doing a move operation, and that, unlike the quote above, use of std::move in such contexts is not about personal preference but about actually gaining benefits of move semantics.

Is it accurate to say then that if std::move is not explicitly used inside a move constructor or a move assignment operator, moving is probably not actually happening, and instead a copy is being done?

2
  • @erip since we write code based on a standard and not based on an implementation (ideally), then it shouldn't be hard to say what the standard advises us to do. Meyers said that the most common use of std::move is inside a function that refers to an argument that is an rvalue, but yet I rarely see this done in online examples Commented May 4, 2016 at 10:38
  • To clarify, you are asking about using move instead of swap? As opposed to using both, swap(*this, move(other)) ? The latter would be pointless of course, but it might help to really clarify what the alternative is Commented May 4, 2016 at 11:56

2 Answers 2

4

Is it accurate to say then that if std::move is not explicitly used inside a move constructor or a move assignment operator, moving is probably not actually happening, and instead a copy is being done?

No, that is most certainly not accurate. In the particular case in the question, it fully depends on the implementation of swap.

A natural implementation would be swap(dumb_array&, dumb_array&) which internally swaps some pointers, perhaps like this:

void swap(dumb_array &lhs, dumb_array &rhs)
{
  std::swap(lhs.pointer_to_data, rhs.pointer_to_data);
}

If that is the case, then the move constructor you've shown certainly performs a lightweight move—just a pointer swap. Second, using std::move when calling swap would be downright wrong, as swap takes lvalue references.

Sign up to request clarification or add additional context in comments.

Comments

1

I just watched a Scott Meyers talk on move semantics where he stated that an rvalue reference as a function parameter is turned back into an lvalue by virtue of the fact that it is now named, and therefore std::move is necessary once again turn it back into an rvalue.

That's correct.

But there are many misconceptions around this topic.

In other words, the code above may very well not be doing a move operation

It won't do a move. However, it's likely that swap is an efficient operation anyway and therefore you don't care.

use of std::move in such contexts is [...] about actually gaining benefits of move semantics.

Not true. Even if you use std::move, there is no guarantee that any moving will occur.

Is it accurate to say then that if std::move is not explicitly used inside a move constructor or a move assignment operator, moving is probably not actually happening, and instead a copy is being done?

moving can occur, even if there is no std::move.

So basically, std::move is neither necessary, nor sufficient, for moving to occur. This may seem quite surprising.


I think there is really no shortcut to this. If you want to understand this, find a good comprehensive tutorial. E.g. The popular C++ Rvalue References Explained by Thomas Becker.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.