当我std::move
没有任何分配就使用时会发生什么?
std::string s = "Moving";
std::move(s); //What happens on this line to s?
//is s still valid here?
当我std::move
没有任何分配就使用时会发生什么?
std::string s = "Moving";
std::move(s); //What happens on this line to s?
//is s still valid here?
std::move()
不会自行移动传递的对象;它只是可能为对象启用移动语义。它由一个转换组成,您可以使用它的结果来选择(如果可能的话)支持移动语义的适当重载。因此,在您的代码中:
std::string s = "Moving";
std::move(s);
std::move(s)
以上转换s
为右值引用。不使用铸造的结果。上面的对象s
实际上并没有被移动,也没有被修改。