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