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