From Jason Turner

[utility]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmps27t5hpw/{from.md → to.md} +130 -19
tmp/tmps27t5hpw/{from.md → to.md} RENAMED
@@ -4,10 +4,11 @@
4
 
5
  The header `<utility>` contains some basic function and class templates
6
  that are used throughout the rest of the library.
7
 
8
  ``` cpp
 
9
  #include <compare> // see [compare.syn]
10
  #include <initializer_list> // see [initializer.list.syn]
11
 
12
  namespace std {
13
  // [utility.swap], swap
@@ -16,17 +17,19 @@ namespace std {
16
  template<class T, size_t N>
17
  constexpr void swap(T (&a)[N], T (&b)[N]) noexcept(is_nothrow_swappable_v<T>);
18
 
19
  // [utility.exchange], exchange
20
  template<class T, class U = T>
21
- constexpr T exchange(T& obj, U&& new_val);
22
 
23
  // [forward], forward/move
24
  template<class T>
25
  constexpr T&& forward(remove_reference_t<T>& t) noexcept;
26
  template<class T>
27
  constexpr T&& forward(remove_reference_t<T>&& t) noexcept;
 
 
28
  template<class T>
29
  constexpr remove_reference_t<T>&& move(T&&) noexcept;
30
  template<class T>
31
  constexpr conditional_t<
32
  !is_nothrow_move_constructible_v<T> && is_copy_constructible_v<T>, const T&, T&&>
@@ -58,10 +61,17 @@ namespace std {
58
  constexpr bool cmp_greater_equal(T t, U u) noexcept;
59
 
60
  template<class R, class T>
61
  constexpr bool in_range(T t) noexcept;
62
 
 
 
 
 
 
 
 
63
  // [intseq], compile-time integer sequences%
64
  %
65
  %
66
 
67
  template<class T, T...>
@@ -79,20 +89,38 @@ namespace std {
79
 
80
  // [pairs], class template pair
81
  template<class T1, class T2>
82
  struct pair;
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  // [pairs.spec], pair specialized algorithms
85
- template<class T1, class T2>
86
- constexpr bool operator==(const pair<T1, T2>&, const pair<T1, T2>&);
87
- template<class T1, class T2>
88
- constexpr common_comparison_category_t<synth-three-way-result<T1>,
89
- synth-three-way-result<T2>>
90
- operator<=>(const pair<T1, T2>&, const pair<T1, T2>&);
91
 
92
  template<class T1, class T2>
93
  constexpr void swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));
 
 
 
94
 
95
  template<class T1, class T2>
96
  constexpr see below make_pair(T1&&, T2&&);
97
 
98
  // [pair.astuple], tuple-like access to pair
@@ -148,17 +176,17 @@ namespace std {
148
 
149
  template<class T>
150
  struct in_place_type_t {
151
  explicit in_place_type_t() = default;
152
  };
153
- template<class T> inline constexpr in_place_type_t<T> in_place_type{};
154
 
155
  template<size_t I>
156
  struct in_place_index_t {
157
  explicit in_place_index_t() = default;
158
  };
159
- template<size_t I> inline constexpr in_place_index_t<I> in_place_index{};
160
  }
161
  ```
162
 
163
  ### `swap` <a id="utility.swap">[[utility.swap]]</a>
164
 
@@ -174,13 +202,11 @@ template<class T>
174
  ([[cpp17.moveconstructible]]) and *Cpp17MoveAssignable*
175
  ([[cpp17.moveassignable]]) requirements.
176
 
177
  *Effects:* Exchanges values stored in two locations.
178
 
179
- *Remarks:* This function is a designated customization
180
- point [[namespace.std]]. The expression inside `noexcept` is equivalent
181
- to:
182
 
183
  ``` cpp
184
  is_nothrow_move_constructible_v<T> && is_nothrow_move_assignable_v<T>
185
  ```
186
 
@@ -198,21 +224,27 @@ template<class T, size_t N>
198
 
199
  ### `exchange` <a id="utility.exchange">[[utility.exchange]]</a>
200
 
201
  ``` cpp
202
  template<class T, class U = T>
203
- constexpr T exchange(T& obj, U&& new_val);
204
  ```
205
 
206
  *Effects:* Equivalent to:
207
 
208
  ``` cpp
209
  T old_val = std::move(obj);
210
  obj = std::forward<U>(new_val);
211
  return old_val;
212
  ```
213
 
 
 
 
 
 
 
214
  ### Forward/move helpers <a id="forward">[[forward]]</a>
215
 
216
  The library provides templated helper functions to simplify applying
217
  move semantics to an lvalue and to simplify the implementation of
218
  forwarding functions. All functions specified in this subclause are
