在没有赋值的情况下调用 std::move() 会发生什么

Gau*_*rav 2 c++ move-semantics c++11 stdmove

当我在std::move没有任何分配的情况下使用时会发生什么?

std::string s = "Moving";
std::move(s);  //What happens on this line to s?
//is s still valid here?
Run Code Online (Sandbox Code Playgroud)

眠りネ*_*ネロク 6

std::move()不会自行移动传递的对象;它只是可能为对象启用移动语义。它由一个强制转换组成,您可以使用其结果来选择(如果可能的话)支持移动语义的正确重载。因此,在您的代码中:

std::string s = "Moving";
std::move(s);
Run Code Online (Sandbox Code Playgroud)

std::move(s)以上转换s右值引用。不使用铸造的结果。s上面的对象实际上并没有移动,也没有被修改。