Очень упрощенное представление std::string
:
namespace std {
class string
{
private:
char *_M_string;
size_t _M_size;
// as you can see, std::string is basically just a wrapper
// around char * and size
public:
// this constructs an empty string
string()
: _M_string(nullptr), _M_size(0) { }
// this will actually copy the string, so it's not interesting to us...
string(const string &other);
// this is the constructor that will be called when you use std::move
string(string &&other)
: string() // <-- construct an empty string first
{
// swap our stuff with "other"s possibly non-empty stuff
swap(_M_string, other._M_string);
swap(_M_size, other._M_size);
// and now "other" string is empty and "this" string has its content
}
~string()
{
// deleting a nullptr is fine
delete [] _M_string;
}
//... other stuff
};
} // std
Таким образом, нет никакой "магии" за std::move
, единственное, что он делает, это помогает выбрать правильную перегрузку конструктора, после этого ондо реализации класса, чтобы воспользоваться его внутренним представлением и сделать «дешевую копию», если это возможно, оставив «другой» объект в каком-то допустимом «пустом» состоянии.