@@ -253,17 +285,58 @@ forwarded to `A`’s constructor as an rvalue. In the second call to
253
  constructor as an lvalue. In both cases, `A2` is deduced as `double`, so
254
  1.414 is forwarded to `A`’s constructor as an rvalue.
255
 
256
  — *end example*]
257
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
  ``` cpp
259
  template<class T> constexpr remove_reference_t<T>&& move(T&& t) noexcept;
260
  ```
261
 
262
  *Returns:* `static_cast<remove_reference_t<T>&&>(t)`.
263
 
264
- [*Example 2*:
265
 
266
  ``` cpp
267
  template<class T, class A1>
268
  shared_ptr<T> factory(A1&& a1) {
269
  return shared_ptr<T>(new T(std::forward<A1>(a1)));
@@ -276,11 +349,11 @@ struct A {
276
  };
277
 
278
  void g() {
279
  A a;
280
  shared_ptr<A> sp1 = factory<A>(a); // ``a'' binds to A(const A&)
281
- shared_ptr<A> sp1 = factory<A>(std::move(a)); // ``a'' binds to A(A&&)
282
  }
283
  ```
284
 
285
  In the first call to `factory`, `A1` is deduced as `A&`, so `a` is
286
  forwarded as a non-const lvalue. This binds to the constructor
@@ -309,17 +382,17 @@ template<class T> constexpr add_const_t<T>& as_const(T& t) noexcept;
309
 
310
  ### Function template `declval` <a id="declval">[[declval]]</a>
311
 
312
  The library provides the function template `declval` to simplify the
313
  definition of expressions which occur as unevaluated operands
314
- [[expr.prop]].
315
 
316
  ``` cpp
317
  template<class T> add_rvalue_reference_t<T> declval() noexcept; // as unevaluated operand
318
  ```
319
 
320
- *Mandates:* This function is not odr-used [[basic.def.odr]].
321
 
322
  *Remarks:* The template parameter `T` of `declval` may be an incomplete
323
  type.
324
 
325
  [*Example 1*:
@@ -327,12 +400,12 @@ type.
327
  ``` cpp
328
  template<class To, class From> decltype(static_cast<To>(declval<From>())) convert(From&&);
329
  ```
330
 
331
  declares a function template `convert` which only participates in
332
- overloading if the type `From` can be explicitly converted to type `To`.
333
- For another example see class template `common_type`
334
  [[meta.trans.other]].
335
 
336
  — *end example*]
337
 
338
  ### Integer comparison functions <a id="utility.intcmp">[[utility.intcmp]]</a>
@@ -424,5 +497,43 @@ return cmp_greater_equal(t, numeric_limits<R>::min()) &&
424
 
425
  [*Note 1*: These function templates cannot be used to compare `byte`,
426
  `char`, `char8_t`, `char16_t`, `char32_t`, `wchar_t`, and
427
  `bool`. — *end note*]
428
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  The header `<utility>` contains some basic function and class templates
6
  that are used throughout the rest of the library.
7
 
8
  ``` cpp
9
+ // all freestanding
10
  #include <compare> // see [compare.syn]
11
  #include <initializer_list> // see [initializer.list.syn]
12
 
13
  namespace std {
14
  // [utility.swap], swap
 
17
  template<class T, size_t N>
18
  constexpr void swap(T (&a)[N], T (&b)[N]) noexcept(is_nothrow_swappable_v<T>);
19
 
20
  // [utility.exchange], exchange
21
  template<class T, class U = T>
22
+ constexpr T exchange(T& obj, U&& new_val) noexcept(see below);
23
 
24
  // [forward], forward/move
25
  template<class T>
26
  constexpr T&& forward(remove_reference_t<T>& t) noexcept;
27
  template<class T>
28
  constexpr T&& forward(remove_reference_t<T>&& t) noexcept;
29
+ template<class T, class U>
30
+ [[nodiscard]] constexpr auto forward_like(U&& x) noexcept -> see below;
31
  template<class T>
32
  constexpr remove_reference_t<T>&& move(T&&) noexcept;
33
  template<class T>
34
  constexpr conditional_t<
35
  !is_nothrow_move_constructible_v<T> && is_copy_constructible_v<T>, const T&, T&&>
 
61
  constexpr bool cmp_greater_equal(T t, U u) noexcept;
62
 
63
  template<class R, class T>
64
  constexpr bool in_range(T t) noexcept;
65
 
66
+ // [utility.underlying], to_underlying
67
+ template<class T>
68
+ constexpr underlying_type_t<T> to_underlying(T value) noexcept;
69
+
70
+ // [utility.unreachable], unreachable
71
+ [[noreturn]] void unreachable();
72
+
73
  // [intseq], compile-time integer sequences%
74
  %
75
  %
76
 
77
  template<class T, T...>
 
89
 
90
  // [pairs], class template pair
91
  template<class T1, class T2>
92
  struct pair;
93
 
94
+ template<class T1, class T2, class U1, class U2,
95
+ template<class> class TQual, template<class> class UQual>
96
+ requires requires { typename pair<common_reference_t<TQual<T1>, UQual<U1>>,
97
+ common_reference_t<TQual<T2>, UQual<U2>>>; }
98
+ struct basic_common_reference<pair<T1, T2>, pair<U1, U2>, TQual, UQual> {
99
+ using type = pair<common_reference_t<TQual<T1>, UQual<U1>>,
100
+ common_reference_t<TQual<T2>, UQual<U2>>>;
101
+ };
102
+
103
+ template<class T1, class T2, class U1, class U2>
104
+ requires requires { typename pair<common_type_t<T1, U1>, common_type_t<T2, U2>>; }
105
+ struct common_type<pair<T1, T2>, pair<U1, U2>> {
106
+ using type = pair<common_type_t<T1, U1>, common_type_t<T2, U2>>;
107
+ };
108
+
109
  // [pairs.spec], pair specialized algorithms
110
+ template<class T1, class T2, class U1, class U2>
111
+ constexpr bool operator==(const pair<T1, T2>&, const pair<U1, U2>&);
112
+ template<class T1, class T2, class U1, class U2>
113
+ constexpr common_comparison_category_t<synth-three-way-result<T1, U1>,
114
+ synth-three-way-result<T2, U2>>
115
+ operator<=>(const pair<T1, T2>&, const pair<U1, U2>&);
116
 
117
  template<class T1, class T2>
118
  constexpr void swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));
119
+ template<class T1, class T2>
120
+ constexpr void swap(const pair<T1, T2>& x, const pair<T1, T2>& y)
121
+ noexcept(noexcept(x.swap(y)));
122
 
123
  template<class T1, class T2>
124
  constexpr see below make_pair(T1&&, T2&&);
125
 
126
  // [pair.astuple], tuple-like access to pair
 
176
 
177
  template<class T>
178
  struct in_place_type_t {
179
  explicit in_place_type_t() = default;
180
  };
181
+ template<class T> constexpr in_place_type_t<T> in_place_type{};
182
 
183
  template<size_t I>
184
  struct in_place_index_t {
185
  explicit in_place_index_t() = default;
186
  };
187
+ template<size_t I> constexpr in_place_index_t<I> in_place_index{};
188
  }
189
  ```
190
 
191
  ### `swap` <a id="utility.swap">[[utility.swap]]</a>
192
 
 
202
  ([[cpp17.moveconstructible]]) and *Cpp17MoveAssignable*
203
  ([[cpp17.moveassignable]]) requirements.
204
 
205
  *Effects:* Exchanges values stored in two locations.
206
 
207
+ *Remarks:* The exception specification is equivalent to:
 
 
208
 
209
  ``` cpp
210
  is_nothrow_move_constructible_v<T> && is_nothrow_move_assignable_v<T>
211
  ```
212
 
 
224
 
225
  ### `exchange` <a id="utility.exchange">[[utility.exchange]]</a>
226
 
227
  ``` cpp
228
  template<class T, class U = T>
229
+ constexpr T exchange(T& obj, U&& new_val) noexcept(see below);
230
  ```
231
 
232
  *Effects:* Equivalent to:
233
 
234
  ``` cpp
235
  T old_val = std::move(obj);
236
  obj = std::forward<U>(new_val);
237
  return old_val;
238
  ```
239
 
240
+ *Remarks:* The exception specification is equivalent to:
241
+
242
+ ``` cpp
243
+ is_nothrow_move_constructible_v<T> && is_nothrow_assignable_v<T&, U>
244
+ ```
245
+
246
  ### Forward/move helpers <a id="forward">[[forward]]</a>
247
 
248
  The library provides templated helper functions to simplify applying
249
  move semantics to an lvalue and to simplify the implementation of
250
  forwarding functions. All functions specified in this subclause are
 
285
  constructor as an lvalue. In both cases, `A2` is deduced as `double`, so
286
  1.414 is forwarded to `A`’s constructor as an rvalue.
287
 
288
  — *end example*]
