Example code:
#include <iostream>
int main()
{
std::vector<int> w(20, 123), x;
w = std::move(w);
std::cout << w.size() << std::endl;
}
Output on g++ 4.8.3: 0
Of course, the standard says that the move-assignment operator leaves the operand in an unspecified state. For example if the code were x = std::move(w);
then we would expect w.size()
to be zero.
However, is there a specified ordering or other clause that covers the self-move case? Is it unspecified whether size is 0
or 20
, or something else, or undefined behaviour? Do the standard containers have any defined semantics here?
Related: this thread talks about whether you should care about self-move in your own classes, but does not discuss whether standard containers' move-assignment operators do, and doesn't provide Standard references.
NB. Is this exactly identical to w = static_cast< std::vector<int> && >(w);
or does the fact that std::move
is a function make a difference?