From Jason Turner

[variant]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp54jghcrp/{from.md → to.md} +1014 -0
tmp/tmp54jghcrp/{from.md → to.md} RENAMED
@@ -0,0 +1,1014 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Variants <a id="variant">[[variant]]</a>
2
+
3
+ ### In general <a id="variant.general">[[variant.general]]</a>
4
+
5
+ A variant object holds and manages the lifetime of a value. If the
6
+ `variant` holds a value, that value’s type has to be one of the template
7
+ argument types given to variant. These template arguments are called
8
+ alternatives.
9
+
10
+ ### Header `<variant>` synopsis <a id="variant.syn">[[variant.syn]]</a>
11
+
12
+ ``` cpp
13
+ namespace std {
14
+ // [variant.variant], class template variant
15
+ template <class... Types>
16
+ class variant;
17
+
18
+ // [variant.helper], variant helper classes
19
+ template <class T> struct variant_size; // not defined
20
+ template <class T> struct variant_size<const T>;
21
+ template <class T> struct variant_size<volatile T>;
22
+ template <class T> struct variant_size<const volatile T>;
23
+ template <class T>
24
+ inline constexpr size_t variant_size_v = variant_size<T>::value;
25
+
26
+ template <class... Types>
27
+ struct variant_size<variant<Types...>>;
28
+
29
+ template <size_t I, class T> struct variant_alternative; // not defined
30
+ template <size_t I, class T> struct variant_alternative<I, const T>;
31
+ template <size_t I, class T> struct variant_alternative<I, volatile T>;
32
+ template <size_t I, class T> struct variant_alternative<I, const volatile T>;
33
+ template <size_t I, class T>
34
+ using variant_alternative_t = typename variant_alternative<I, T>::type;
35
+
36
+ template <size_t I, class... Types>
37
+ struct variant_alternative<I, variant<Types...>>;
38
+
39
+ inline constexpr size_t variant_npos = -1;
40
+
41
+ // [variant.get], value access
42
+ template <class T, class... Types>
43
+ constexpr bool holds_alternative(const variant<Types...>&) noexcept;
44
+
45
+ template <size_t I, class... Types>
46
+ constexpr variant_alternative_t<I, variant<Types...>>& get(variant<Types...>&);
47
+ template <size_t I, class... Types>
48
+ constexpr variant_alternative_t<I, variant<Types...>>&& get(variant<Types...>&&);
49
+ template <size_t I, class... Types>
50
+ constexpr const variant_alternative_t<I, variant<Types...>>& get(const variant<Types...>&);
51
+ template <size_t I, class... Types>
52
+ constexpr const variant_alternative_t<I, variant<Types...>>&& get(const variant<Types...>&&);
53
+
54
+ template <class T, class... Types>
55
+ constexpr T& get(variant<Types...>&);
56
+ template <class T, class... Types>
57
+ constexpr T&& get(variant<Types...>&&);
58
+ template <class T, class... Types>
59
+ constexpr const T& get(const variant<Types...>&);
60
+ template <class T, class... Types>
61
+ constexpr const T&& get(const variant<Types...>&&);
62
+
63
+ template <size_t I, class... Types>
64
+ constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
65
+ get_if(variant<Types...>*) noexcept;
66
+ template <size_t I, class... Types>
67
+ constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
68
+ get_if(const variant<Types...>*) noexcept;
69
+
70
+ template <class T, class... Types>
71
+ constexpr add_pointer_t<T>
72
+ get_if(variant<Types...>*) noexcept;
73
+ template <class T, class... Types>
74
+ constexpr add_pointer_t<const T>
75
+ get_if(const variant<Types...>*) noexcept;
76
+
77
+ // [variant.relops], relational operators
78
+ template <class... Types>
79
+ constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
80
+ template <class... Types>
81
+ constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
82
+ template <class... Types>
83
+ constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
84
+ template <class... Types>
85
+ constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
86
+ template <class... Types>
87
+ constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
88
+ template <class... Types>
89
+ constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
90
+
91
+ // [variant.visit], visitation
92
+ template <class Visitor, class... Variants>
93
+ constexpr see below visit(Visitor&&, Variants&&...);
94
+
95
+ // [variant.monostate], class monostate
96
+ struct monostate;
97
+
98
+ // [variant.monostate.relops], monostate relational operators
99
+ constexpr bool operator<(monostate, monostate) noexcept;
100
+ constexpr bool operator>(monostate, monostate) noexcept;
101
+ constexpr bool operator<=(monostate, monostate) noexcept;
102
+ constexpr bool operator>=(monostate, monostate) noexcept;
103
+ constexpr bool operator==(monostate, monostate) noexcept;
104
+ constexpr bool operator!=(monostate, monostate) noexcept;
105
+
106
+ // [variant.specalg], specialized algorithms
107
+ template <class... Types>
108
+ void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
109
+
110
+ // [variant.bad.access], class bad_variant_access
111
+ class bad_variant_access;
112
+
113
+ // [variant.hash], hash support
114
+ template <class T> struct hash;
115
+ template <class... Types> struct hash<variant<Types...>>;
116
+ template <> struct hash<monostate>;
117
+
118
+ // [variant.traits], allocator-related traits
119
+ template <class T, class Alloc> struct uses_allocator;
120
+ template <class... Types, class Alloc> struct uses_allocator<variant<Types...>, Alloc>;
121
+ }
122
+ ```
123
+
124
+ ### Class template `variant` <a id="variant.variant">[[variant.variant]]</a>
125
+
126
+ ``` cpp
127
+ namespace std {
128
+ template <class... Types>
129
+ class variant {
130
+ public:
131
+ // [variant.ctor], constructors
132
+ constexpr variant() noexcept(see below);
133
+ variant(const variant&);
134
+ variant(variant&&) noexcept(see below);
135
+
136
+ template <class T>
137
+ constexpr variant(T&&) noexcept(see below);
138
+
139
+ template <class T, class... Args>
140
+ constexpr explicit variant(in_place_type_t<T>, Args&&...);
141
+ template <class T, class U, class... Args>
142
+ constexpr explicit variant(in_place_type_t<T>, initializer_list<U>, Args&&...);
143
+
144
+ template <size_t I, class... Args>
145
+ constexpr explicit variant(in_place_index_t<I>, Args&&...);
146
+ template <size_t I, class U, class... Args>
147
+ constexpr explicit variant(in_place_index_t<I>, initializer_list<U>, Args&&...);
148
+
149
+ // allocator-extended constructors
150
+ template <class Alloc>
151
+ variant(allocator_arg_t, const Alloc&);
152
+ template <class Alloc>
153
+ variant(allocator_arg_t, const Alloc&, const variant&);
154
+ template <class Alloc>
155
+ variant(allocator_arg_t, const Alloc&, variant&&);
156
+ template <class Alloc, class T>
157
+ variant(allocator_arg_t, const Alloc&, T&&);
158
+ template <class Alloc, class T, class... Args>
159
+ variant(allocator_arg_t, const Alloc&, in_place_type_t<T>, Args&&...);
160
+ template <class Alloc, class T, class U, class... Args>
161
+ variant(allocator_arg_t, const Alloc&, in_place_type_t<T>,
162
+ initializer_list<U>, Args&&...);
163
+ template <class Alloc, size_t I, class... Args>
164
+ variant(allocator_arg_t, const Alloc&, in_place_index_t<I>, Args&&...);
165
+ template <class Alloc, size_t I, class U, class... Args>
166
+ variant(allocator_arg_t, const Alloc&, in_place_index_t<I>,
167
+ initializer_list<U>, Args&&...);
168
+
169
+ // [variant.dtor], destructor
170
+ ~variant();
171
+
172
+ // [variant.assign], assignment
173
+ variant& operator=(const variant&);
174
+ variant& operator=(variant&&) noexcept(see below);
175
+
176
+ template <class T> variant& operator=(T&&) noexcept(see below);
177
+
178
+ // [variant.mod], modifiers
179
+ template <class T, class... Args>
180
+ T& emplace(Args&&...);
181
+ template <class T, class U, class... Args>
182
+ T& emplace(initializer_list<U>, Args&&...);
183
+ template <size_t I, class... Args>
184
+ variant_alternative_t<I, variant<Types...>>& emplace(Args&&...);
185
+ template <size_t I, class U, class... Args>
186
+ variant_alternative_t<I, variant<Types...>>& emplace(initializer_list<U>, Args&&...);
187
+
188
+ // [variant.status], value status
189
+ constexpr bool valueless_by_exception() const noexcept;
190
+ constexpr size_t index() const noexcept;
191
+
192
+ // [variant.swap], swap
193
+ void swap(variant&) noexcept(see below);
194
+ };
195
+ }
196
+ ```
197
+
198
+ Any instance of `variant` at any given time either holds a value of one
199
+ of its alternative types, or it holds no value. When an instance of
200
+ `variant` holds a value of alternative type `T`, it means that a value
201
+ of type `T`, referred to as the `variant` object’s *contained value*, is
202
+ allocated within the storage of the `variant` object. Implementations
203
+ are not permitted to use additional storage, such as dynamic memory, to
204
+ allocate the contained value. The contained value shall be allocated in
205
+ a region of the `variant` storage suitably aligned for all types in
206
+ `Types...`. It is *implementation-defined* whether over-aligned types
207
+ are supported.
208
+
209
+ All types in `Types...` shall be (possibly cv-qualified) object types
210
+ that are not arrays.
211
+
212
+ A program that instantiates the definition of `variant` with no template
213
+ arguments is ill-formed.
214
+
215
+ #### Constructors <a id="variant.ctor">[[variant.ctor]]</a>
216
+
217
+ In the descriptions that follow, let i be in the range \[`0`,
218
+ `sizeof...(Types)`), and `Tᵢ` be the iᵗʰ type in `Types...`.
219
+
220
+ ``` cpp
221
+ constexpr variant() noexcept(see below);
222
+ ```
223
+
224
+ *Effects:* Constructs a `variant` holding a value-initialized value of
225
+ type `T₀`.
226
+
227
+ *Postconditions:* `valueless_by_exception()` is `false` and `index()` is
228
+ `0`.
229
+
230
+ *Throws:* Any exception thrown by the value-initialization of `T₀`.
231
+
232
+ *Remarks:* This function shall be `constexpr` if and only if the
233
+ value-initialization of the alternative type `T₀` would satisfy the
234
+ requirements for a constexpr function. The expression inside `noexcept`
235
+ is equivalent to `is_nothrow_default_constructible_v<``T₀``>`. This
236
+ function shall not participate in overload resolution unless
237
+ `is_default_constructible_v<``T₀``>` is `true`.
238
+
239
+ [*Note 1*: See also class `monostate`. — *end note*]
240
+
241
+ ``` cpp
242
+ variant(const variant& w);
243
+ ```
244
+
245
+ *Effects:* If `w` holds a value, initializes the `variant` to hold the
246
+ same alternative as `w` and direct-initializes the contained value with
247
+ `get<j>(w)`, where `j` is `w.index()`. Otherwise, initializes the
248
+ `variant` to not hold a value.
249
+
250
+ *Throws:* Any exception thrown by direct-initializing any `Tᵢ` for all
251
+ i.
252
+
253
+ *Remarks:* This function shall not participate in overload resolution
254
+ unless `is_copy_constructible_v<``Tᵢ``>` is `true` for all i.
255
+
256
+ ``` cpp
257
+ variant(variant&& w) noexcept(see below);
258
+ ```
259
+
260
+ *Effects:* If `w` holds a value, initializes the `variant` to hold the
261
+ same alternative as `w` and direct-initializes the contained value with
262
+ `get<j>(std::move(w))`, where `j` is `w.index()`. Otherwise, initializes
263
+ the `variant` to not hold a value.
264
+
265
+ *Throws:* Any exception thrown by move-constructing any `Tᵢ` for all i.
266
+
267
+ *Remarks:* The expression inside `noexcept` is equivalent to the logical
268
+ AND of `is_nothrow_move_constructible_v<``Tᵢ``>` for all i. This
269
+ function shall not participate in overload resolution unless
270
+ `is_move_constructible_v<``Tᵢ``>` is `true` for all i.
271
+
272
+ ``` cpp
273
+ template <class T> constexpr variant(T&& t) noexcept(see below);
274
+ ```
275
+
276
+ Let `Tⱼ` be a type that is determined as follows: build an imaginary
277
+ function *FUN*(Tᵢ) for each alternative type `Tᵢ`. The overload
278
+ *FUN*(Tⱼ) selected by overload resolution for the expression
279
+ *FUN*(std::forward\<T\>(t)) defines the alternative `Tⱼ` which is the
280
+ type of the contained value after construction.
281
+
282
+ *Effects:* Initializes `*this` to hold the alternative type `Tⱼ` and
283
+ direct-initializes the contained value as if
284
+ direct-non-list-initializing it with `std::forward<T>(t)`.
285
+
286
+ *Postconditions:* `holds_alternative<``Tⱼ``>(*this)` is `true`.
287
+
288
+ *Throws:* Any exception thrown by the initialization of the selected
289
+ alternative `Tⱼ`.
290
+
291
+ *Remarks:* This function shall not participate in overload resolution
292
+ unless `is_same_v<decay_t<T>, variant>` is `false`, unless `decay_t<T>`
293
+ is neither a specialization of `in_place_type_t` nor a specialization of
294
+ `in_place_index_t`, unless `is_constructible_v<``Tⱼ``, T>` is `true`,
295
+ and unless the expression *FUN*(`std::forward<T>(t))` (with *FUN* being
296
+ the above-mentioned set of imaginary functions) is well formed.
297
+
298
+ [*Note 2*:
299
+
300
+ ``` cpp
301
+ variant<string, string> v("abc");
302
+ ```
303
+
304
+ is ill-formed, as both alternative types have an equally viable
305
+ constructor for the argument.
306
+
307
+ — *end note*]
308
+
309
+ The expression inside `noexcept` is equivalent to
310
+ `is_nothrow_constructible_v<``Tⱼ``, T>`. If `Tⱼ`’s selected constructor
311
+ is a constexpr constructor, this constructor shall be a constexpr
312
+ constructor.
313
+
314
+ ``` cpp
315
+ template <class T, class... Args> constexpr explicit variant(in_place_type_t<T>, Args&&... args);
316
+ ```
317
+
318
+ *Effects:* Initializes the contained value as if
319
+ direct-non-list-initializing an object of type `T` with the arguments
320
+ `std::forward<Args>(args)...`.
321
+
322
+ *Postconditions:* `holds_alternative<T>(*this)` is `true`.
323
+
324
+ *Throws:* Any exception thrown by calling the selected constructor of
325
+ `T`.
326
+
327
+ *Remarks:* This function shall not participate in overload resolution
328
+ unless there is exactly one occurrence of `T` in `Types...` and
329
+ `is_constructible_v<T, Args...>` is `true`. If `T`’s selected
330
+ constructor is a constexpr constructor, this constructor shall be a
331
+ constexpr constructor.
332
+
333
+ ``` cpp
334
+ template <class T, class U, class... Args>
335
+ constexpr explicit variant(in_place_type_t<T>, initializer_list<U> il, Args&&... args);
336
+ ```
337
+
338
+ *Effects:* Initializes the contained value as if
339
+ direct-non-list-initializing an object of type `T` with the arguments
340
+ `il, std::forward<Args>(args)...`.
341
+
342
+ *Postconditions:* `holds_alternative<T>(*this)` is `true`.
343
+
344
+ *Throws:* Any exception thrown by calling the selected constructor of
345
+ `T`.
346
+
347
+ *Remarks:* This function shall not participate in overload resolution
348
+ unless there is exactly one occurrence of `T` in `Types...` and
349
+ `is_constructible_v<T, initializer_list<U>&, Args...>` is `true`. If
350
+ `T`’s selected constructor is a constexpr constructor, this constructor
351
+ shall be a constexpr constructor.
352
+
353
+ ``` cpp
354
+ template <size_t I, class... Args> constexpr explicit variant(in_place_index_t<I>, Args&&... args);
355
+ ```
356
+
357
+ *Effects:* Initializes the contained value as if
358
+ direct-non-list-initializing an object of type `T_I` with the arguments
359
+ `std::forward<Args>(args)...`.
360
+
361
+ *Postconditions:* `index()` is `I`.
362
+
363
+ *Throws:* Any exception thrown by calling the selected constructor of
364
+ `T_I`.
365
+
366
+ *Remarks:* This function shall not participate in overload resolution
367
+ unless
368
+
369
+ - `I` is less than `sizeof...(Types)` and
370
+ - `is_constructible_v<``T_I``, Args...>` is `true`.
371
+
372
+ If `T_I`’s selected constructor is a constexpr constructor, this
373
+ constructor shall be a constexpr constructor.
374
+
375
+ ``` cpp
376
+ template <size_t I, class U, class... Args>
377
+ constexpr explicit variant(in_place_index_t<I>, initializer_list<U> il, Args&&... args);
378
+ ```
379
+
380
+ *Effects:* Initializes the contained value as if
381
+ direct-non-list-initializing an object of type `T_I` with the arguments
382
+ `il, std::forward<Args>(args)...`.
383
+
384
+ *Postconditions:* `index()` is `I`.
385
+
386
+ *Remarks:* This function shall not participate in overload resolution
387
+ unless
388
+
389
+ - `I` is less than `sizeof...(Types)` and
390
+ - `is_constructible_v<``T_I``, initializer_list<U>&, Args...>` is
391
+ `true`.
392
+
393
+ If `T_I`’s selected constructor is a constexpr constructor, this
394
+ constructor shall be a constexpr constructor.
395
+
396
+ ``` cpp
397
+ // allocator-extended constructors
398
+ template <class Alloc>
399
+ variant(allocator_arg_t, const Alloc& a);
400
+ template <class Alloc>
401
+ variant(allocator_arg_t, const Alloc& a, const variant& v);
402
+ template <class Alloc>
403
+ variant(allocator_arg_t, const Alloc& a, variant&& v);
404
+ template <class Alloc, class T>
405
+ variant(allocator_arg_t, const Alloc& a, T&& t);
406
+ template <class Alloc, class T, class... Args>
407
+ variant(allocator_arg_t, const Alloc& a, in_place_type_t<T>, Args&&... args);
408
+ template <class Alloc, class T, class U, class... Args>
409
+ variant(allocator_arg_t, const Alloc& a, in_place_type_t<T>,
410
+ initializer_list<U> il, Args&&... args);
411
+ template <class Alloc, size_t I, class... Args>
412
+ variant(allocator_arg_t, const Alloc& a, in_place_index_t<I>, Args&&... args);
413
+ template <class Alloc, size_t I, class U, class... Args>
414
+ variant(allocator_arg_t, const Alloc& a, in_place_index_t<I>,
415
+ initializer_list<U> il, Args&&... args);
416
+ ```
417
+
418
+ *Requires:* `Alloc` shall meet the requirements for an
419
+ Allocator ([[allocator.requirements]]).
420
+
421
+ *Effects:* Equivalent to the preceding constructors except that the
422
+ contained value is constructed with uses-allocator
423
+ construction ([[allocator.uses.construction]]).
424
+
425
+ #### Destructor <a id="variant.dtor">[[variant.dtor]]</a>
426
+
427
+ ``` cpp
428
+ ~variant();
429
+ ```
430
+
431
+ *Effects:* If `valueless_by_exception()` is `false`, destroys the
432
+ currently contained value.
433
+
434
+ *Remarks:* If `is_trivially_destructible_v<``Tᵢ``> == true` for all `Tᵢ`
435
+ then this destructor shall be a trivial destructor.
436
+
437
+ #### Assignment <a id="variant.assign">[[variant.assign]]</a>
438
+
439
+ ``` cpp
440
+ variant& operator=(const variant& rhs);
441
+ ```
442
+
443
+ Let j be `rhs.index()`.
444
+
445
+ *Effects:*
446
+
447
+ - If neither `*this` nor `rhs` holds a value, there is no effect.
448
+ Otherwise,
449
+ - if `*this` holds a value but `rhs` does not, destroys the value
450
+ contained in `*this` and sets `*this` to not hold a value. Otherwise,
451
+ - if `index() == `j, assigns the value contained in `rhs` to the value
452
+ contained in `*this`. Otherwise,
453
+ - if either `is_nothrow_copy_constructible_v<``Tⱼ``>` or
454
+ `!is_nothrow_move_constructible_v<``Tⱼ``>` is `true`, equivalent to
455
+ `emplace<`j`>(get<`j`>(rhs))`. Otherwise,
456
+ - equivalent to `operator=(variant(rhs))`.
457
+
458
+ *Returns:* `*this`.
459
+
460
+ *Postconditions:* `index() == rhs.index()`.
461
+
462
+ *Remarks:* This function shall not participate in overload resolution
463
+ unless `is_copy_constructible_v<``Tᵢ``> &&`
464
+ `is_copy_assignable_v<``Tᵢ``>` is `true` for all i.
465
+
466
+ ``` cpp
467
+ variant& operator=(variant&& rhs) noexcept(see below);
468
+ ```
469
+
470
+ Let j be `rhs.index()`.
471
+
472
+ *Effects:*
473
+
474
+ - If neither `*this` nor `rhs` holds a value, there is no effect.
475
+ Otherwise,
476
+ - if `*this` holds a value but `rhs` does not, destroys the value
477
+ contained in `*this` and sets `*this` to not hold a value. Otherwise,
478
+ - if `index() == `j, assigns `get<`j`>(std::move(rhs))` to the value
479
+ contained in `*this`. Otherwise,
480
+ - equivalent to `emplace<`j`>(get<`j`>(std::move(rhs)))`.
481
+
482
+ *Returns:* `*this`.
483
+
484
+ *Remarks:* This function shall not participate in overload resolution
485
+ unless `is_move_constructible_v<``Tᵢ``> && is_move_assignable_v<``Tᵢ``>`
486
+ is `true` for all i. The expression inside `noexcept` is equivalent to:
487
+ `is_nothrow_move_constructible_v<``Tᵢ``> && is_nothrow_move_assignable_v<``Tᵢ``>`
488
+ for all i.
489
+
490
+ - If an exception is thrown during the call to `Tⱼ`’s move construction
491
+ (with j being `rhs.index())`, the `variant` will hold no value.
492
+ - If an exception is thrown during the call to `Tⱼ`’s move assignment,
493
+ the state of the contained value is as defined by the exception safety
494
+ guarantee of `Tⱼ`’s move assignment; `index()` will be j.
495
+
496
+ ``` cpp
497
+ template <class T> variant& operator=(T&& t) noexcept(see below);
498
+ ```
499
+
500
+ Let `Tⱼ` be a type that is determined as follows: build an imaginary
501
+ function *FUN*(Tᵢ) for each alternative type `Tᵢ`. The overload
502
+ *FUN*(Tⱼ) selected by overload resolution for the expression
503
+ *FUN*(std::forward\<T\>(t)) defines the alternative `Tⱼ` which is the
504
+ type of the contained value after assignment.
505
+
506
+ *Effects:*
507
+
508
+ - If `*this` holds a `Tⱼ`, assigns `std::forward<T>(t)` to the value
509
+ contained in `*this`. Otherwise,
510
+ - if `is_nothrow_constructible_v<``Tⱼ``, T> ||`
511
+ `!is_nothrow_move_constructible_v<``Tⱼ``>` is `true`, equivalent to
512
+ `emplace<`j`>(std::forward<T>(t))`. Otherwise,
513
+ - equivalent to `operator=(variant(std::forward<T>(t)))`.
514
+
515
+ *Postconditions:* `holds_alternative<``Tⱼ``>(*this)` is `true`, with
516
+ `Tⱼ` selected by the imaginary function overload resolution described
517
+ above.
518
+
519
+ *Returns:* `*this`.
520
+
521
+ *Remarks:* This function shall not participate in overload resolution
522
+ unless `is_same_v<decay_t<T>, variant>` is `false`, unless
523
+ `is_assignable_v<``Tⱼ``&, T> && is_constructible_v<``Tⱼ``, T>` is
524
+ `true`, and unless the expression *FUN*(std::forward\<T\>(t)) (with
525
+ *FUN* being the above-mentioned set of imaginary functions) is well
526
+ formed.
527
+
528
+ [*Note 1*:
529
+
530
+ ``` cpp
531
+ variant<string, string> v;
532
+ v = "abc";
533
+ ```
534
+
535
+ is ill-formed, as both alternative types have an equally viable
536
+ constructor for the argument.
537
+
538
+ — *end note*]
539
+
540
+ The expression inside `noexcept` is equivalent to:
541
+
542
+ ``` cpp
543
+ is_nothrow_assignable_v<Tⱼ&, T> && is_nothrow_constructible_v<Tⱼ, T>
544
+ ```
545
+
546
+ - If an exception is thrown during the assignment of
547
+ `std::forward<T>(t)` to the value contained in `*this`, the state of
548
+ the contained value and `t` are as defined by the exception safety
549
+ guarantee of the assignment expression; `valueless_by_exception()`
550
+ will be `false`.
551
+ - If an exception is thrown during the initialization of the contained
552
+ value, the `variant` object might not hold a value.
553
+
554
+ #### Modifiers <a id="variant.mod">[[variant.mod]]</a>
555
+
556
+ ``` cpp
557
+ template <class T, class... Args> T& emplace(Args&&... args);
558
+ ```
559
+
560
+ Let I be the zero-based index of `T` in `Types...`.
561
+
562
+ *Effects:* Equivalent to:
563
+ `return emplace<`I`>(std::forward<Args>(args)...);`
564
+
565
+ *Remarks:* This function shall not participate in overload resolution
566
+ unless `is_constructible_v<T, Args...>` is `true`, and `T` occurs
567
+ exactly once in `Types...`.
568
+
569
+ ``` cpp
570
+ template <class T, class U, class... Args> T& emplace(initializer_list<U> il, Args&&... args);
571
+ ```
572
+
573
+ Let I be the zero-based index of `T` in `Types...`.
574
+
575
+ *Effects:* Equivalent to:
576
+ `return emplace<`I`>(il, std::forward<Args>(args)...);`
577
+
578
+ *Remarks:* This function shall not participate in overload resolution
579
+ unless `is_constructible_v<T, initializer_list<U>&, Args...>` is `true`,
580
+ and `T` occurs exactly once in `Types...`.
581
+
582
+ ``` cpp
583
+ template <size_t I, class... Args>
584
+ variant_alternative_t<I, variant<Types...>>& emplace(Args&&... args);
585
+ ```
586
+
587
+ *Requires:* `I < sizeof...(Types)`.
588
+
589
+ *Effects:* Destroys the currently contained value if
590
+ `valueless_by_exception()` is `false`. Then initializes the contained
591
+ value as if direct-non-list-initializing a value of type `T_I` with the
592
+ arguments `std::forward<Args>(args)...`.
593
+
594
+ *Postconditions:* `index()` is `I`.
595
+
596
+ *Returns:* A reference to the new contained value.
597
+
598
+ *Throws:* Any exception thrown during the initialization of the
599
+ contained value.
600
+
601
+ *Remarks:* This function shall not participate in overload resolution
602
+ unless `is_constructible_v<``T_I``, Args...>` is `true`. If an exception
603
+ is thrown during the initialization of the contained value, the
604
+ `variant` might not hold a value.
605
+
606
+ ``` cpp
607
+ template <size_t I, class U, class... Args>
608
+ variant_alternative_t<I, variant<Types...>>& emplace(initializer_list<U> il, Args&&... args);
609
+ ```
610
+
611
+ *Requires:* `I < sizeof...(Types)`.
612
+
613
+ *Effects:* Destroys the currently contained value if
614
+ `valueless_by_exception()` is `false`. Then initializes the contained
615
+ value as if direct-non-list-initializing a value of type `T_I` with the
616
+ arguments `il, std::forward<Args>(args)...`.
617
+
618
+ *Postconditions:* `index()` is `I`.
619
+
620
+ *Returns:* A reference to the new contained value.
621
+
622
+ *Throws:* Any exception thrown during the initialization of the
623
+ contained value.
624
+
625
+ *Remarks:* This function shall not participate in overload resolution
626
+ unless `is_constructible_v<``T_I``, initializer_list<U>&, Args...>` is
627
+ `true`. If an exception is thrown during the initialization of the
628
+ contained value, the `variant` might not hold a value.
629
+
630
+ #### Value status <a id="variant.status">[[variant.status]]</a>
631
+
632
+ ``` cpp
633
+ constexpr bool valueless_by_exception() const noexcept;
634
+ ```
635
+
636
+ *Effects:* Returns `false` if and only if the `variant` holds a value.
637
+
638
+ [*Note 1*:
639
+
640
+ A `variant` might not hold a value if an exception is thrown during a
641
+ type-changing assignment or emplacement. The latter means that even a
642
+ `variant<float, int>` can become `valueless_by_exception()`, for
643
+ instance by
644
+
645
+ ``` cpp
646
+ struct S { operator int() { throw 42; }};
647
+ variant<float, int> v{12.f};
648
+ v.emplace<1>(S());
649
+ ```
650
+
651
+ — *end note*]
652
+
653
+ ``` cpp
654
+ constexpr size_t index() const noexcept;
655
+ ```
656
+
657
+ *Effects:* If `valueless_by_exception()` is `true`, returns
658
+ `variant_npos`. Otherwise, returns the zero-based index of the
659
+ alternative of the contained value.
660
+
661
+ #### Swap <a id="variant.swap">[[variant.swap]]</a>
662
+
663
+ ``` cpp
664
+ void swap(variant& rhs) noexcept(see below);
665
+ ```
666
+
667
+ *Requires:* Lvalues of type `Tᵢ` shall be
668
+ swappable ([[swappable.requirements]]) and
669
+ `is_move_constructible_v<``Tᵢ``>` shall be `true` for all i.
670
+
671
+ *Effects:*
672
+
673
+ - if `valueless_by_exception() && rhs.valueless_by_exception()` no
674
+ effect. Otherwise,
675
+ - if `index() == rhs.index()`, calls
676
+ `swap(get<`i`>(*this), get<`i`>(rhs))` where i is `index()`.
677
+ Otherwise,
678
+ - exchanges values of `rhs` and `*this`.
679
+
680
+ *Throws:* If `index() == rhs.index()`, any exception thrown by
681
+ `swap(get<`i`>(*this), get<`i`>(rhs))` with i being `index()`.
682
+ Otherwise, any exception thrown by the move constructor of `Tᵢ` or `Tⱼ`
683
+ with i being `index()` and j being `rhs.index()`.
684
+
685
+ *Remarks:* If an exception is thrown during the call to function
686
+ `swap(get<`i`>(*this), get<`i`>(rhs))`, the states of the contained
687
+ values of `*this` and of `rhs` are determined by the exception safety
688
+ guarantee of `swap` for lvalues of `Tᵢ` with i being `index()`. If an
689
+ exception is thrown during the exchange of the values of `*this` and
690
+ `rhs`, the states of the values of `*this` and of `rhs` are determined
691
+ by the exception safety guarantee of `variant`’s move constructor. The
692
+ expression inside `noexcept` is equivalent to the logical AND of
693
+ `is_nothrow_move_constructible_v<``Tᵢ``> && is_nothrow_swappable_v<``Tᵢ``>`
694
+ for all i.
695
+
696
+ ### `variant` helper classes <a id="variant.helper">[[variant.helper]]</a>
697
+
698
+ ``` cpp
699
+ template <class T> struct variant_size;
700
+ ```
701
+
702
+ *Remarks:* All specializations of `variant_size` shall meet the
703
+ `UnaryTypeTrait` requirements ([[meta.rqmts]]) with a base
704
+ characteristic of `integral_constant<size_t, N>` for some `N`.
705
+
706
+ ``` cpp
707
+ template <class T> class variant_size<const T>;
708
+ template <class T> class variant_size<volatile T>;
709
+ template <class T> class variant_size<const volatile T>;
710
+ ```
711
+
712
+ Let `VS` denote `variant_size<T>` of the cv-unqualified type `T`. Then
713
+ each of the three templates shall meet the `UnaryTypeTrait`
714
+ requirements ([[meta.rqmts]]) with a base characteristic of
715
+ `integral_constant<size_t, VS::value>`.
716
+
717
+ ``` cpp
718
+ template <class... Types>
719
+ struct variant_size<variant<Types...>> : integral_constant<size_t, sizeof...(Types)> { };
720
+ ```
721
+
722
+ ``` cpp
723
+ template <size_t I, class T> class variant_alternative<I, const T>;
724
+ template <size_t I, class T> class variant_alternative<I, volatile T>;
725
+ template <size_t I, class T> class variant_alternative<I, const volatile T>;
726
+ ```
727
+
728
+ Let `VA` denote `variant_alternative<I, T>` of the cv-unqualified type
729
+ `T`. Then each of the three templates shall meet the
730
+ `TransformationTrait` requirements ([[meta.rqmts]]) with a member
731
+ typedef `type` that names the following type:
732
+
733
+ - for the first specialization, `add_const_t<VA::type>`,
734
+ - for the second specialization, `add_volatile_t<VA::type>`, and
735
+ - for the third specialization, `add_cv_t<VA::type>`.
736
+
737
+ ``` cpp
738
+ variant_alternative<I, variant<Types...>>::type
739
+ ```
740
+
741
+ *Requires:* `I < sizeof...(Types)`.
742
+
743
+ *Value:* The type `T_I`.
744
+
745
+ ### Value access <a id="variant.get">[[variant.get]]</a>
746
+
747
+ ``` cpp
748
+ template <class T, class... Types>
749
+ constexpr bool holds_alternative(const variant<Types...>& v) noexcept;
750
+ ```
751
+
752
+ *Requires:* The type `T` occurs exactly once in `Types...`. Otherwise,
753
+ the program is ill-formed.
754
+
755
+ *Returns:* `true` if `index()` is equal to the zero-based index of `T`
756
+ in `Types...`.
757
+
758
+ ``` cpp
759
+ template <size_t I, class... Types>
760
+ constexpr variant_alternative_t<I, variant<Types...>>& get(variant<Types...>& v);
761
+ template <size_t I, class... Types>
762
+ constexpr variant_alternative_t<I, variant<Types...>>&& get(variant<Types...>&& v);
763
+ template <size_t I, class... Types>
764
+ constexpr const variant_alternative_t<I, variant<Types...>>& get(const variant<Types...>& v);
765
+ template <size_t I, class... Types>
766
+ constexpr const variant_alternative_t<I, variant<Types...>>&& get(const variant<Types...>&& v);
767
+ ```
768
+
769
+ *Requires:* `I < sizeof...(Types)`. Otherwise the program is ill-formed.
770
+
771
+ *Effects:* If `v.index()` is `I`, returns a reference to the object
772
+ stored in the `variant`. Otherwise, throws an exception of type
773
+ `bad_variant_access`.
774
+
775
+ ``` cpp
776
+ template <class T, class... Types> constexpr T& get(variant<Types...>& v);
777
+ template <class T, class... Types> constexpr T&& get(variant<Types...>&& v);
778
+ template <class T, class... Types> constexpr const T& get(const variant<Types...>& v);
779
+ template <class T, class... Types> constexpr const T&& get(const variant<Types...>&& v);
780
+ ```
781
+
782
+ *Requires:* The type `T` occurs exactly once in `Types...`. Otherwise,
783
+ the program is ill-formed.
784
+
785
+ *Effects:* If `v` holds a value of type `T`, returns a reference to that
786
+ value. Otherwise, throws an exception of type `bad_variant_access`.
787
+
788
+ ``` cpp
789
+ template <size_t I, class... Types>
790
+ constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
791
+ get_if(variant<Types...>* v) noexcept;
792
+ template <size_t I, class... Types>
793
+ constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
794
+ get_if(const variant<Types...>* v) noexcept;
795
+ ```
796
+
797
+ *Requires:* `I < sizeof...(Types)`. Otherwise the program is ill-formed.
798
+
799
+ *Returns:* A pointer to the value stored in the `variant`, if
800
+ `v != nullptr` and `v->index() == I`. Otherwise, returns `nullptr`.
801
+
802
+ ``` cpp
803
+ template <class T, class... Types>
804
+ constexpr add_pointer_t<T>
805
+ get_if(variant<Types...>* v) noexcept;
806
+ template <class T, class... Types>
807
+ constexpr add_pointer_t<const T>
808
+ get_if(const variant<Types...>* v) noexcept;
809
+ ```
810
+
811
+ *Requires:* The type `T` occurs exactly once in `Types...`. Otherwise,
812
+ the program is ill-formed.
813
+
814
+ *Effects:* Equivalent to: `return get_if<`i`>(v);` with i being the
815
+ zero-based index of `T` in `Types...`.
816
+
817
+ ### Relational operators <a id="variant.relops">[[variant.relops]]</a>
818
+
819
+ ``` cpp
820
+ template <class... Types>
821
+ constexpr bool operator==(const variant<Types...>& v, const variant<Types...>& w);
822
+ ```
823
+
824
+ *Requires:* `get<`i`>(v) == get<`i`>(w)` is a valid expression returning
825
+ a type that is convertible to `bool`, for all i.
826
+
827
+ *Returns:* If `v.index() != w.index()`, `false`; otherwise if
828
+ `v.valueless_by_exception()`, `true`; otherwise
829
+ `get<`i`>(v) == get<`i`>(w)` with i being `v.index()`.
830
+
831
+ ``` cpp
832
+ template <class... Types>
833
+ constexpr bool operator!=(const variant<Types...>& v, const variant<Types...>& w);
834
+ ```
835
+
836
+ *Requires:* `get<`i`>(v) != get<`i`>(w)` is a valid expression returning
837
+ a type that is convertible to `bool`, for all i.
838
+
839
+ *Returns:* If `v.index() != w.index()`, `true`; otherwise if
840
+ `v.valueless_by_exception()`, `false`; otherwise
841
+ `get<`i`>(v) != get<`i`>(w)` with i being `v.index()`.
842
+
843
+ ``` cpp
844
+ template <class... Types>
845
+ constexpr bool operator<(const variant<Types...>& v, const variant<Types...>& w);
846
+ ```
847
+
848
+ *Requires:* `get<`i`>(v) < get<`i`>(w)` is a valid expression returning
849
+ a type that is convertible to `bool`, for all i.
850
+
851
+ *Returns:* If `w.valueless_by_exception()`, `false`; otherwise if
852
+ `v.valueless_by_exception()`, `true`; otherwise, if
853
+ `v.index() < w.index()`, `true`; otherwise if `v.index() > w.index()`,
854
+ `false`; otherwise `get<`i`>(v) < get<`i`>(w)` with i being `v.index()`.
855
+
856
+ ``` cpp
857
+ template <class... Types>
858
+ constexpr bool operator>(const variant<Types...>& v, const variant<Types...>& w);
859
+ ```
860
+
861
+ *Requires:* `get<`i`>(v) > get<`i`>(w)` is a valid expression returning
862
+ a type that is convertible to `bool`, for all i.
863
+
864
+ *Returns:* If `v.valueless_by_exception()`, `false`; otherwise if
865
+ `w.valueless_by_exception()`, `true`; otherwise, if
866
+ `v.index() > w.index()`, `true`; otherwise if `v.index() < w.index()`,
867
+ `false`; otherwise `get<`i`>(v) > get<`i`>(w)` with i being `v.index()`.
868
+
869
+ ``` cpp
870
+ template <class... Types>
871
+ constexpr bool operator<=(const variant<Types...>& v, const variant<Types...>& w);
872
+ ```
873
+
874
+ *Requires:* `get<`i`>(v) <= get<`i`>(w)` is a valid expression returning
875
+ a type that is convertible to `bool`, for all i.
876
+
877
+ *Returns:* If `v.valueless_by_exception()`, `true`; otherwise if
878
+ `w.valueless_by_exception()`, `false`; otherwise, if
879
+ `v.index() < w.index()`, `true`; otherwise if `v.index() > w.index()`,
880
+ `false`; otherwise `get<`i`>(v) <= get<`i`>(w)` with i being
881
+ `v.index()`.
882
+
883
+ ``` cpp
884
+ template <class... Types>
885
+ constexpr bool operator>=(const variant<Types...>& v, const variant<Types...>& w);
886
+ ```
887
+
888
+ *Requires:* `get<`i`>(v) >= get<`i`>(w)` is a valid expression returning
889
+ a type that is convertible to `bool`, for all i.
890
+
891
+ *Returns:* If `w.valueless_by_exception()`, `true`; otherwise if
892
+ `v.valueless_by_exception()`, `false`; otherwise, if
893
+ `v.index() > w.index()`, `true`; otherwise if `v.index() < w.index()`,
894
+ `false`; otherwise `get<`i`>(v) >= get<`i`>(w)` with i being
895
+ `v.index()`.
896
+
897
+ ### Visitation <a id="variant.visit">[[variant.visit]]</a>
898
+
899
+ ``` cpp
900
+ template <class Visitor, class... Variants>
901
+ constexpr see below visit(Visitor&& vis, Variants&&... vars);
902
+ ```
903
+
904
+ *Requires:* The expression in the *Effects:* element shall be a valid
905
+ expression of the same type and value category, for all combinations of
906
+ alternative types of all variants. Otherwise, the program is ill-formed.
907
+
908
+ *Effects:* Let `is...` be `vars.index()...`. Returns
909
+ *INVOKE*(forward\<Visitor\>(vis), get\<is\>(
910
+ `forward<Variants>(vars))...);`.
911
+
912
+ *Remarks:* The return type is the common type of all possible *`INVOKE`*
913
+ expressions of the *Effects:* element.
914
+
915
+ *Throws:* `bad_variant_access` if any `variant` in `vars` is
916
+ `valueless_by_exception()`.
917
+
918
+ *Complexity:* For `sizeof...(Variants) <= 1`, the invocation of the
919
+ callable object is implemented in constant time, i.e. it does not depend
920
+ on `sizeof...(Types).` For `sizeof...(Variants) > 1`, the invocation of
921
+ the callable object has no complexity requirements.
922
+
923
+ ### Class `monostate` <a id="variant.monostate">[[variant.monostate]]</a>
924
+
925
+ ``` cpp
926
+ struct monostate{};
927
+ ```
928
+
929
+ The class `monostate` can serve as a first alternative type for a
930
+ `variant` to make the `variant` type default constructible.
931
+
932
+ ### `monostate` relational operators <a id="variant.monostate.relops">[[variant.monostate.relops]]</a>
933
+
934
+ ``` cpp
935
+ constexpr bool operator<(monostate, monostate) noexcept { return false; }
936
+ constexpr bool operator>(monostate, monostate) noexcept { return false; }
937
+ constexpr bool operator<=(monostate, monostate) noexcept { return true; }
938
+ constexpr bool operator>=(monostate, monostate) noexcept { return true; }
939
+ constexpr bool operator==(monostate, monostate) noexcept { return true; }
940
+ constexpr bool operator!=(monostate, monostate) noexcept { return false; }
941
+ ```
942
+
943
+ [*Note 1*: `monostate` objects have only a single state; they thus
944
+ always compare equal. — *end note*]
945
+
946
+ ### Specialized algorithms <a id="variant.specalg">[[variant.specalg]]</a>
947
+
948
+ ``` cpp
949
+ template <class... Types>
950
+ void swap(variant<Types...>& v, variant<Types...>& w) noexcept(see below);
951
+ ```
952
+
953
+ *Effects:* Equivalent to `v.swap(w)`.
954
+
955
+ *Remarks:* This function shall not participate in overload resolution
956
+ unless `is_move_constructible_v<``Tᵢ``> && is_swappable_v<``Tᵢ``>` is
957
+ `true` for all i. The expression inside `noexcept` is equivalent to
958
+ `noexcept(v.swap(w))`.
959
+
960
+ ### Class `bad_variant_access` <a id="variant.bad.access">[[variant.bad.access]]</a>
961
+
962
+ ``` cpp
963
+ class bad_variant_access : public exception {
964
+ public:
965
+ bad_variant_access() noexcept;
966
+ const char* what() const noexcept override;
967
+ };
968
+ ```
969
+
970
+ Objects of type `bad_variant_access` are thrown to report invalid
971
+ accesses to the value of a `variant` object.
972
+
973
+ ``` cpp
974
+ bad_variant_access() noexcept;
975
+ ```
976
+
977
+ Constructs a `bad_variant_access` object.
978
+
979
+ ``` cpp
980
+ const char* what() const noexcept override;
981
+ ```
982
+
983
+ *Returns:* An *implementation-defined* NTBS.
984
+
985
+ ### Hash support <a id="variant.hash">[[variant.hash]]</a>
986
+
987
+ ``` cpp
988
+ template <class... Types> struct hash<variant<Types...>>;
989
+ ```
990
+
991
+ The specialization `hash<variant<Types...>>` is
992
+ enabled ([[unord.hash]]) if and only if every specialization in
993
+ `hash<remove_const_t<Types>>...` is enabled. The member functions are
994
+ not guaranteed to be `noexcept`.
995
+
996
+ ``` cpp
997
+ template <> struct hash<monostate>;
998
+ ```
999
+
1000
+ The specialization is enabled ([[unord.hash]]).
1001
+
1002
+ ### Allocator-related traits <a id="variant.traits">[[variant.traits]]</a>
1003
+
1004
+ ``` cpp
1005
+ template <class... Types, class Alloc>
1006
+ struct uses_allocator<variant<Types...>, Alloc> : true_type { };
1007
+ ```
1008
+
1009
+ *Requires:* `Alloc` shall be an Allocator ([[allocator.requirements]]).
1010
+
1011
+ [*Note 1*: Specialization of this trait informs other library
1012
+ components that variant can be constructed with an allocator, even
1013
+ though it does not have a nested `allocator_type`. — *end note*]
1014
+