289
 
290
+ ``` cpp
291
+ template<class T, class U>
292
+ [[nodiscard]] constexpr auto forward_like(U&& x) noexcept -> see below;
293
+ ```
294
+
295
+ *Mandates:* `T` is a referenceable type [[defns.referenceable]].
296
+
297
+ - Let *`COPY_CONST`*`(A, B)` be `const B` if `A` is a const type,
298
+ otherwise `B`.
299
+ - Let *`OVERRIDE_REF`*`(A, B)` be `remove_reference_t<B>&&` if `A` is an
300
+ rvalue reference type, otherwise `B&`.
301
+ - Let `V` be
302
+ ``` cpp
303
+ OVERRIDE_REF(T&&, COPY_CONST(remove_reference_t<T>, remove_reference_t<U>))
304
+ ```
305
+
306
+ *Returns:* `static_cast<V>(x)`.
307
+
308
+ *Remarks:* The return type is `V`.
309
+
310
+ [*Example 2*:
311
+
312
+ ``` cpp
313
+ struct accessor {
314
+ vector<string>* container;
315
+ decltype(auto) operator[](this auto&& self, size_t i) {
316
+ return std::forward_like<decltype(self)>((*container)[i]);
317
+ }
318
+ };
319
+ void g() {
320
+ vector v{"a"s, "b"s};
321
+ accessor a{&v};
322
+ string& x = a[0]; // OK, binds to lvalue reference
323
+ string&& y = std::move(a)[0]; // OK, is rvalue reference
324
+ string const&& z = std::move(as_const(a))[1]; // OK, is const&&
325
+ string& w = as_const(a)[1]; // error: will not bind to non-const
326
+ }
327
+ ```
328
+
329
+ — *end example*]
330
+
331
  ``` cpp
332
  template<class T> constexpr remove_reference_t<T>&& move(T&& t) noexcept;
333
  ```
