template <class T>typename remove_reference<T>::type&& move (T&& arg) noexcept;
1
static_cast<remove_reference<decltype(arg)>::type&&>(arg)
<algorithm> overloads this function, providing a similar behavior applied to ranges.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// move example
#include <utility> // std::move
#include <iostream> // std::cout
#include <vector> // std::vector
#include <string> // std::string
int main () {
std::string foo = "foo-string";
std::string bar = "bar-string";
std::vector<std::string> myvector;
myvector.push_back (foo); // copies
myvector.push_back (std::move(bar)); // moves
std::cout << "myvector contains:";
for (std::string& x:myvector) std::cout << ' ' << x;
std::cout << '\n';
return 0;
}
myvector.push_back copies the value of foo into the vector (foo keeps the value it had before the call).myvector contains: foo-string bar-string