I am learning C ++ 11, and I have a question regarding the semantics of movement and references to rvalue values. My sample code is as follows (C ++ shell URL cpp.sh/8gt ):
#include <iostream>
Result:
size of a before move: 3 size of a after move: 3
It seems that the move assignment operator std :: vector is not called on the line v = a in the aaa function, otherwise a will be of size 0 instead of 3.
However, if I changed v = a to v = std::move(a) , the result is
size before moving: 3
size after movement: 0
and I think this time the destination assignment operator std :: vector was called.
My quesiton is why the assignment operator is not called for the first time? According to C ++, the std :: vector reference has an assignment operator that accepts an rvalue reference.
copy (1) vector & operator = (const vector & x); move (2) vector & operator = ( vector & x );
initializer list (3) vector & operator = (initializer_list il);
Then, in the string v = a , since a is declared as an rvalue reference, the move operator must be called. Why do we still need to wrap with std :: move?
Thank you very much in advance!
[edit] I think both Loopunroller and Kerrek SB answered my question. Thank you I donโt think I can choose two answers, so Iโll just choose the first one.
source share