334
 
335
  *Returns:* `static_cast<remove_reference_t<T>&&>(t)`.
336
 
337
+ [*Example 3*:
338
 
339
  ``` cpp
340
  template<class T, class A1>
341
  shared_ptr<T> factory(A1&& a1) {
342
  return shared_ptr<T>(new T(std::forward<A1>(a1)));
 
349
  };
350
 
351
  void g() {
352
  A a;
353
  shared_ptr<A> sp1 = factory<A>(a); // ``a'' binds to A(const A&)
354
+ shared_ptr<A> sp2 = factory<A>(std::move(a)); // ``a'' binds to A(A&&)
355
  }
356
  ```
357
 
358
  In the first call to `factory`, `A1` is deduced as `A&`, so `a` is
359
  forwarded as a non-const lvalue. This binds to the constructor
 
382
 
383
  ### Function template `declval` <a id="declval">[[declval]]</a>
384
 
385
  The library provides the function template `declval` to simplify the
386
  definition of expressions which occur as unevaluated operands
387
+ [[term.unevaluated.operand]].
388
 
389
  ``` cpp
390
  template<class T> add_rvalue_reference_t<T> declval() noexcept; // as unevaluated operand
391
  ```
392
 
393
+ *Mandates:* This function is not odr-used [[term.odr.use]].
394
 
395
  *Remarks:* The template parameter `T` of `declval` may be an incomplete
396
  type.
397
 
398
  [*Example 1*:
 
400
  ``` cpp
401
  template<class To, class From> decltype(static_cast<To>(declval<From>())) convert(From&&);
402
  ```
403
 
404
  declares a function template `convert` which only participates in
405
+ overload resolution if the type `From` can be explicitly converted to
406
+ type `To`. For another example see class template `common_type`
407
  [[meta.trans.other]].
408
 
409
  — *end example*]
410
 
411
  ### Integer comparison functions <a id="utility.intcmp">[[utility.intcmp]]</a>
 
497
 
498
  [*Note 1*: These function templates cannot be used to compare `byte`,
499
  `char`, `char8_t`, `char16_t`, `char32_t`, `wchar_t`, and
500
  `bool`. — *end note*]
501
 
502
+ ### Function template `to_underlying` <a id="utility.underlying">[[utility.underlying]]</a>
503
+
504
+ ``` cpp
505
+ template<class T>
506
+ constexpr underlying_type_t<T> to_underlying(T value) noexcept;
507
+ ```
508
+
509
+ *Returns:* `static_cast<underlying_type_t<T>>(value)`.
510
+
511
+ ### Function `unreachable` <a id="utility.unreachable">[[utility.unreachable]]</a>
512
+
513
+ ``` cpp
514
+ [[noreturn]] void unreachable();
515
+ ```
516
+
517
+ *Preconditions:* `false` is `true`.
518
+
519
+ [*Note 1*: This precondition cannot be satisfied, thus the behavior of
520
+ calling `unreachable` is undefined. — *end note*]
521
+
522
+ [*Example 1*:
523
+
524
+ ``` cpp
525
+ int f(int x) {
526
+ switch (x) {
527
+ case 0:
528
+ case 1:
529
+ return x;
530
+ default:
531
+ std::unreachable();
532
+ }
533
+ }
534
+ int a = f(1); // OK, a has value 1
535
+ int b = f(3); // undefined behavior
536
+ ```
537
+
538
+ — *end example*]
539
+