std::move
From Cppreference
C++ Standard Library | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Utilities library | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <utility>
|
||
template< class T >
typename std::remove_reference<T>::type&& move( T&& t ); |
(C++11 feature) | |
std::move obtains an rvalue reference to its argument.
It is typically used to implement move semantics when invoking functions. When std::move is used the compiler will look for a function overload that takes an rvalue reference. This means that the contents of t will be moved into the function, leaving t as an xvalue.
Contents |
[edit] Parameters
t | - | the object to be moved |
[edit] Return value
static_cast<typename std::remove_reference<T>::type&&>(t)
[edit] Exceptions
[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 copied; instead, the contents // of str will be moved into the vector. This is less // expensive, but also means str will 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"; }
Output:
After copy, str is "Hello" After move, str is "" The contents of the vector are "Hello", "Hello"
[edit] Complexity
Constant
[edit] See also
|
forwards a function argument (function template) |
||
|
obtains an rvalue reference if the move constructor does not throw (function template) |