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