0

I don't understand why the standard is now using a move constructor and move assignment operator that take r-value arguments. I know that often it's useful to use temporary objects as arguments to a constructor or an assignment, as:

Object obj1(1);
obj1 = Object(2);

and instead of creating a temporary, deep copying, and destroying the temporary, it can just move the object over more efficiently. But isn't it the case that move semantics would be done just as commonly using an existing l-value? In that case the whole "&&" double ampersand to mean an r-value reference when you're using an l-value is confusing to me.

Object obj1(1), obj2(2);
obj1 = std::move(obj2);    // Use move assignment operator here, still using move semantics

In using the move assignment operator in this case, it has nothing to do with any r-value, am I right? It's just that I keep hearing that the "&&" double ampersand in

Object& operator=(Object&& other);

is an r-value reference, but in this example it's not, right? And in any case when you want to move an existing object to another, there's no involvement of an r-value or r-value reference, right?

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • "*I don't understand why the standard is now using a move constructor and move assignment operator that take r-value arguments.*" "Now", as opposed to when? "Now" was C++11, when move-semantics were invented. – Nicol Bolas Aug 21 '17 at 21:10
  • @Nicol Bolas Yeah from C++11. I mean, before move semantics if you wanted to move one object to another for whatever reason you wouldn't think of temporaries or r-value references, would you? You'd just think about how to copy(move) the members over and make sure the other object wouldn't explode from having multiple pointers to something. – Zebrafish Aug 21 '17 at 21:15
  • @Zebrafish "copy" and "move" are different concepts ... if you copy a vector then there are two copies of its content afterwards, but if you move a vector there is only one – M.M Aug 21 '17 at 21:39

0 Answers0