1// -*- C++ -*- 2//===------------------------------ variant -------------------------------===// 3// 4// The LLVM Compiler Infrastructure 5// 6// This file is dual licensed under the MIT and the University of Illinois Open 7// Source Licenses. See LICENSE.TXT for details. 8// 9//===----------------------------------------------------------------------===// 10 11#ifndef _LIBCPP_VARIANT 12#define _LIBCPP_VARIANT 13 14/* 15 variant synopsis 16 17namespace std { 18 19 // 20.7.2, class template variant 20 template <class... Types> 21 class variant { 22 public: 23 24 // 20.7.2.1, constructors 25 constexpr variant() noexcept(see below); 26 variant(const variant&); 27 variant(variant&&) noexcept(see below); 28 29 template <class T> constexpr variant(T&&) noexcept(see below); 30 31 template <class T, class... Args> 32 constexpr explicit variant(in_place_type_t<T>, Args&&...); 33 34 template <class T, class U, class... Args> 35 constexpr explicit variant( 36 in_place_type_t<T>, initializer_list<U>, Args&&...); 37 38 template <size_t I, class... Args> 39 constexpr explicit variant(in_place_index_t<I>, Args&&...); 40 41 template <size_t I, class U, class... Args> 42 constexpr explicit variant( 43 in_place_index_t<I>, initializer_list<U>, Args&&...); 44 45 // 20.7.2.2, destructor 46 ~variant(); 47 48 // 20.7.2.3, assignment 49 variant& operator=(const variant&); 50 variant& operator=(variant&&) noexcept(see below); 51 52 template <class T> variant& operator=(T&&) noexcept(see below); 53 54 // 20.7.2.4, modifiers 55 template <class T, class... Args> 56 T& emplace(Args&&...); 57 58 template <class T, class U, class... Args> 59 T& emplace(initializer_list<U>, Args&&...); 60 61 template <size_t I, class... Args> 62 variant_alternative_t<I, variant>& emplace(Args&&...); 63 64 template <size_t I, class U, class... Args> 65 variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...); 66 67 // 20.7.2.5, value status 68 constexpr bool valueless_by_exception() const noexcept; 69 constexpr size_t index() const noexcept; 70 71 // 20.7.2.6, swap 72 void swap(variant&) noexcept(see below); 73 }; 74 75 // 20.7.3, variant helper classes 76 template <class T> struct variant_size; // undefined 77 78 template <class T> 79 constexpr size_t variant_size_v = variant_size<T>::value; 80 81 template <class T> struct variant_size<const T>; 82 template <class T> struct variant_size<volatile T>; 83 template <class T> struct variant_size<const volatile T>; 84 85 template <class... Types> 86 struct variant_size<variant<Types...>>; 87 88 template <size_t I, class T> struct variant_alternative; // undefined 89 90 template <size_t I, class T> 91 using variant_alternative_t = typename variant_alternative<I, T>::type; 92 93 template <size_t I, class T> struct variant_alternative<I, const T>; 94 template <size_t I, class T> struct variant_alternative<I, volatile T>; 95 template <size_t I, class T> struct variant_alternative<I, const volatile T>; 96 97 template <size_t I, class... Types> 98 struct variant_alternative<I, variant<Types...>>; 99 100 constexpr size_t variant_npos = -1; 101 102 // 20.7.4, value access 103 template <class T, class... Types> 104 constexpr bool holds_alternative(const variant<Types...>&) noexcept; 105 106 template <size_t I, class... Types> 107 constexpr variant_alternative_t<I, variant<Types...>>& 108 get(variant<Types...>&); 109 110 template <size_t I, class... Types> 111 constexpr variant_alternative_t<I, variant<Types...>>&& 112 get(variant<Types...>&&); 113 114 template <size_t I, class... Types> 115 constexpr variant_alternative_t<I, variant<Types...>> const& 116 get(const variant<Types...>&); 117 118 template <size_t I, class... Types> 119 constexpr variant_alternative_t<I, variant<Types...>> const&& 120 get(const variant<Types...>&&); 121 122 template <class T, class... Types> 123 constexpr T& get(variant<Types...>&); 124 125 template <class T, class... Types> 126 constexpr T&& get(variant<Types...>&&); 127 128 template <class T, class... Types> 129 constexpr const T& get(const variant<Types...>&); 130 131 template <class T, class... Types> 132 constexpr const T&& get(const variant<Types...>&&); 133 134 template <size_t I, class... Types> 135 constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>> 136 get_if(variant<Types...>*) noexcept; 137 138 template <size_t I, class... Types> 139 constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>> 140 get_if(const variant<Types...>*) noexcept; 141 142 template <class T, class... Types> 143 constexpr add_pointer_t<T> 144 get_if(variant<Types...>*) noexcept; 145 146 template <class T, class... Types> 147 constexpr add_pointer_t<const T> 148 get_if(const variant<Types...>*) noexcept; 149 150 // 20.7.5, relational operators 151 template <class... Types> 152 constexpr bool operator==(const variant<Types...>&, const variant<Types...>&); 153 154 template <class... Types> 155 constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&); 156 157 template <class... Types> 158 constexpr bool operator<(const variant<Types...>&, const variant<Types...>&); 159 160 template <class... Types> 161 constexpr bool operator>(const variant<Types...>&, const variant<Types...>&); 162 163 template <class... Types> 164 constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&); 165 166 template <class... Types> 167 constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&); 168 169 // 20.7.6, visitation 170 template <class Visitor, class... Variants> 171 constexpr see below visit(Visitor&&, Variants&&...); 172 173 // 20.7.7, class monostate 174 struct monostate; 175 176 // 20.7.8, monostate relational operators 177 constexpr bool operator<(monostate, monostate) noexcept; 178 constexpr bool operator>(monostate, monostate) noexcept; 179 constexpr bool operator<=(monostate, monostate) noexcept; 180 constexpr bool operator>=(monostate, monostate) noexcept; 181 constexpr bool operator==(monostate, monostate) noexcept; 182 constexpr bool operator!=(monostate, monostate) noexcept; 183 184 // 20.7.9, specialized algorithms 185 template <class... Types> 186 void swap(variant<Types...>&, variant<Types...>&) noexcept(see below); 187 188 // 20.7.10, class bad_variant_access 189 class bad_variant_access; 190 191 // 20.7.11, hash support 192 template <class T> struct hash; 193 template <class... Types> struct hash<variant<Types...>>; 194 template <> struct hash<monostate>; 195 196} // namespace std 197 198*/ 199 200#include <__config> 201#include <__tuple> 202#include <array> 203#include <exception> 204#include <functional> 205#include <initializer_list> 206#include <new> 207#include <tuple> 208#include <type_traits> 209#include <utility> 210 211#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 212#pragma GCC system_header 213#endif 214 215namespace std { // explicitly not using versioning namespace 216 217class _LIBCPP_EXCEPTION_ABI bad_variant_access : public exception { 218public: 219 virtual const char* what() const _NOEXCEPT; 220}; 221 222} // namespace std 223 224_LIBCPP_BEGIN_NAMESPACE_STD 225 226#if _LIBCPP_STD_VER > 14 227 228_LIBCPP_NORETURN 229inline _LIBCPP_INLINE_VISIBILITY 230void __throw_bad_variant_access() { 231#ifndef _LIBCPP_NO_EXCEPTIONS 232 throw bad_variant_access(); 233#else 234 _VSTD::abort(); 235#endif 236} 237 238template <class... _Types> 239class _LIBCPP_TEMPLATE_VIS variant; 240 241template <class _Tp> 242struct _LIBCPP_TEMPLATE_VIS variant_size; 243 244template <class _Tp> 245constexpr size_t variant_size_v = variant_size<_Tp>::value; 246 247template <class _Tp> 248struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {}; 249 250template <class _Tp> 251struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {}; 252 253template <class _Tp> 254struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp> 255 : variant_size<_Tp> {}; 256 257template <class... _Types> 258struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>> 259 : integral_constant<size_t, sizeof...(_Types)> {}; 260 261template <size_t _Ip, class _Tp> 262struct _LIBCPP_TEMPLATE_VIS variant_alternative; 263 264template <size_t _Ip, class _Tp> 265using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type; 266 267template <size_t _Ip, class _Tp> 268struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp> 269 : add_const<variant_alternative_t<_Ip, _Tp>> {}; 270 271template <size_t _Ip, class _Tp> 272struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp> 273 : add_volatile<variant_alternative_t<_Ip, _Tp>> {}; 274 275template <size_t _Ip, class _Tp> 276struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp> 277 : add_cv<variant_alternative_t<_Ip, _Tp>> {}; 278 279template <size_t _Ip, class... _Types> 280struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> { 281 static_assert(_Ip < sizeof...(_Types)); 282 using type = __type_pack_element<_Ip, _Types...>; 283}; 284 285constexpr size_t variant_npos = static_cast<size_t>(-1); 286constexpr unsigned int __variant_npos = static_cast<unsigned int>(-1); 287 288namespace __find_detail { 289 290template <class _Tp, class... _Types> 291inline _LIBCPP_INLINE_VISIBILITY 292constexpr size_t __find_index() { 293 constexpr bool __matches[] = {is_same_v<_Tp, _Types>...}; 294 size_t __result = __not_found; 295 for (size_t __i = 0; __i < sizeof...(_Types); ++__i) { 296 if (__matches[__i]) { 297 if (__result != __not_found) { 298 return __ambiguous; 299 } 300 __result = __i; 301 } 302 } 303 return __result; 304} 305 306template <size_t _Index> 307struct __find_unambiguous_index_sfinae_impl 308 : integral_constant<size_t, _Index> {}; 309 310template <> 311struct __find_unambiguous_index_sfinae_impl<__not_found> {}; 312 313template <> 314struct __find_unambiguous_index_sfinae_impl<__ambiguous> {}; 315 316template <class _Tp, class... _Types> 317struct __find_unambiguous_index_sfinae 318 : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {}; 319 320} // namespace __find_detail 321 322namespace __variant_detail { 323 324struct __valueless_t {}; 325 326enum class _Trait { _TriviallyAvailable, _Available, _Unavailable }; 327 328template <typename _Tp, 329 template <typename> class _IsTriviallyAvailable, 330 template <typename> class _IsAvailable> 331constexpr _Trait __trait = 332 _IsTriviallyAvailable<_Tp>::value 333 ? _Trait::_TriviallyAvailable 334 : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable; 335 336inline _LIBCPP_INLINE_VISIBILITY 337constexpr _Trait __common_trait(initializer_list<_Trait> __traits) { 338 _Trait __result = _Trait::_TriviallyAvailable; 339 for (_Trait __t : __traits) { 340 if (static_cast<int>(__t) > static_cast<int>(__result)) { 341 __result = __t; 342 } 343 } 344 return __result; 345} 346 347template <typename... _Types> 348struct __traits { 349 static constexpr _Trait __copy_constructible_trait = 350 __common_trait({__trait<_Types, 351 is_trivially_copy_constructible, 352 is_copy_constructible>...}); 353 354 static constexpr _Trait __move_constructible_trait = 355 __common_trait({__trait<_Types, 356 is_trivially_move_constructible, 357 is_move_constructible>...}); 358 359 static constexpr _Trait __copy_assignable_trait = __common_trait( 360 {__copy_constructible_trait, 361 __move_constructible_trait, 362 __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...}); 363 364 static constexpr _Trait __move_assignable_trait = __common_trait( 365 {__move_constructible_trait, 366 __trait<_Types, is_trivially_move_assignable, is_move_assignable>...}); 367 368 static constexpr _Trait __destructible_trait = __common_trait( 369 {__trait<_Types, is_trivially_destructible, is_destructible>...}); 370}; 371 372namespace __access { 373 374struct __union { 375 template <class _Vp> 376 inline _LIBCPP_INLINE_VISIBILITY 377 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) { 378 return _VSTD::forward<_Vp>(__v).__head; 379 } 380 381 template <class _Vp, size_t _Ip> 382 inline _LIBCPP_INLINE_VISIBILITY 383 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) { 384 return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>); 385 } 386}; 387 388struct __base { 389 template <size_t _Ip, class _Vp> 390 inline _LIBCPP_INLINE_VISIBILITY 391 static constexpr auto&& __get_alt(_Vp&& __v) { 392 return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data, 393 in_place_index<_Ip>); 394 } 395}; 396 397struct __variant { 398 template <size_t _Ip, class _Vp> 399 inline _LIBCPP_INLINE_VISIBILITY 400 static constexpr auto&& __get_alt(_Vp&& __v) { 401 return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl); 402 } 403}; 404 405} // namespace __access 406 407namespace __visitation { 408 409struct __base { 410 template <class _Visitor, class... _Vs> 411 inline _LIBCPP_INLINE_VISIBILITY 412 static constexpr decltype(auto) 413 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) { 414 constexpr auto __fdiagonal = 415 __make_fdiagonal<_Visitor&&, 416 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>(); 417 return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor), 418 _VSTD::forward<_Vs>(__vs).__as_base()...); 419 } 420 421 template <class _Visitor, class... _Vs> 422 inline _LIBCPP_INLINE_VISIBILITY 423 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, 424 _Vs&&... __vs) { 425 constexpr auto __fmatrix = 426 __make_fmatrix<_Visitor&&, 427 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>(); 428 const size_t __indices[] = {__vs.index()...}; 429 return __at(__fmatrix, __indices)(_VSTD::forward<_Visitor>(__visitor), 430 _VSTD::forward<_Vs>(__vs).__as_base()...); 431 } 432 433private: 434 template <class _Tp> 435 inline _LIBCPP_INLINE_VISIBILITY 436 static constexpr const _Tp& __at_impl(const _Tp& __elem, const size_t*) { 437 return __elem; 438 } 439 440 template <class _Tp, size_t _Np> 441 inline _LIBCPP_INLINE_VISIBILITY 442 static constexpr auto&& __at_impl(const array<_Tp, _Np>& __elems, 443 const size_t* __index) { 444 return __at_impl(__elems[*__index], __index + 1); 445 } 446 447 template <class _Tp, size_t _Np, size_t _Ip> 448 inline _LIBCPP_INLINE_VISIBILITY 449 static constexpr auto&& __at(const array<_Tp, _Np>& __elems, 450 const size_t (&__indices)[_Ip]) { 451 return __at_impl(__elems, begin(__indices)); 452 } 453 454 template <class _Fp, class... _Fs> 455 static constexpr void __std_visit_visitor_return_type_check() { 456 static_assert( 457 __all<is_same_v<_Fp, _Fs>...>::value, 458 "`std::visit` requires the visitor to have a single return type."); 459 } 460 461 template <class... _Fs> 462 inline _LIBCPP_INLINE_VISIBILITY 463 static constexpr auto __make_farray(_Fs&&... __fs) { 464 __std_visit_visitor_return_type_check<decay_t<_Fs>...>(); 465 using __result = array<common_type_t<decay_t<_Fs>...>, sizeof...(_Fs)>; 466 return __result{{_VSTD::forward<_Fs>(__fs)...}}; 467 } 468 469 template <std::size_t... _Is> 470 struct __dispatcher { 471 template <class _Fp, class... _Vs> 472 inline _LIBCPP_INLINE_VISIBILITY 473 static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) { 474 return __invoke_constexpr( 475 static_cast<_Fp>(__f), 476 __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...); 477 } 478 }; 479 480 template <class _Fp, class... _Vs, size_t... _Is> 481 inline _LIBCPP_INLINE_VISIBILITY 482 static constexpr auto __make_dispatch(index_sequence<_Is...>) { 483 return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>; 484 } 485 486 template <size_t _Ip, class _Fp, class... _Vs> 487 inline _LIBCPP_INLINE_VISIBILITY 488 static constexpr auto __make_fdiagonal_impl() { 489 return __make_dispatch<_Fp, _Vs...>( 490 index_sequence<(__identity<_Vs>{}, _Ip)...>{}); 491 } 492 493 template <class _Fp, class... _Vs, size_t... _Is> 494 inline _LIBCPP_INLINE_VISIBILITY 495 static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) { 496 return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...); 497 } 498 499 template <class _Fp, class _Vp, class... _Vs> 500 inline _LIBCPP_INLINE_VISIBILITY 501 static constexpr auto __make_fdiagonal() { 502 constexpr size_t _Np = decay_t<_Vp>::__size(); 503 static_assert(__all<(_Np == decay_t<_Vs>::__size())...>::value); 504 return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{}); 505 } 506 507 template <class _Fp, class... _Vs, size_t... _Is> 508 inline _LIBCPP_INLINE_VISIBILITY 509 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) { 510 return __make_dispatch<_Fp, _Vs...>(__is); 511 } 512 513 template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls> 514 inline _LIBCPP_INLINE_VISIBILITY 515 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>, 516 index_sequence<_Js...>, 517 _Ls... __ls) { 518 return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>( 519 index_sequence<_Is..., _Js>{}, __ls...)...); 520 } 521 522 template <class _Fp, class... _Vs> 523 inline _LIBCPP_INLINE_VISIBILITY 524 static constexpr auto __make_fmatrix() { 525 return __make_fmatrix_impl<_Fp, _Vs...>( 526 index_sequence<>{}, make_index_sequence<decay_t<_Vs>::__size()>{}...); 527 } 528}; 529 530struct __variant { 531 template <class _Visitor, class... _Vs> 532 inline _LIBCPP_INLINE_VISIBILITY 533 static constexpr decltype(auto) 534 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) { 535 return __base::__visit_alt_at(__index, 536 _VSTD::forward<_Visitor>(__visitor), 537 _VSTD::forward<_Vs>(__vs).__impl...); 538 } 539 540 template <class _Visitor, class... _Vs> 541 inline _LIBCPP_INLINE_VISIBILITY 542 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, 543 _Vs&&... __vs) { 544 return __base::__visit_alt(_VSTD::forward<_Visitor>(__visitor), 545 _VSTD::forward<_Vs>(__vs).__impl...); 546 } 547 548 template <class _Visitor, class... _Vs> 549 inline _LIBCPP_INLINE_VISIBILITY 550 static constexpr decltype(auto) 551 __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) { 552 return __visit_alt_at( 553 __index, 554 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)), 555 _VSTD::forward<_Vs>(__vs)...); 556 } 557 558 template <class _Visitor, class... _Vs> 559 inline _LIBCPP_INLINE_VISIBILITY 560 static constexpr decltype(auto) __visit_value(_Visitor&& __visitor, 561 _Vs&&... __vs) { 562 return __visit_alt( 563 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)), 564 _VSTD::forward<_Vs>(__vs)...); 565 } 566 567private: 568 template <class _Visitor, class... _Values> 569 static constexpr void __std_visit_exhaustive_visitor_check() { 570 static_assert(is_callable_v<_Visitor(_Values...)>, 571 "`std::visit` requires the visitor to be exhaustive."); 572 } 573 574 template <class _Visitor> 575 struct __value_visitor { 576 template <class... _Alts> 577 inline _LIBCPP_INLINE_VISIBILITY 578 constexpr decltype(auto) operator()(_Alts&&... __alts) const { 579 __std_visit_exhaustive_visitor_check< 580 _Visitor, 581 decltype((_VSTD::forward<_Alts>(__alts).__value))...>(); 582 return __invoke_constexpr(_VSTD::forward<_Visitor>(__visitor), 583 _VSTD::forward<_Alts>(__alts).__value...); 584 } 585 _Visitor&& __visitor; 586 }; 587 588 template <class _Visitor> 589 inline _LIBCPP_INLINE_VISIBILITY 590 static constexpr auto __make_value_visitor(_Visitor&& __visitor) { 591 return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)}; 592 } 593}; 594 595} // namespace __visitation 596 597template <size_t _Index, class _Tp> 598struct _LIBCPP_TEMPLATE_VIS __alt { 599 using __value_type = _Tp; 600 601 template <class... _Args> 602 inline _LIBCPP_INLINE_VISIBILITY 603 explicit constexpr __alt(in_place_t, _Args&&... __args) 604 : __value(_VSTD::forward<_Args>(__args)...) {} 605 606 __value_type __value; 607}; 608 609template <_Trait _DestructibleTrait, size_t _Index, class... _Types> 610union _LIBCPP_TEMPLATE_VIS __union; 611 612template <_Trait _DestructibleTrait, size_t _Index> 613union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {}; 614 615#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \ 616 template <size_t _Index, class _Tp, class... _Types> \ 617 union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \ 618 _Index, \ 619 _Tp, \ 620 _Types...> { \ 621 public: \ 622 inline _LIBCPP_INLINE_VISIBILITY \ 623 explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \ 624 \ 625 template <class... _Args> \ 626 inline _LIBCPP_INLINE_VISIBILITY \ 627 explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \ 628 : __head(in_place, _VSTD::forward<_Args>(__args)...) {} \ 629 \ 630 template <size_t _Ip, class... _Args> \ 631 inline _LIBCPP_INLINE_VISIBILITY \ 632 explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \ 633 : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \ 634 \ 635 __union(const __union&) = default; \ 636 __union(__union&&) = default; \ 637 \ 638 destructor \ 639 \ 640 __union& operator=(const __union&) = default; \ 641 __union& operator=(__union&&) = default; \ 642 \ 643 private: \ 644 char __dummy; \ 645 __alt<_Index, _Tp> __head; \ 646 __union<destructible_trait, _Index + 1, _Types...> __tail; \ 647 \ 648 friend struct __access::__union; \ 649 } 650 651_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;); 652_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {}); 653_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;); 654 655#undef _LIBCPP_VARIANT_UNION 656 657template <_Trait _DestructibleTrait, class... _Types> 658class _LIBCPP_TEMPLATE_VIS __base { 659public: 660 inline _LIBCPP_INLINE_VISIBILITY 661 explicit constexpr __base(__valueless_t tag) noexcept 662 : __data(tag), __index(__variant_npos) {} 663 664 template <size_t _Ip, class... _Args> 665 inline _LIBCPP_INLINE_VISIBILITY 666 explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args) 667 : 668 __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...), 669 __index(_Ip) {} 670 671 inline _LIBCPP_INLINE_VISIBILITY 672 constexpr bool valueless_by_exception() const noexcept { 673 return index() == variant_npos; 674 } 675 676 inline _LIBCPP_INLINE_VISIBILITY 677 constexpr size_t index() const noexcept { 678 return __index == __variant_npos ? variant_npos : __index; 679 } 680 681protected: 682 inline _LIBCPP_INLINE_VISIBILITY 683 constexpr auto&& __as_base() & { return *this; } 684 685 inline _LIBCPP_INLINE_VISIBILITY 686 constexpr auto&& __as_base() && { return _VSTD::move(*this); } 687 688 inline _LIBCPP_INLINE_VISIBILITY 689 constexpr auto&& __as_base() const & { return *this; } 690 691 inline _LIBCPP_INLINE_VISIBILITY 692 constexpr auto&& __as_base() const && { return _VSTD::move(*this); } 693 694 inline _LIBCPP_INLINE_VISIBILITY 695 static constexpr size_t __size() { return sizeof...(_Types); } 696 697 __union<_DestructibleTrait, 0, _Types...> __data; 698 unsigned int __index; 699 700 friend struct __access::__base; 701 friend struct __visitation::__base; 702}; 703 704template <class _Traits, _Trait = _Traits::__destructible_trait> 705class _LIBCPP_TEMPLATE_VIS __destructor; 706 707#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \ 708 template <class... _Types> \ 709 class _LIBCPP_TEMPLATE_VIS __destructor<__traits<_Types...>, \ 710 destructible_trait> \ 711 : public __base<destructible_trait, _Types...> { \ 712 using __base_type = __base<destructible_trait, _Types...>; \ 713 \ 714 public: \ 715 using __base_type::__base_type; \ 716 using __base_type::operator=; \ 717 \ 718 __destructor(const __destructor&) = default; \ 719 __destructor(__destructor&&) = default; \ 720 destructor \ 721 __destructor& operator=(const __destructor&) = default; \ 722 __destructor& operator=(__destructor&&) = default; \ 723 \ 724 protected: \ 725 inline _LIBCPP_INLINE_VISIBILITY \ 726 destroy \ 727 } 728 729_LIBCPP_VARIANT_DESTRUCTOR( 730 _Trait::_TriviallyAvailable, 731 ~__destructor() = default;, 732 void __destroy() noexcept { this->__index = __variant_npos; }); 733 734_LIBCPP_VARIANT_DESTRUCTOR( 735 _Trait::_Available, 736 ~__destructor() { __destroy(); }, 737 void __destroy() noexcept { 738 if (!this->valueless_by_exception()) { 739 __visitation::__base::__visit_alt( 740 [](auto& __alt) noexcept { 741 using __alt_type = decay_t<decltype(__alt)>; 742 __alt.~__alt_type(); 743 }, 744 *this); 745 } 746 this->__index = __variant_npos; 747 }); 748 749_LIBCPP_VARIANT_DESTRUCTOR( 750 _Trait::_Unavailable, 751 ~__destructor() = delete;, 752 void __destroy() noexcept = delete;); 753 754#undef _LIBCPP_VARIANT_DESTRUCTOR 755 756template <class _Traits> 757class _LIBCPP_TEMPLATE_VIS __constructor : public __destructor<_Traits> { 758 using __base_type = __destructor<_Traits>; 759 760public: 761 using __base_type::__base_type; 762 using __base_type::operator=; 763 764protected: 765 template <size_t _Ip, class _Tp, class... _Args> 766 inline _LIBCPP_INLINE_VISIBILITY 767 static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) { 768 ::new ((void*)_VSTD::addressof(__a)) 769 __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...); 770 return __a.__value; 771 } 772 773 template <class _Rhs> 774 inline _LIBCPP_INLINE_VISIBILITY 775 static void __generic_construct(__constructor& __lhs, _Rhs&& __rhs) { 776 __lhs.__destroy(); 777 if (!__rhs.valueless_by_exception()) { 778 __visitation::__base::__visit_alt_at( 779 __rhs.index(), 780 [](auto& __lhs_alt, auto&& __rhs_alt) { 781 __construct_alt( 782 __lhs_alt, 783 _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value); 784 }, 785 __lhs, _VSTD::forward<_Rhs>(__rhs)); 786 __lhs.__index = __rhs.index(); 787 } 788 } 789}; 790 791template <class _Traits, _Trait = _Traits::__move_constructible_trait> 792class _LIBCPP_TEMPLATE_VIS __move_constructor; 793 794#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \ 795 move_constructor) \ 796 template <class... _Types> \ 797 class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \ 798 move_constructible_trait> \ 799 : public __constructor<__traits<_Types...>> { \ 800 using __base_type = __constructor<__traits<_Types...>>; \ 801 \ 802 public: \ 803 using __base_type::__base_type; \ 804 using __base_type::operator=; \ 805 \ 806 __move_constructor(const __move_constructor&) = default; \ 807 move_constructor \ 808 ~__move_constructor() = default; \ 809 __move_constructor& operator=(const __move_constructor&) = default; \ 810 __move_constructor& operator=(__move_constructor&&) = default; \ 811 } 812 813_LIBCPP_VARIANT_MOVE_CONSTRUCTOR( 814 _Trait::_TriviallyAvailable, 815 __move_constructor(__move_constructor&& __that) = default;); 816 817_LIBCPP_VARIANT_MOVE_CONSTRUCTOR( 818 _Trait::_Available, 819 __move_constructor(__move_constructor&& __that) noexcept( 820 __all<is_nothrow_move_constructible_v<_Types>...>::value) 821 : __move_constructor(__valueless_t{}) { 822 this->__generic_construct(*this, _VSTD::move(__that)); 823 }); 824 825_LIBCPP_VARIANT_MOVE_CONSTRUCTOR( 826 _Trait::_Unavailable, 827 __move_constructor(__move_constructor&&) = delete;); 828 829#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR 830 831template <class _Traits, _Trait = _Traits::__copy_constructible_trait> 832class _LIBCPP_TEMPLATE_VIS __copy_constructor; 833 834#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \ 835 copy_constructor) \ 836 template <class... _Types> \ 837 class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \ 838 copy_constructible_trait> \ 839 : public __move_constructor<__traits<_Types...>> { \ 840 using __base_type = __move_constructor<__traits<_Types...>>; \ 841 \ 842 public: \ 843 using __base_type::__base_type; \ 844 using __base_type::operator=; \ 845 \ 846 copy_constructor \ 847 __copy_constructor(__copy_constructor&&) = default; \ 848 ~__copy_constructor() = default; \ 849 __copy_constructor& operator=(const __copy_constructor&) = default; \ 850 __copy_constructor& operator=(__copy_constructor&&) = default; \ 851 } 852 853_LIBCPP_VARIANT_COPY_CONSTRUCTOR( 854 _Trait::_TriviallyAvailable, 855 __copy_constructor(const __copy_constructor& __that) = default;); 856 857_LIBCPP_VARIANT_COPY_CONSTRUCTOR( 858 _Trait::_Available, 859 __copy_constructor(const __copy_constructor& __that) 860 : __copy_constructor(__valueless_t{}) { 861 this->__generic_construct(*this, __that); 862 }); 863 864_LIBCPP_VARIANT_COPY_CONSTRUCTOR( 865 _Trait::_Unavailable, 866 __copy_constructor(const __copy_constructor&) = delete;); 867 868#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR 869 870template <class _Traits> 871class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> { 872 using __base_type = __copy_constructor<_Traits>; 873 874public: 875 using __base_type::__base_type; 876 using __base_type::operator=; 877 878 template <size_t _Ip, class... _Args> 879 inline _LIBCPP_INLINE_VISIBILITY 880 auto& __emplace(_Args&&... __args) { 881 this->__destroy(); 882 auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this), 883 _VSTD::forward<_Args>(__args)...); 884 this->__index = _Ip; 885 return __res; 886 } 887 888protected: 889 template <bool _CopyAssign, size_t _Ip, class _Tp, class _Arg> 890 inline _LIBCPP_INLINE_VISIBILITY 891 void __assign_alt(__alt<_Ip, _Tp>& __a, 892 _Arg&& __arg, 893 bool_constant<_CopyAssign> __tag) { 894 if (this->index() == _Ip) { 895 __a.__value = _VSTD::forward<_Arg>(__arg); 896 } else { 897 struct { 898 void operator()(true_type) const { 899 __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg))); 900 } 901 void operator()(false_type) const { 902 __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg)); 903 } 904 __assignment* __this; 905 _Arg&& __arg; 906 } __impl{this, _VSTD::forward<_Arg>(__arg)}; 907 __impl(__tag); 908 } 909 } 910 911 template <class _That> 912 inline _LIBCPP_INLINE_VISIBILITY 913 void __generic_assign(_That&& __that) { 914 if (this->valueless_by_exception() && __that.valueless_by_exception()) { 915 // do nothing. 916 } else if (__that.valueless_by_exception()) { 917 this->__destroy(); 918 } else { 919 __visitation::__base::__visit_alt_at( 920 __that.index(), 921 [this](auto& __this_alt, auto&& __that_alt) { 922 this->__assign_alt( 923 __this_alt, 924 _VSTD::forward<decltype(__that_alt)>(__that_alt).__value, 925 is_lvalue_reference<_That>{}); 926 }, 927 *this, _VSTD::forward<_That>(__that)); 928 } 929 } 930}; 931 932template <class _Traits, _Trait = _Traits::__move_assignable_trait> 933class _LIBCPP_TEMPLATE_VIS __move_assignment; 934 935#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \ 936 move_assignment) \ 937 template <class... _Types> \ 938 class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \ 939 move_assignable_trait> \ 940 : public __assignment<__traits<_Types...>> { \ 941 using __base_type = __assignment<__traits<_Types...>>; \ 942 \ 943 public: \ 944 using __base_type::__base_type; \ 945 using __base_type::operator=; \ 946 \ 947 __move_assignment(const __move_assignment&) = default; \ 948 __move_assignment(__move_assignment&&) = default; \ 949 ~__move_assignment() = default; \ 950 __move_assignment& operator=(const __move_assignment&) = default; \ 951 move_assignment \ 952 } 953 954_LIBCPP_VARIANT_MOVE_ASSIGNMENT( 955 _Trait::_TriviallyAvailable, 956 __move_assignment& operator=(__move_assignment&& __that) = default;); 957 958_LIBCPP_VARIANT_MOVE_ASSIGNMENT( 959 _Trait::_Available, 960 __move_assignment& operator=(__move_assignment&& __that) noexcept( 961 __all<(is_nothrow_move_constructible_v<_Types> && 962 is_nothrow_move_assignable_v<_Types>)...>::value) { 963 this->__generic_assign(_VSTD::move(__that)); 964 return *this; 965 }); 966 967_LIBCPP_VARIANT_MOVE_ASSIGNMENT( 968 _Trait::_Unavailable, 969 __move_assignment& operator=(__move_assignment&&) = delete;); 970 971#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT 972 973template <class _Traits, _Trait = _Traits::__copy_assignable_trait> 974class _LIBCPP_TEMPLATE_VIS __copy_assignment; 975 976#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \ 977 copy_assignment) \ 978 template <class... _Types> \ 979 class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \ 980 copy_assignable_trait> \ 981 : public __move_assignment<__traits<_Types...>> { \ 982 using __base_type = __move_assignment<__traits<_Types...>>; \ 983 \ 984 public: \ 985 using __base_type::__base_type; \ 986 using __base_type::operator=; \ 987 \ 988 __copy_assignment(const __copy_assignment&) = default; \ 989 __copy_assignment(__copy_assignment&&) = default; \ 990 ~__copy_assignment() = default; \ 991 copy_assignment \ 992 __copy_assignment& operator=(__copy_assignment&&) = default; \ 993 } 994 995_LIBCPP_VARIANT_COPY_ASSIGNMENT( 996 _Trait::_TriviallyAvailable, 997 __copy_assignment& operator=(const __copy_assignment& __that) = default;); 998 999_LIBCPP_VARIANT_COPY_ASSIGNMENT( 1000 _Trait::_Available, 1001 __copy_assignment& operator=(const __copy_assignment& __that) { 1002 this->__generic_assign(__that); 1003 return *this; 1004 }); 1005 1006_LIBCPP_VARIANT_COPY_ASSIGNMENT( 1007 _Trait::_Unavailable, 1008 __copy_assignment& operator=(const __copy_assignment&) = delete;); 1009 1010#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT 1011 1012template <class... _Types> 1013class _LIBCPP_TEMPLATE_VIS __impl 1014 : public __copy_assignment<__traits<_Types...>> { 1015 using __base_type = __copy_assignment<__traits<_Types...>>; 1016 1017public: 1018 using __base_type::__base_type; 1019 using __base_type::operator=; 1020 1021 template <size_t _Ip, class _Arg> 1022 inline _LIBCPP_INLINE_VISIBILITY 1023 void __assign(_Arg&& __arg) { 1024 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this), 1025 _VSTD::forward<_Arg>(__arg), 1026 false_type{}); 1027 } 1028 1029 inline _LIBCPP_INLINE_VISIBILITY 1030 void __swap(__impl& __that) { 1031 if (this->valueless_by_exception() && __that.valueless_by_exception()) { 1032 // do nothing. 1033 } else if (this->index() == __that.index()) { 1034 __visitation::__base::__visit_alt_at( 1035 this->index(), 1036 [](auto& __this_alt, auto& __that_alt) { 1037 using _VSTD::swap; 1038 swap(__this_alt.__value, __that_alt.__value); 1039 }, 1040 *this, 1041 __that); 1042 } else { 1043 __impl* __lhs = this; 1044 __impl* __rhs = _VSTD::addressof(__that); 1045 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) { 1046 _VSTD::swap(__lhs, __rhs); 1047 } 1048 __impl __tmp(_VSTD::move(*__rhs)); 1049#ifndef _LIBCPP_NO_EXCEPTIONS 1050 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws 1051 // and `__tmp` is nothrow move constructible then we move `__tmp` back 1052 // into `__rhs` and provide the strong exception safety guarentee. 1053 try { 1054 this->__generic_construct(*__rhs, _VSTD::move(*__lhs)); 1055 } catch (...) { 1056 if (__tmp.__move_nothrow()) { 1057 this->__generic_construct(*__rhs, _VSTD::move(__tmp)); 1058 } 1059 throw; 1060 } 1061#else 1062 this->__generic_construct(*__rhs, _VSTD::move(*__lhs)); 1063#endif 1064 this->__generic_construct(*__lhs, _VSTD::move(__tmp)); 1065 } 1066 } 1067 1068private: 1069 inline _LIBCPP_INLINE_VISIBILITY 1070 bool __move_nothrow() const { 1071 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...}; 1072 return this->valueless_by_exception() || __results[this->index()]; 1073 } 1074}; 1075 1076template <class... _Types> 1077struct __overload; 1078 1079template <> 1080struct __overload<> { void operator()() const; }; 1081 1082template <class _Tp, class... _Types> 1083struct __overload<_Tp, _Types...> : __overload<_Types...> { 1084 using __overload<_Types...>::operator(); 1085 __identity<_Tp> operator()(_Tp) const; 1086}; 1087 1088template <class _Tp, class... _Types> 1089using __best_match_t = typename result_of_t<__overload<_Types...>(_Tp&&)>::type; 1090 1091} // __variant_detail 1092 1093template <class... _Types> 1094class _LIBCPP_TEMPLATE_VIS variant 1095 : private __sfinae_ctor_base< 1096 __all<is_copy_constructible_v<_Types>...>::value, 1097 __all<is_move_constructible_v<_Types>...>::value>, 1098 private __sfinae_assign_base< 1099 __all<(is_copy_constructible_v<_Types> && 1100 is_move_constructible_v<_Types> && 1101 is_copy_assignable_v<_Types>)...>::value, 1102 __all<(is_move_constructible_v<_Types> && 1103 is_move_assignable_v<_Types>)...>::value> { 1104 static_assert(0 < sizeof...(_Types), 1105 "variant must consist of at least one alternative."); 1106 1107 static_assert(__all<!is_array_v<_Types>...>::value, 1108 "variant can not have an array type as an alternative."); 1109 1110 static_assert(__all<!is_reference_v<_Types>...>::value, 1111 "variant can not have a reference type as an alternative."); 1112 1113 static_assert(__all<!is_void_v<_Types>...>::value, 1114 "variant can not have a void type as an alternative."); 1115 1116 using __first_type = variant_alternative_t<0, variant>; 1117 1118public: 1119 template <bool _Dummy = true, 1120 enable_if_t<__dependent_type<is_default_constructible<__first_type>, 1121 _Dummy>::value, 1122 int> = 0> 1123 inline _LIBCPP_INLINE_VISIBILITY 1124 constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>) 1125 : __impl(in_place_index<0>) {} 1126 1127 variant(const variant&) = default; 1128 variant(variant&&) = default; 1129 1130 template < 1131 class _Arg, 1132 enable_if_t<!is_same_v<decay_t<_Arg>, variant>, int> = 0, 1133 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>, 1134 size_t _Ip = 1135 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 1136 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0> 1137 inline _LIBCPP_INLINE_VISIBILITY 1138 constexpr variant(_Arg&& __arg) noexcept( 1139 is_nothrow_constructible_v<_Tp, _Arg>) 1140 : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {} 1141 1142 template <size_t _Ip, class... _Args, 1143 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, 1144 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, 1145 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> 1146 inline _LIBCPP_INLINE_VISIBILITY 1147 explicit constexpr variant( 1148 in_place_index_t<_Ip>, 1149 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>) 1150 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {} 1151 1152 template < 1153 size_t _Ip, 1154 class _Up, 1155 class... _Args, 1156 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, 1157 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, 1158 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, 1159 int> = 0> 1160 inline _LIBCPP_INLINE_VISIBILITY 1161 explicit constexpr variant( 1162 in_place_index_t<_Ip>, 1163 initializer_list<_Up> __il, 1164 _Args&&... __args) noexcept( 1165 is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>) 1166 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {} 1167 1168 template < 1169 class _Tp, 1170 class... _Args, 1171 size_t _Ip = 1172 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 1173 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> 1174 inline _LIBCPP_INLINE_VISIBILITY 1175 explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept( 1176 is_nothrow_constructible_v<_Tp, _Args...>) 1177 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {} 1178 1179 template < 1180 class _Tp, 1181 class _Up, 1182 class... _Args, 1183 size_t _Ip = 1184 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 1185 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, 1186 int> = 0> 1187 inline _LIBCPP_INLINE_VISIBILITY 1188 explicit constexpr variant( 1189 in_place_type_t<_Tp>, 1190 initializer_list<_Up> __il, 1191 _Args&&... __args) noexcept( 1192 is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>) 1193 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {} 1194 1195 ~variant() = default; 1196 1197 variant& operator=(const variant&) = default; 1198 variant& operator=(variant&&) = default; 1199 1200 template < 1201 class _Arg, 1202 enable_if_t<!is_same_v<decay_t<_Arg>, variant>, int> = 0, 1203 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>, 1204 size_t _Ip = 1205 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 1206 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>, 1207 int> = 0> 1208 inline _LIBCPP_INLINE_VISIBILITY 1209 variant& operator=(_Arg&& __arg) noexcept( 1210 is_nothrow_assignable_v<_Tp&, _Arg> && 1211 is_nothrow_constructible_v<_Tp, _Arg>) { 1212 __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg)); 1213 return *this; 1214 } 1215 1216 template < 1217 size_t _Ip, 1218 class... _Args, 1219 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, 1220 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, 1221 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> 1222 inline _LIBCPP_INLINE_VISIBILITY 1223 _Tp& emplace(_Args&&... __args) { 1224 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...); 1225 } 1226 1227 template < 1228 size_t _Ip, 1229 class _Up, 1230 class... _Args, 1231 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0, 1232 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>, 1233 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, 1234 int> = 0> 1235 inline _LIBCPP_INLINE_VISIBILITY 1236 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) { 1237 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...); 1238 } 1239 1240 template < 1241 class _Tp, 1242 class... _Args, 1243 size_t _Ip = 1244 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 1245 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0> 1246 inline _LIBCPP_INLINE_VISIBILITY 1247 _Tp& emplace(_Args&&... __args) { 1248 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...); 1249 } 1250 1251 template < 1252 class _Tp, 1253 class _Up, 1254 class... _Args, 1255 size_t _Ip = 1256 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value, 1257 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, 1258 int> = 0> 1259 inline _LIBCPP_INLINE_VISIBILITY 1260 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) { 1261 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...); 1262 } 1263 1264 inline _LIBCPP_INLINE_VISIBILITY 1265 constexpr bool valueless_by_exception() const noexcept { 1266 return __impl.valueless_by_exception(); 1267 } 1268 1269 inline _LIBCPP_INLINE_VISIBILITY 1270 constexpr size_t index() const noexcept { return __impl.index(); } 1271 1272 template < 1273 bool _Dummy = true, 1274 enable_if_t< 1275 __all<( 1276 __dependent_type<is_move_constructible<_Types>, _Dummy>::value && 1277 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value, 1278 int> = 0> 1279 inline _LIBCPP_INLINE_VISIBILITY 1280 void swap(variant& __that) noexcept( 1281 __all<(is_nothrow_move_constructible_v<_Types> && 1282 is_nothrow_swappable_v<_Types>)...>::value) { 1283 __impl.__swap(__that.__impl); 1284 } 1285 1286private: 1287 __variant_detail::__impl<_Types...> __impl; 1288 1289 friend struct __variant_detail::__access::__variant; 1290 friend struct __variant_detail::__visitation::__variant; 1291}; 1292 1293template <size_t _Ip, class... _Types> 1294inline _LIBCPP_INLINE_VISIBILITY 1295constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept { 1296 return __v.index() == _Ip; 1297} 1298 1299template <class _Tp, class... _Types> 1300inline _LIBCPP_INLINE_VISIBILITY 1301constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept { 1302 return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 1303} 1304 1305template <size_t _Ip, class _Vp> 1306inline _LIBCPP_INLINE_VISIBILITY 1307static constexpr auto&& __generic_get(_Vp&& __v) { 1308 using __variant_detail::__access::__variant; 1309 if (!__holds_alternative<_Ip>(__v)) { 1310 __throw_bad_variant_access(); 1311 } 1312 return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value; 1313} 1314 1315template <size_t _Ip, class... _Types> 1316inline _LIBCPP_INLINE_VISIBILITY 1317constexpr variant_alternative_t<_Ip, variant<_Types...>>& get( 1318 variant<_Types...>& __v) { 1319 static_assert(_Ip < sizeof...(_Types)); 1320 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1321 return __generic_get<_Ip>(__v); 1322} 1323 1324template <size_t _Ip, class... _Types> 1325inline _LIBCPP_INLINE_VISIBILITY 1326constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get( 1327 variant<_Types...>&& __v) { 1328 static_assert(_Ip < sizeof...(_Types)); 1329 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1330 return __generic_get<_Ip>(_VSTD::move(__v)); 1331} 1332 1333template <size_t _Ip, class... _Types> 1334inline _LIBCPP_INLINE_VISIBILITY 1335constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get( 1336 const variant<_Types...>& __v) { 1337 static_assert(_Ip < sizeof...(_Types)); 1338 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1339 return __generic_get<_Ip>(__v); 1340} 1341 1342template <size_t _Ip, class... _Types> 1343inline _LIBCPP_INLINE_VISIBILITY 1344constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get( 1345 const variant<_Types...>&& __v) { 1346 static_assert(_Ip < sizeof...(_Types)); 1347 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1348 return __generic_get<_Ip>(_VSTD::move(__v)); 1349} 1350 1351template <class _Tp, class... _Types> 1352inline _LIBCPP_INLINE_VISIBILITY 1353constexpr _Tp& get(variant<_Types...>& __v) { 1354 static_assert(!is_void_v<_Tp>); 1355 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 1356} 1357 1358template <class _Tp, class... _Types> 1359inline _LIBCPP_INLINE_VISIBILITY 1360constexpr _Tp&& get(variant<_Types...>&& __v) { 1361 static_assert(!is_void_v<_Tp>); 1362 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>( 1363 _VSTD::move(__v)); 1364} 1365 1366template <class _Tp, class... _Types> 1367inline _LIBCPP_INLINE_VISIBILITY 1368constexpr const _Tp& get(const variant<_Types...>& __v) { 1369 static_assert(!is_void_v<_Tp>); 1370 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 1371} 1372 1373template <class _Tp, class... _Types> 1374inline _LIBCPP_INLINE_VISIBILITY 1375constexpr const _Tp&& get(const variant<_Types...>&& __v) { 1376 static_assert(!is_void_v<_Tp>); 1377 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>( 1378 _VSTD::move(__v)); 1379} 1380 1381template <size_t _Ip, class _Vp> 1382inline _LIBCPP_INLINE_VISIBILITY 1383constexpr auto* __generic_get_if(_Vp* __v) noexcept { 1384 using __variant_detail::__access::__variant; 1385 return __v && __holds_alternative<_Ip>(*__v) 1386 ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value) 1387 : nullptr; 1388} 1389 1390template <size_t _Ip, class... _Types> 1391inline _LIBCPP_INLINE_VISIBILITY 1392constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>> 1393get_if(variant<_Types...>* __v) noexcept { 1394 static_assert(_Ip < sizeof...(_Types)); 1395 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1396 return __generic_get_if<_Ip>(__v); 1397} 1398 1399template <size_t _Ip, class... _Types> 1400inline _LIBCPP_INLINE_VISIBILITY 1401constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>> 1402get_if(const variant<_Types...>* __v) noexcept { 1403 static_assert(_Ip < sizeof...(_Types)); 1404 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>); 1405 return __generic_get_if<_Ip>(__v); 1406} 1407 1408template <class _Tp, class... _Types> 1409inline _LIBCPP_INLINE_VISIBILITY 1410constexpr add_pointer_t<_Tp> 1411get_if(variant<_Types...>* __v) noexcept { 1412 static_assert(!is_void_v<_Tp>); 1413 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 1414} 1415 1416template <class _Tp, class... _Types> 1417inline _LIBCPP_INLINE_VISIBILITY 1418constexpr add_pointer_t<const _Tp> 1419get_if(const variant<_Types...>* __v) noexcept { 1420 static_assert(!is_void_v<_Tp>); 1421 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v); 1422} 1423 1424template <class... _Types> 1425inline _LIBCPP_INLINE_VISIBILITY 1426constexpr bool operator==(const variant<_Types...>& __lhs, 1427 const variant<_Types...>& __rhs) { 1428 using __variant_detail::__visitation::__variant; 1429 if (__lhs.index() != __rhs.index()) return false; 1430 if (__lhs.valueless_by_exception()) return true; 1431 return __variant::__visit_value_at(__lhs.index(), equal_to<>{}, __lhs, __rhs); 1432} 1433 1434template <class... _Types> 1435inline _LIBCPP_INLINE_VISIBILITY 1436constexpr bool operator!=(const variant<_Types...>& __lhs, 1437 const variant<_Types...>& __rhs) { 1438 using __variant_detail::__visitation::__variant; 1439 if (__lhs.index() != __rhs.index()) return true; 1440 if (__lhs.valueless_by_exception()) return false; 1441 return __variant::__visit_value_at( 1442 __lhs.index(), not_equal_to<>{}, __lhs, __rhs); 1443} 1444 1445template <class... _Types> 1446inline _LIBCPP_INLINE_VISIBILITY 1447constexpr bool operator<(const variant<_Types...>& __lhs, 1448 const variant<_Types...>& __rhs) { 1449 using __variant_detail::__visitation::__variant; 1450 if (__rhs.valueless_by_exception()) return false; 1451 if (__lhs.valueless_by_exception()) return true; 1452 if (__lhs.index() < __rhs.index()) return true; 1453 if (__lhs.index() > __rhs.index()) return false; 1454 return __variant::__visit_value_at(__lhs.index(), less<>{}, __lhs, __rhs); 1455} 1456 1457template <class... _Types> 1458inline _LIBCPP_INLINE_VISIBILITY 1459constexpr bool operator>(const variant<_Types...>& __lhs, 1460 const variant<_Types...>& __rhs) { 1461 using __variant_detail::__visitation::__variant; 1462 if (__lhs.valueless_by_exception()) return false; 1463 if (__rhs.valueless_by_exception()) return true; 1464 if (__lhs.index() > __rhs.index()) return true; 1465 if (__lhs.index() < __rhs.index()) return false; 1466 return __variant::__visit_value_at(__lhs.index(), greater<>{}, __lhs, __rhs); 1467} 1468 1469template <class... _Types> 1470inline _LIBCPP_INLINE_VISIBILITY 1471constexpr bool operator<=(const variant<_Types...>& __lhs, 1472 const variant<_Types...>& __rhs) { 1473 using __variant_detail::__visitation::__variant; 1474 if (__lhs.valueless_by_exception()) return true; 1475 if (__rhs.valueless_by_exception()) return false; 1476 if (__lhs.index() < __rhs.index()) return true; 1477 if (__lhs.index() > __rhs.index()) return false; 1478 return __variant::__visit_value_at( 1479 __lhs.index(), less_equal<>{}, __lhs, __rhs); 1480} 1481 1482template <class... _Types> 1483inline _LIBCPP_INLINE_VISIBILITY 1484constexpr bool operator>=(const variant<_Types...>& __lhs, 1485 const variant<_Types...>& __rhs) { 1486 using __variant_detail::__visitation::__variant; 1487 if (__rhs.valueless_by_exception()) return true; 1488 if (__lhs.valueless_by_exception()) return false; 1489 if (__lhs.index() > __rhs.index()) return true; 1490 if (__lhs.index() < __rhs.index()) return false; 1491 return __variant::__visit_value_at( 1492 __lhs.index(), greater_equal<>{}, __lhs, __rhs); 1493} 1494 1495template <class _Visitor, class... _Vs> 1496inline _LIBCPP_INLINE_VISIBILITY 1497constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) { 1498 using __variant_detail::__visitation::__variant; 1499 bool __results[] = {__vs.valueless_by_exception()...}; 1500 for (bool __result : __results) { 1501 if (__result) { 1502 __throw_bad_variant_access(); 1503 } 1504 } 1505 return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor), 1506 _VSTD::forward<_Vs>(__vs)...); 1507} 1508 1509struct _LIBCPP_TEMPLATE_VIS monostate {}; 1510 1511inline _LIBCPP_INLINE_VISIBILITY 1512constexpr bool operator<(monostate, monostate) noexcept { return false; } 1513 1514inline _LIBCPP_INLINE_VISIBILITY 1515constexpr bool operator>(monostate, monostate) noexcept { return false; } 1516 1517inline _LIBCPP_INLINE_VISIBILITY 1518constexpr bool operator<=(monostate, monostate) noexcept { return true; } 1519 1520inline _LIBCPP_INLINE_VISIBILITY 1521constexpr bool operator>=(monostate, monostate) noexcept { return true; } 1522 1523inline _LIBCPP_INLINE_VISIBILITY 1524constexpr bool operator==(monostate, monostate) noexcept { return true; } 1525 1526inline _LIBCPP_INLINE_VISIBILITY 1527constexpr bool operator!=(monostate, monostate) noexcept { return false; } 1528 1529template <class... _Types> 1530inline _LIBCPP_INLINE_VISIBILITY 1531auto swap(variant<_Types...>& __lhs, 1532 variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs))) 1533 -> decltype(__lhs.swap(__rhs)) { 1534 __lhs.swap(__rhs); 1535} 1536 1537template <class... _Types> 1538struct _LIBCPP_TEMPLATE_VIS hash< 1539 __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> { 1540 using argument_type = variant<_Types...>; 1541 using result_type = size_t; 1542 1543 inline _LIBCPP_INLINE_VISIBILITY 1544 result_type operator()(const argument_type& __v) const { 1545 using __variant_detail::__visitation::__variant; 1546 size_t __res = 1547 __v.valueless_by_exception() 1548 ? 299792458 // Random value chosen by the universe upon creation 1549 : __variant::__visit_alt( 1550 [](const auto& __alt) { 1551 using __alt_type = decay_t<decltype(__alt)>; 1552 using __value_type = remove_const_t< 1553 typename __alt_type::__value_type>; 1554 return hash<__value_type>{}(__alt.__value); 1555 }, 1556 __v); 1557 return __hash_combine(__res, hash<size_t>{}(__v.index())); 1558 } 1559}; 1560 1561template <> 1562struct _LIBCPP_TEMPLATE_VIS hash<monostate> { 1563 using argument_type = monostate; 1564 using result_type = size_t; 1565 1566 inline _LIBCPP_INLINE_VISIBILITY 1567 result_type operator()(const argument_type&) const _NOEXCEPT { 1568 return 66740831; // return a fundamentally attractive random value. 1569 } 1570}; 1571 1572#endif // _LIBCPP_STD_VER > 14 1573 1574_LIBCPP_END_NAMESPACE_STD 1575 1576#endif // _LIBCPP_VARIANT 1577