Search code examples
c++c++11move-semanticsstdmove

What happens when std::move() is called without assignment


What happens when I just use std::move without any assignment?

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

Solution

  • std::move() doesn't move the passed object by itself; it just possibly enables move semantics for the object. It consists of a cast whose result you can use to select – if possible – the proper overload that supports move semantics. Therefore, in your code:

    std::string s = "Moving";
    std::move(s);
    

    std::move(s) above casts s to an rvalue reference. The result of the casting is not used. The object s above is not actually moved, and it isn't modified.