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