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
B
is an rvalue reference, why you can applystd::move
to an ravlue reference's member? - After
B.key
andB.value
have been moved, both have been invalidated, but howB
as an object of classA
gets invalidated? - What if I have
A a(A())
,A()
is apparently an rvlaue, canA()
be moved bystd::move
and 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
,Z
can be anything), in the body of the definition Why on earth I should usestd::move(Z)
sinceZ
is already passed by an rvalue reference and when should I usestd::move
?