Skip to content

Commit d5729cb

Browse files
committed
feat: add std::move page
1 parent dbc48b0 commit d5729cb

File tree

6 files changed

+207
-29
lines changed

6 files changed

+207
-29
lines changed

src/components/decl-doc/Decl.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
border: none;
2222
background-color: transparent;
2323
font-size: 1rem;
24-
line-height: 1.2;
2524
}
2625

2726
.decl .expressive-code pre code .ec-line .code {
2827
padding: 0;
28+
line-height: 1.2;
2929
}
3030

3131
.decl .expressive-code .copy {

src/components/desc-list/Desc.astro

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,3 @@ const hasKind = kind !== undefined;
3838
margin: 0;
3939
}
4040
</style>
41-
42-
<style is:global>
43-
.desc-items .revision-tag {
44-
display: block;
45-
}
46-
</style>

src/components/revision/RevisionBlock.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const vertical = dir === "v";
2828
<style>
2929
.revision {
3030
display: flex;
31-
align-items: center;
3231
}
3332

3433
.revision:not(.noborder) {
@@ -44,6 +43,7 @@ const vertical = dir === "v";
4443
}
4544

4645
.revision.horizontal {
46+
align-items: center;
4747
column-gap: 1em;
4848
}
4949

src/content/docs/c/library/headers/index.mdx

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
FeatureTestMacroValue,
1313
} from "@components/feature-test-macro";
1414
import Missing from "@components/Missing.astro";
15-
import { Revision } from "@components/revision";
15+
import { Revision, RevisionBlock } from "@components/revision";
1616

1717
The interface of C standard library is defined by the following collection of headers.
1818

@@ -23,7 +23,9 @@ The interface of C standard library is defined by the following collection of he
2323
</Desc>
2424

2525
<Desc>
26-
<Revision slot="item" noborder since="C99"><CHeader name="complex" /></Revision>
26+
<RevisionBlock slot="item" noborder since="C99" dir="v">
27+
<CHeader name="complex" />
28+
</RevisionBlock>
2729
Conditionally compiled macro that compares its argument to zero
2830
</Desc>
2931

@@ -38,7 +40,9 @@ The interface of C standard library is defined by the following collection of he
3840
</Desc>
3941

4042
<Desc>
41-
<Revision slot="item" noborder since="C99"><CHeader name="fenv" /></Revision>
43+
<RevisionBlock slot="item" noborder since="C99" dir="v">
44+
<CHeader name="fenv" />
45+
</RevisionBlock>
4246
Floating-point environment
4347
</Desc>
4448

@@ -48,12 +52,16 @@ The interface of C standard library is defined by the following collection of he
4852
</Desc>
4953

5054
<Desc>
51-
<Revision slot="item" noborder since="C99"><CHeader name="inttypes" /></Revision>
55+
<RevisionBlock slot="item" noborder since="C99" dir="v">
56+
<CHeader name="inttypes" />
57+
</RevisionBlock>
5258
Format conversion of integer types
5359
</Desc>
5460

5561
<Desc>
56-
<Revision slot="item" noborder since="C95"><CHeader name="iso646" /></Revision>
62+
<RevisionBlock slot="item" noborder since="C95" dir="v">
63+
<CHeader name="iso646" />
64+
</RevisionBlock>
5765
Alternative operator spellings
5866
</Desc>
5967

@@ -83,14 +91,15 @@ The interface of C standard library is defined by the following collection of he
8391
</Desc>
8492

8593
<Desc>
86-
<Revision
94+
<RevisionBlock
8795
slot="item"
8896
noborder
8997
since="C11"
98+
dir="v"
9099
traits={[ { trait: "deprecated", since: "C23" } ]}
91100
>
92101
<CHeader name="signal" />
93-
</Revision>
102+
</RevisionBlock>
94103
<Missing>alignas</Missing> and <Missing>alignof</Missing> convenience macros
95104
</Desc>
96105

@@ -100,29 +109,36 @@ The interface of C standard library is defined by the following collection of he
100109
</Desc>
101110

