The std::move function aims at implementing move semantics. The function receives a forwarding reference to an object and returns an rvalue reference to this object. One of the possible implementations of std::move looks as follows:
template<typename T>
std::remove_reference_t<T>&& move(T&& x)
{
return static_cast<std::remove_reference_t<T>&&>(x);
}
lvalue and rvalue references can be passed to the std::move function, and an rvalue reference must be obtained as a result. Therefore, to denote the return type, we first use the std::remove_reference_t trait, that simplifies the template type T to a usual non-reference type. Then we explicitly add '&&'. Let's take a look at the following code fragment:
std::vector<int> &vec1 = DoSomeCalculations();
std::vector<int> vec2 = std::move(vec1);
Here's what we have here:
Now let's consider the case when an rvalue reference is passed to std::move:
std::vector<int> &&vec1 = DoAnotherCalculations();
std::vector<int> vec2 = std::move(vec1);
Here's what happens:
The std::move call results in an xvalue. This means that the resources of such an object can be reused. If it's possible, move semantics (not the copy semantics) is applied for an xvalue expression. For example, if the result of the std::move function is assigned to some obj object of the type type, then the move assignment operator is called (if it is defined for type).
Additional links:
0