What happens when you go to the rvalue link?

So, I was burned this a couple of times. What is the difference between the two:

 Movable&& object = std::move(another_movable);

and this:

 Movable object = std::move(another_movable);

It seems that both should work the same way, but I always get inexplicable behavior (object change properties) with a second example. Why is this?

+4
source share
3 answers

Maybe a comparison with the version with the lvalue number might help:

Object& object = another_object;

Object object  = another_object;

The first is a reference to lvalue, so the constructors are not called. It simply refers to an object with a name another_objectas a pointer. Object objectcreates a completely new object, so the constructors will be called (not assuming they will be copied).

rvalue- , rvalues. rvalue, .

+6
Movable&& object = std::move(another_movable);

, another_movable.

Movable object = std::move(another_movable);

, rvalue another_movable.

+5

, . , , rvalue, .

, , rvalue. :

Moveable object = another_moveable;

Moveable object = function_returning_another_moveable();

, , rvalue. . http://en.cppreference.com/w/cpp/language/copy_elision

, Moveable .

, , , - -, . , another_moveable Moveable, Moveable rvalue Moveable another_moveable, , , .

: rvalue , , rvalue , , , . rvalue , , , , ! , , !

0

All Articles