102111
<Desc>
103-
<Revision slot="item" noborder since="C11"><CHeader name="stdatomic" /></Revision>
112+
<RevisionBlock slot="item" noborder since="C11" dir="v">
113+
<CHeader name="stdatomic" />
114+
</RevisionBlock>
104115
Atomic operations
105116
</Desc>
106117

107118
<Desc>
108-
<Revision slot="item" noborder since="C23"><CHeader name="stdbit" /></Revision>
119+
<RevisionBlock slot="item" noborder since="C23" dir="v">
120+
<CHeader name="stdbit" />
121+
</RevisionBlock>
109122
Macros to work with the byte and bit representations of types
110123
</Desc>
111124

112125
<Desc>
113-
<Revision
126+
<RevisionBlock
114127
slot="item"
115128
noborder
116129
since="C99"
130+
dir="v"
117131
traits={[ { trait: "deprecated", since: "C23" } ]}
118132
>
119133
<CHeader name="stdbool" />
120-
</Revision>
134+
</RevisionBlock>
121135
Macros for boolean type
122136
</Desc>
123137

124138
<Desc>
125-
<Revision slot="item" noborder since="C23"><CHeader name="stdckdint" /></Revision>
139+
<RevisionBlock slot="item" noborder since="C23" dir="v">
140+
<CHeader name="stdckdint" />
141+
</RevisionBlock>
126142
Macros for performing checked integer arithmetic
127143
</Desc>
128144

@@ -132,7 +148,9 @@ The interface of C standard library is defined by the following collection of he
132148
</Desc>
133149

134150
<Desc>
135-
<Revision slot="item" noborder since="C99"><CHeader name="stdint" /></Revision>
151+
<RevisionBlock slot="item" noborder since="C99" dir="v">
152+
<CHeader name="stdint" />
153+
</RevisionBlock>
136154
Fixed-width integer types
137155
</Desc>
138156

@@ -147,19 +165,22 @@ The interface of C standard library is defined by the following collection of he
147165
</Desc>
148166

149167
<Desc>
150-
<Revision slot="item" noborder since="C29"><CHeader name="stdmchar" /></Revision>
168+
<RevisionBlock slot="item" noborder since="C29" dir="v">
169+
<CHeader name="stdmchar" />
170+
</RevisionBlock>
151171
Text transcode
152172
</Desc>
153173

154174
<Desc>
155-
<Revision
175+
<RevisionBlock
156176
slot="item"
157177
noborder
158178
since="C11"
179+
dir="v"
159180
traits={[ { trait: "deprecated", since: "C23" } ]}
160181
>
161182
<CHeader name="stdnoreturn" />
162-
</Revision>
183+
</RevisionBlock>
163184
<Missing>noreturn</Missing> convenience macro
164185
</Desc>
165186

@@ -169,12 +190,16 @@ The interface of C standard library is defined by the following collection of he
169190
</Desc>
170191

171192
<Desc>
172-
<Revision slot="item" noborder since="C99"><CHeader name="tgmath" /></Revision>
193+
<RevisionBlock slot="item" noborder since="C99" dir="v">
194+
<CHeader name="tgmath" />
195+
</RevisionBlock>
173196
Type-generic math (macros wrapping <CHeader name="math" /> and <CHeader name="complex" />)
174197
</Desc>
175198

176199
<Desc>
177-
<Revision slot="item" noborder since="C11"><CHeader name="threads" /></Revision>
200+
<RevisionBlock slot="item" noborder since="C11" dir="v">
201+
<CHeader name="threads" />
202+
</RevisionBlock>
178203
Thread library
179204
</Desc>
180205

@@ -184,17 +209,23 @@ The interface of C standard library is defined by the following collection of he
184209
</Desc>
185210

186211
<Desc>
187-
<Revision slot="item" noborder since="C11"><CHeader name="uchar" /></Revision>
212+
<RevisionBlock slot="item" noborder since="C11" dir="v">
213+
<CHeader name="uchar" />
214+
</RevisionBlock>
188215
UTF-16 and UTF-32 character utilities
189216
</Desc>
190217

