I am reading about the std::move, move constructor and move assignment operator.
To be honest, all I got now is confusion. Now I have a class:
class A{
public:
int key;
int value;
A(){key = 3; value = 4;}
//Simple move constructor
A(A&& B){ A.key = std::move(B.key);
A.value = std::move(B.value);}
};
- I thought
Bis an rvalue reference, why you can applystd::moveto an ravlue reference's member? - After
B.keyandB.valuehave been moved, both have been invalidated, but howBas an object of classAgets invalidated? - What if I have
A a(A()),A()is apparently an rvlaue, canA()be moved bystd::moveand why? Similarly, if I have a function
int add(int && z){ int x = std:move(z); int y = std:move(z); return x+y; }
What if I call add(5), how can 5 be moved and why?
And notice that z has been moved twice, after z has been moved first time, it has been invalidated, how can you move it again?
- When defining
foo (T && Z )(T,Zcan be anything), in the body of the definition Why on earth I should usestd::move(Z)sinceZis already passed by an rvalue reference and when should I usestd::move?