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