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