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