In this answer there is a sample implementation of move constructor of a string
object:
string(string&& that) // string&& is an rvalue reference to a string
{
data = that.data;
that.data = nullptr;
}
And also beside this toy example I've seen many places that say
a moved object shall be in a valid but unspecified state.
For example a std::string
is usually an empty one after being moved. My question is why bother ourself to change the state of a rvalue?
If an rvalue refrence is a hint to the callee that "we no longer need the object, so do whatever you want with it", so why change it and set it to nullptr
or make the string empty? We can do nothing with it if the caller has told us I no longer use it.