std::move

From cppreference.com
< cpp‎ | utility

Defined in header <utility>
template< class T >
typename std::remove_reference<T>::type&& move( T&& t );
(since C++11)
(until C++14)
template< class T >
constexpr typename std::remove_reference<T>::type&& move( T&& t );
(since C++14)

std::move obtains an rvalue reference to its argument and converts it to an xvalue.

Code that receives such an xvalue has the opportunity to optimize away unnecessary overhead by moving data out of the argument, leaving it in a valid but unspecified state.

Contents

[edit] Parameters

t - the object to be moved

[edit] Return value

static_cast<typename std::remove_reference<T>::type&&>(t)

[edit] Exceptions

noexcept specification:  
noexcept
  


[edit] Notes

The functions that accept rvalue reference parameters (including move constructors, move assignment operators, and regular member functions such as std::vector::push_back) are selected, by overload resolution, when called with rvalue arguments (either prvalues such as a temporary objects or xvalues such as the one produced by std::move). If the argument identifies a resource-owning object, these overloads have the option, but aren't required, to move any resources held by the argument. For example, a move constructor of a linked list might copy the pointer to the head of the list and store nullptr in the argument instead of allocating and copying individual nodes.

Names of rvalue reference variables are lvalues and have to be converted to xvalues to be bound to the function overloads that accept rvalue reference parameters, which is why move constructors and move assignment operators typically use std::move:

// Simple move constructor
Foo(A&& arg) : member(std::move(arg)) // the expression "arg" is lvalue
{} 
// Simple move assignment operator
A& operator=(A&& other) {
     member = std::move(other.member);
     return *this;
}

One exception is when the type of the function parameter is rvalue reference to type template parameter ("universal reference"), in which case std::forward is used instead.

Unless otherwise specified, all standard library functions that accept rvalue reference parameters (such as std::vector::push_back) are guaranteed to leave the moved-from argument in valid but unspecified state (that is, only the functions without preconditions, such as the assignment operator, may be used on the object after it was moved into a standard library container:

std::vector<std::string> v;
std::string str = "example";
v.push_back(std::move(str)); // str is now valid but unspecified
str[0]; // undefined behavior: operator[](size_t n) has a precondition size() > n
str.clear(); // OK, clear() has no preconditions

Also, the standard library functions called with xvalue arguments may assume the argument is the only reference to the object; if it was constructed from an lvalue with std::move, no aliasing checks are made. In particular, this means that standard library move assignment operators do not have to perform self-assignment checks:

std::string s = "example";
s = std::move(s); // undefined behavior

[edit] Example

#include <iostream>
#include <utility>
#include <vector>
#include <string>
 
int main()
{
    std::string str = "Hello";
    std::vector<std::string> v;
 
    // uses the push_back(const T&) overload, which means 
    // we'll incur the cost of copying str
    v.push_back(str);
    std::cout << "After copy, str is \"" << str << "\"\n";
 
    // uses the rvalue reference push_back(T&&) overload, 
    // which means no strings will be copied; instead, the contents
    // of str will be moved into the vector.  This is less
    // expensive, but also means str might now be empty.
    v.push_back(std::move(str));
    std::cout << "After move, str is \"" << str << "\"\n";
 
    std::cout << "The contents of the vector are \"" << v[0]
                                         << "\", \"" << v[1] << "\"\n";
 
    // string move assignment operator is often implemented as swap,
    // in this case, the moved-from object is NOT empty
    std::string str2 = "Good-bye";
    std::cout << "Before move from str2, str2 = '" << str2 << "'\n";
    v[0] = std::move(str2);
    std::cout << "After move from str2, str2 = '" << str2 << "'\n";
}

Possible output:

After copy, str is "Hello"
After move, str is ""
The contents of the vector are "Hello", "Hello"
Before move from str2, str2 = 'Good-bye'
After move from str2, str2 = 'Hello'

[edit] Complexity

Constant

[edit] See also

(C++11)
forwards a function argument
(function template)
obtains an rvalue reference if the move constructor does not throw
(function template)
(C++11)
moves a range of elements to a new location
(function template)