Is it permitted for an object (in particular of an std
class) to be self-moved or it is an undefined-behavior?
Consider an example:
#include <iostream>
#include <string>
int main()
{
std::string s("123");
s = std::move(s);
std::cout << s;
}
In gcc/clang the program prints nothing, so s
string is lost during moving:
https://gcc.godbolt.org/z/xqTWKfMxM
But in MSVC it works fine.
From cpp ref:
Also, the standard library functions called with xvalue arguments may assume the argument is the only reference to the object; if it was constructed from an lvalue with std::move, no aliasing checks are made. However, self-move-assignment of standard library types is guaranteed to place the object in a valid (but usually unspecified) state:
std::vector<int> v = {2, 3, 3};
v = std::move(v); // the value of v is unspecified