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