This code works as expected (online here).
At the end v
is empty and w
is not empty as it has pilfered the contents of v
.
vector<int> v;
v.push_back(1);
cout << "v.size(): " << v.size() << endl;
auto vp = move(v);
vector<int> w(vp);
cout << "w.size(): " << w.size() << endl;
cout << "v.size(): " << v.size() << endl;
But if I replace auto vp=move(v)
with
vector<int> && vp = move (v);
Then it doesn't move. Instead it copies and both vectors are non-empty at the end. As shown here.
Clarification: More specifically, what is the auto-derived type of vp
? If it's not vector<int> &&
, then what else could it be? Why do the two examples give different results despite being so similar?
Extra: I also tried this, and it still copied instead of moving
std :: remove_reference< vector<int> > :: type && vp = move(v);