You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`std::move` is used to _indicate_ that an object `t` may be "moved from", i.e. allowing the efficient transfer of resources from `t` to another object.
35
+
36
+
In particular, `std::move` produces an <Missing>xvalue expression</Missing> that identifies its argument `t`. It is exactly equivalent to a `static_cast` to an rvalue reference type.
The functions that accept rvalue reference parameters (including <Missing>move constructors</Missing>, <Missing>move assignment operators</Missing>, and regular member functions such as <Missing>`std::vector::push_back`</Missing>) are selected, by <Missing>overload resolution</Missing>, when called with <Missing>rvalue</Missing> arguments (either <Missing>prvalues</Missing> such as a temporary object or <Missing>xvalues</Missing> 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.
54
+
55
+
Names of <Missing>rvalue reference</Missing> variables are <Missing>lvalues</Missing> and have to be converted to <Missing>xvalues</Missing> to be bound to the function overloads that accept rvalue reference parameters, which is why <Missing>move constructors</Missing> and <Missing>move assignment operators</Missing> typically use `std::move`:
56
+
57
+
```cpp
58
+
// Simple move constructor
59
+
// the expression "arg.member" is lvalue
60
+
A(A&& arg) : member(std::move(arg.member)) {}
61
+
62
+
// Simple move assignment operator
63
+
A& operator=(A&& other) {
64
+
member=std::move(other.member);
65
+
return*this;
66
+
}
67
+
```
68
+
69
+
One exception is when the type of the function parameter is a <Missing>forwarding reference</Missing> (which looks like an rvalue reference to type template parameter), in which case <Missing>`std::forward`</Missing> is used instead.
70
+
71
+
Unless otherwise specified, all standard library objects that have been moved from are placed in a "valid but unspecified state", meaning the object's class invariants hold (so functions without preconditions, such as the assignment operator, can be safely used on the object after it was moved from):
72
+
73
+
```cpp
74
+
std::vector<std::string> v;
75
+
std::string str = "example";
76
+
v.push_back(std::move(str)); // str is now valid but unspecified
77
+
str.back(); // undefined behavior if size() == 0: back() has a precondition !empty()
78
+
if (!str.empty())
79
+
str.back(); // OK, empty() has no precondition and back() precondition is met
80
+
81
+
str.clear(); // OK, clear() has no preconditions
82
+
```
83
+
84
+
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. However, self-move-assignment of standard library types is guaranteed to place the object in a valid (but usually unspecified) state:
85
+
86
+
```cpp
87
+
std::vector<int> v = {2, 3, 3};
88
+
v = std::move(v); // the value of v is unspecified
89
+
```
90
+
91
+
## Example
92
+
93
+
```cpp
94
+
#include <iomanip>
95
+
#include <iostream>
96
+
#include <string>
97
+
#include <utility>
98
+
#include <vector>
99
+
100
+
int main() {
101
+
std::string str = "Salut";
102
+
std::vector<std::string> v;
103
+
104
+
// uses the push_back(const T&) overload, which means
0 commit comments