191218
<Desc>
192-
<Revision slot="item" noborder since="C95"><CHeader name="wchar" /></Revision>
219+
<RevisionBlock slot="item" noborder since="C95" dir="v">
220+
<CHeader name="wchar" />
221+
</RevisionBlock>
193222
Extended multibyte and wide character utilities
194223
</Desc>
195224

196225
<Desc>
197-
<Revision slot="item" noborder since="C95"><CHeader name="wctype" /></Revision>
226+
<RevisionBlock slot="item" noborder since="C95" dir="v">
227+
<CHeader name="wctype" />
228+
</RevisionBlock>
198229
Functions to determine the type contained in wide character data
199230
</Desc>
200231
</DescList>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
label: Utilities library
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
title: std::move
3+
cppdoc:
4+
keys: ["cpp.library.utilities.move"]
5+
---
6+
7+
import { CppHeader } from "@components/header";
8+
import { Decl, DeclDoc } from "@components/decl-doc";
9+
import { Desc, DescList } from "@components/desc-list";
10+
import Missing from "@components/Missing.astro";
11+
import { ParamDoc, ParamDocList } from "@components/param-doc";
12+
import { Revision, RevisionBlock } from "@components/revision";
13+
14+
Defined in header <CppHeader name="utility" />.
15+
16+
<DeclDoc>
17+
<Decl slot="decl">
18+
<RevisionBlock since="C++11" until="C++14" noborder>
19+
```cpp
20+
template <typename T>
21+
typename std::remove_reference<T>::type&& move(T&& t) noexcept;
22+
```
23+
</RevisionBlock>
24+
</Decl>
25+
<Decl slot="decl">
26+
<RevisionBlock since="C++14" noborder>
27+
```cpp
28+
template <class T>
29+
constexpr std::remove_reference_t<T>&& move(T&& t) noexcept;
30+
```
31+
</RevisionBlock>
32+
</Decl>
33+
34+
`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.
37+
</DeclDoc>
38+
39+
## Parameters
40+
41+
<ParamDocList>
42+
<ParamDoc name="t">
43+
the object to be moved
44+
</ParamDoc>
45+
</ParamDocList>
46+
47+
## Return value
48+
49+
`static_cast<typename std::remove_reference<T>::type&&>(t)`
50+
51+
## Notes
52+
53+
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
105+
// we'll incur the cost of copying str
106+
v.push_back(str);
107+
std::cout << "After copy, str is " << std::quoted(str) << '\n';
108+
109+
// uses the rvalue reference push_back(T&&) overload,
110+
// which means no strings will be copied; instead, the contents
111+
// of str will be moved into the vector. This is less
112+
// expensive, but also means str might now be empty.
113+
v.push_back(std::move(str));
114+
std::cout << "After move, str is " << std::quoted(str) << '\n';
115+
116+
std::cout << "The contents of the vector are {" << std::quoted(v[0])
117+
<< ", " << std::quoted(v[1]) << "}\n";
118+
}
119+
```
120+
121+
Possible output:
122+
123+
```
124+
After copy, str is "Salut"
125+
After move, str is ""
126+
The contents of the vector are {"Salut", "Salut"}
127+
```
128+
129+
## See also
130+
131+
<DescList>
132+
<Desc kind="function template">
133+
<Fragment slot="item">
134+
<RevisionBlock noborder since="C++11" dir="v"><Missing>`forward`</Missing></RevisionBlock>
135+
</Fragment>
136+
forwards a function argument and use the type template argument to preserve its value category
137+
</Desc>
138+
139+
<Desc kind="function template">
140+
<Fragment slot="item">
141+
<RevisionBlock noborder since="C++11" dir="v"><Missing>`move_if_noexcept`</Missing></RevisionBlock>
142+
</Fragment>
143+
converts the argument to an xvalue if the move constructor does not throw
144+
</Desc>
145+
146+
<Desc kind="function template">
147+
<Fragment slot="item">
148+
<RevisionBlock noborder since="C++11" dir="v"><Missing>`move`</Missing></RevisionBlock>
149+
</Fragment>
150+
moves a range of elements to a new location
151+
</Desc>
152+
</DescList>

0 commit comments

Comments
 (0)