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