2 //===------------------------------ variant -------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_VARIANT
12 #define _LIBCPP_VARIANT
19 // 20.7.2, class template variant
20 template <class... Types>
24 // 20.7.2.1, constructors
25 constexpr variant() noexcept(see below);
26 variant(const variant&);
27 variant(variant&&) noexcept(see below);
29 template <class T> constexpr variant(T&&) noexcept(see below);
31 template <class T, class... Args>
32 constexpr explicit variant(in_place_type_t<T>, Args&&...);
34 template <class T, class U, class... Args>
35 constexpr explicit variant(
36 in_place_type_t<T>, initializer_list<U>, Args&&...);
38 template <size_t I, class... Args>
39 constexpr explicit variant(in_place_index_t<I>, Args&&...);
41 template <size_t I, class U, class... Args>
42 constexpr explicit variant(
43 in_place_index_t<I>, initializer_list<U>, Args&&...);
45 // 20.7.2.2, destructor
48 // 20.7.2.3, assignment
49 variant& operator=(const variant&);
50 variant& operator=(variant&&) noexcept(see below);
52 template <class T> variant& operator=(T&&) noexcept(see below);
54 // 20.7.2.4, modifiers
55 template <class T, class... Args>
56 T& emplace(Args&&...);
58 template <class T, class U, class... Args>
59 T& emplace(initializer_list<U>, Args&&...);
61 template <size_t I, class... Args>
62 variant_alternative_t<I, variant>& emplace(Args&&...);
64 template <size_t I, class U, class... Args>
65 variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
67 // 20.7.2.5, value status
68 constexpr bool valueless_by_exception() const noexcept;
69 constexpr size_t index() const noexcept;
72 void swap(variant&) noexcept(see below);
75 // 20.7.3, variant helper classes
76 template <class T> struct variant_size; // undefined
79 inline constexpr size_t variant_size_v = variant_size<T>::value;
81 template <class T> struct variant_size<const T>;
82 template <class T> struct variant_size<volatile T>;
83 template <class T> struct variant_size<const volatile T>;
85 template <class... Types>
86 struct variant_size<variant<Types...>>;
88 template <size_t I, class T> struct variant_alternative; // undefined
90 template <size_t I, class T>
91 using variant_alternative_t = typename variant_alternative<I, T>::type;
93 template <size_t I, class T> struct variant_alternative<I, const T>;
94 template <size_t I, class T> struct variant_alternative<I, volatile T>;
95 template <size_t I, class T> struct variant_alternative<I, const volatile T>;
97 template <size_t I, class... Types>
98 struct variant_alternative<I, variant<Types...>>;
100 inline constexpr size_t variant_npos = -1;
102 // 20.7.4, value access
103 template <class T, class... Types>
104 constexpr bool holds_alternative(const variant<Types...>&) noexcept;
106 template <size_t I, class... Types>
107 constexpr variant_alternative_t<I, variant<Types...>>&
108 get(variant<Types...>&);
110 template <size_t I, class... Types>
111 constexpr variant_alternative_t<I, variant<Types...>>&&
112 get(variant<Types...>&&);
114 template <size_t I, class... Types>
115 constexpr variant_alternative_t<I, variant<Types...>> const&
116 get(const variant<Types...>&);
118 template <size_t I, class... Types>
119 constexpr variant_alternative_t<I, variant<Types...>> const&&
120 get(const variant<Types...>&&);
122 template <class T, class... Types>
123 constexpr T& get(variant<Types...>&);
125 template <class T, class... Types>
126 constexpr T&& get(variant<Types...>&&);
128 template <class T, class... Types>
129 constexpr const T& get(const variant<Types...>&);
131 template <class T, class... Types>
132 constexpr const T&& get(const variant<Types...>&&);
134 template <size_t I, class... Types>
135 constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
136 get_if(variant<Types...>*) noexcept;
138 template <size_t I, class... Types>
139 constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
140 get_if(const variant<Types...>*) noexcept;
142 template <class T, class... Types>
143 constexpr add_pointer_t<T>
144 get_if(variant<Types...>*) noexcept;
146 template <class T, class... Types>
147 constexpr add_pointer_t<const T>
148 get_if(const variant<Types...>*) noexcept;
150 // 20.7.5, relational operators
151 template <class... Types>
152 constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
154 template <class... Types>
155 constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
157 template <class... Types>
158 constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
160 template <class... Types>
161 constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
163 template <class... Types>
164 constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
166 template <class... Types>
167 constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
169 // 20.7.6, visitation
170 template <class Visitor, class... Variants>
171 constexpr see below visit(Visitor&&, Variants&&...);
173 // 20.7.7, class monostate
176 // 20.7.8, monostate relational operators
177 constexpr bool operator<(monostate, monostate) noexcept;
178 constexpr bool operator>(monostate, monostate) noexcept;
179 constexpr bool operator<=(monostate, monostate) noexcept;
180 constexpr bool operator>=(monostate, monostate) noexcept;
181 constexpr bool operator==(monostate, monostate) noexcept;
182 constexpr bool operator!=(monostate, monostate) noexcept;
184 // 20.7.9, specialized algorithms
185 template <class... Types>
186 void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
188 // 20.7.10, class bad_variant_access
189 class bad_variant_access;
191 // 20.7.11, hash support
192 template <class T> struct hash;
193 template <class... Types> struct hash<variant<Types...>>;
194 template <> struct hash<monostate>;
204 #include <functional>
205 #include <initializer_list>
208 #include <type_traits>
212 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
213 #pragma GCC system_header
217 #include <__undef_macros>
219 namespace std { // explicitly not using versioning namespace
221 class _LIBCPP_EXCEPTION_ABI bad_variant_access : public exception {
223 virtual const char* what() const _NOEXCEPT;
228 _LIBCPP_BEGIN_NAMESPACE_STD
230 #if _LIBCPP_STD_VER > 14
233 inline _LIBCPP_INLINE_VISIBILITY
234 void __throw_bad_variant_access() {
235 #ifndef _LIBCPP_NO_EXCEPTIONS
236 throw bad_variant_access();
242 template <class... _Types>
243 class _LIBCPP_TEMPLATE_VIS variant;
246 struct _LIBCPP_TEMPLATE_VIS variant_size;
249 _LIBCPP_INLINE_VAR constexpr size_t variant_size_v = variant_size<_Tp>::value;
252 struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
255 struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
258 struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
259 : variant_size<_Tp> {};
261 template <class... _Types>
262 struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
263 : integral_constant<size_t, sizeof...(_Types)> {};
265 template <size_t _Ip, class _Tp>
266 struct _LIBCPP_TEMPLATE_VIS variant_alternative;
268 template <size_t _Ip, class _Tp>
269 using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
271 template <size_t _Ip, class _Tp>
272 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
273 : add_const<variant_alternative_t<_Ip, _Tp>> {};
275 template <size_t _Ip, class _Tp>
276 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
277 : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
279 template <size_t _Ip, class _Tp>
280 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
281 : add_cv<variant_alternative_t<_Ip, _Tp>> {};
283 template <size_t _Ip, class... _Types>
284 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
285 static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
286 using type = __type_pack_element<_Ip, _Types...>;
289 _LIBCPP_INLINE_VAR constexpr size_t variant_npos = static_cast<size_t>(-1);
291 constexpr int __choose_index_type(unsigned int __num_elem) {
292 if (__num_elem < std::numeric_limits<unsigned char>::max())
294 if (__num_elem < std::numeric_limits<unsigned short>::max())
299 template <size_t _NumAlts>
300 using __variant_index_t =
301 #ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
304 std::tuple_element_t<
305 __choose_index_type(_NumAlts),
306 std::tuple<unsigned char, unsigned short, unsigned int>
310 template <class _IndexType>
311 constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
313 namespace __find_detail {
315 template <class _Tp, class... _Types>
316 inline _LIBCPP_INLINE_VISIBILITY
317 constexpr size_t __find_index() {
318 constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
319 size_t __result = __not_found;
320 for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
321 if (__matches[__i]) {
322 if (__result != __not_found) {
331 template <size_t _Index>
332 struct __find_unambiguous_index_sfinae_impl
333 : integral_constant<size_t, _Index> {};
336 struct __find_unambiguous_index_sfinae_impl<__not_found> {};
339 struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
341 template <class _Tp, class... _Types>
342 struct __find_unambiguous_index_sfinae
343 : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
345 } // namespace __find_detail
347 namespace __variant_detail {
349 struct __valueless_t {};
351 enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
353 template <typename _Tp,
354 template <typename> class _IsTriviallyAvailable,
355 template <typename> class _IsAvailable>
356 constexpr _Trait __trait =
357 _IsTriviallyAvailable<_Tp>::value
358 ? _Trait::_TriviallyAvailable
359 : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
361 inline _LIBCPP_INLINE_VISIBILITY
362 constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
363 _Trait __result = _Trait::_TriviallyAvailable;
364 for (_Trait __t : __traits) {
365 if (static_cast<int>(__t) > static_cast<int>(__result)) {
372 template <typename... _Types>
374 static constexpr _Trait __copy_constructible_trait =
375 __common_trait({__trait<_Types,
376 is_trivially_copy_constructible,
377 is_copy_constructible>...});
379 static constexpr _Trait __move_constructible_trait =
380 __common_trait({__trait<_Types,
381 is_trivially_move_constructible,
382 is_move_constructible>...});
384 static constexpr _Trait __copy_assignable_trait = __common_trait(
385 {__copy_constructible_trait,
386 __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
388 static constexpr _Trait __move_assignable_trait = __common_trait(
389 {__move_constructible_trait,
390 __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
392 static constexpr _Trait __destructible_trait = __common_trait(
393 {__trait<_Types, is_trivially_destructible, is_destructible>...});
400 inline _LIBCPP_INLINE_VISIBILITY
401 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
402 return _VSTD::forward<_Vp>(__v).__head;
405 template <class _Vp, size_t _Ip>
406 inline _LIBCPP_INLINE_VISIBILITY
407 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
408 return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
413 template <size_t _Ip, class _Vp>
414 inline _LIBCPP_INLINE_VISIBILITY
415 static constexpr auto&& __get_alt(_Vp&& __v) {
416 return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
417 in_place_index<_Ip>);
422 template <size_t _Ip, class _Vp>
423 inline _LIBCPP_INLINE_VISIBILITY
424 static constexpr auto&& __get_alt(_Vp&& __v) {
425 return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
429 } // namespace __access
431 namespace __visitation {
434 template <class _Visitor, class... _Vs>
435 inline _LIBCPP_INLINE_VISIBILITY
436 static constexpr decltype(auto)
437 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
438 constexpr auto __fdiagonal =
439 __make_fdiagonal<_Visitor&&,
440 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
441 return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
442 _VSTD::forward<_Vs>(__vs).__as_base()...);
445 template <class _Visitor, class... _Vs>
446 inline _LIBCPP_INLINE_VISIBILITY
447 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
449 constexpr auto __fmatrix =
450 __make_fmatrix<_Visitor&&,
451 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
452 return __at(__fmatrix, __vs.index()...)(
453 _VSTD::forward<_Visitor>(__visitor),
454 _VSTD::forward<_Vs>(__vs).__as_base()...);
459 inline _LIBCPP_INLINE_VISIBILITY
460 static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
462 template <class _Tp, size_t _Np, typename... _Indices>
463 inline _LIBCPP_INLINE_VISIBILITY
464 static constexpr auto&& __at(const array<_Tp, _Np>& __elems,
465 size_t __index, _Indices... __indices) {
466 return __at(__elems[__index], __indices...);
469 template <class _Fp, class... _Fs>
470 static constexpr void __std_visit_visitor_return_type_check() {
472 __all<is_same_v<_Fp, _Fs>...>::value,
473 "`std::visit` requires the visitor to have a single return type.");
476 template <class... _Fs>
477 inline _LIBCPP_INLINE_VISIBILITY
478 static constexpr auto __make_farray(_Fs&&... __fs) {
479 __std_visit_visitor_return_type_check<__uncvref_t<_Fs>...>();
480 using __result = array<common_type_t<__uncvref_t<_Fs>...>, sizeof...(_Fs)>;
481 return __result{{_VSTD::forward<_Fs>(__fs)...}};
484 template <std::size_t... _Is>
485 struct __dispatcher {
486 template <class _Fp, class... _Vs>
487 inline _LIBCPP_INLINE_VISIBILITY
488 static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
489 return __invoke_constexpr(
490 static_cast<_Fp>(__f),
491 __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
495 template <class _Fp, class... _Vs, size_t... _Is>
496 inline _LIBCPP_INLINE_VISIBILITY
497 static constexpr auto __make_dispatch(index_sequence<_Is...>) {
498 return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
501 template <size_t _Ip, class _Fp, class... _Vs>
502 inline _LIBCPP_INLINE_VISIBILITY
503 static constexpr auto __make_fdiagonal_impl() {
504 return __make_dispatch<_Fp, _Vs...>(
505 index_sequence<(__identity<_Vs>{}, _Ip)...>{});
508 template <class _Fp, class... _Vs, size_t... _Is>
509 inline _LIBCPP_INLINE_VISIBILITY
510 static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
511 return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
514 template <class _Fp, class _Vp, class... _Vs>
515 inline _LIBCPP_INLINE_VISIBILITY
516 static constexpr auto __make_fdiagonal() {
517 constexpr size_t _Np = __uncvref_t<_Vp>::__size();
518 static_assert(__all<(_Np == __uncvref_t<_Vs>::__size())...>::value);
519 return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
522 template <class _Fp, class... _Vs, size_t... _Is>
523 inline _LIBCPP_INLINE_VISIBILITY
524 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
525 return __make_dispatch<_Fp, _Vs...>(__is);
528 template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
529 inline _LIBCPP_INLINE_VISIBILITY
530 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
531 index_sequence<_Js...>,
533 return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
534 index_sequence<_Is..., _Js>{}, __ls...)...);
537 template <class _Fp, class... _Vs>
538 inline _LIBCPP_INLINE_VISIBILITY
539 static constexpr auto __make_fmatrix() {
540 return __make_fmatrix_impl<_Fp, _Vs...>(
541 index_sequence<>{}, make_index_sequence<__uncvref_t<_Vs>::__size()>{}...);
546 template <class _Visitor, class... _Vs>
547 inline _LIBCPP_INLINE_VISIBILITY
548 static constexpr decltype(auto)
549 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
550 return __base::__visit_alt_at(__index,
551 _VSTD::forward<_Visitor>(__visitor),
552 _VSTD::forward<_Vs>(__vs).__impl...);
555 template <class _Visitor, class... _Vs>
556 inline _LIBCPP_INLINE_VISIBILITY
557 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
559 return __base::__visit_alt(_VSTD::forward<_Visitor>(__visitor),
560 _VSTD::forward<_Vs>(__vs).__impl...);
563 template <class _Visitor, class... _Vs>
564 inline _LIBCPP_INLINE_VISIBILITY
565 static constexpr decltype(auto)
566 __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
567 return __visit_alt_at(
569 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
570 _VSTD::forward<_Vs>(__vs)...);
573 template <class _Visitor, class... _Vs>
574 inline _LIBCPP_INLINE_VISIBILITY
575 static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
578 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
579 _VSTD::forward<_Vs>(__vs)...);
583 template <class _Visitor, class... _Values>
584 static constexpr void __std_visit_exhaustive_visitor_check() {
585 static_assert(is_invocable_v<_Visitor, _Values...>,
586 "`std::visit` requires the visitor to be exhaustive.");
589 template <class _Visitor>
590 struct __value_visitor {
591 template <class... _Alts>
592 inline _LIBCPP_INLINE_VISIBILITY
593 constexpr decltype(auto) operator()(_Alts&&... __alts) const {
594 __std_visit_exhaustive_visitor_check<
596 decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
597 return __invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
598 _VSTD::forward<_Alts>(__alts).__value...);
600 _Visitor&& __visitor;
603 template <class _Visitor>
604 inline _LIBCPP_INLINE_VISIBILITY
605 static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
606 return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
610 } // namespace __visitation
612 template <size_t _Index, class _Tp>
613 struct _LIBCPP_TEMPLATE_VIS __alt {
614 using __value_type = _Tp;
616 template <class... _Args>
617 inline _LIBCPP_INLINE_VISIBILITY
618 explicit constexpr __alt(in_place_t, _Args&&... __args)
619 : __value(_VSTD::forward<_Args>(__args)...) {}
621 __value_type __value;
624 template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
625 union _LIBCPP_TEMPLATE_VIS __union;
627 template <_Trait _DestructibleTrait, size_t _Index>
628 union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
630 #define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \
631 template <size_t _Index, class _Tp, class... _Types> \
632 union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \
637 inline _LIBCPP_INLINE_VISIBILITY \
638 explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
640 template <class... _Args> \
641 inline _LIBCPP_INLINE_VISIBILITY \
642 explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
643 : __head(in_place, _VSTD::forward<_Args>(__args)...) {} \
645 template <size_t _Ip, class... _Args> \
646 inline _LIBCPP_INLINE_VISIBILITY \
647 explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
648 : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
650 __union(const __union&) = default; \
651 __union(__union&&) = default; \
655 __union& operator=(const __union&) = default; \
656 __union& operator=(__union&&) = default; \
660 __alt<_Index, _Tp> __head; \
661 __union<destructible_trait, _Index + 1, _Types...> __tail; \
663 friend struct __access::__union; \
666 _LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
667 _LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
668 _LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
670 #undef _LIBCPP_VARIANT_UNION
672 template <_Trait _DestructibleTrait, class... _Types>
673 class _LIBCPP_TEMPLATE_VIS __base {
675 using __index_t = __variant_index_t<sizeof...(_Types)>;
677 inline _LIBCPP_INLINE_VISIBILITY
678 explicit constexpr __base(__valueless_t tag) noexcept
679 : __data(tag), __index(__variant_npos<__index_t>) {}
681 template <size_t _Ip, class... _Args>
682 inline _LIBCPP_INLINE_VISIBILITY
683 explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
685 __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
688 inline _LIBCPP_INLINE_VISIBILITY
689 constexpr bool valueless_by_exception() const noexcept {
690 return index() == variant_npos;
693 inline _LIBCPP_INLINE_VISIBILITY
694 constexpr size_t index() const noexcept {
695 return __index == __variant_npos<__index_t> ? variant_npos : __index;
699 inline _LIBCPP_INLINE_VISIBILITY
700 constexpr auto&& __as_base() & { return *this; }
702 inline _LIBCPP_INLINE_VISIBILITY
703 constexpr auto&& __as_base() && { return _VSTD::move(*this); }
705 inline _LIBCPP_INLINE_VISIBILITY
706 constexpr auto&& __as_base() const & { return *this; }
708 inline _LIBCPP_INLINE_VISIBILITY
709 constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
711 inline _LIBCPP_INLINE_VISIBILITY
712 static constexpr size_t __size() { return sizeof...(_Types); }
714 __union<_DestructibleTrait, 0, _Types...> __data;
717 friend struct __access::__base;
718 friend struct __visitation::__base;
721 template <class _Traits, _Trait = _Traits::__destructible_trait>
722 class _LIBCPP_TEMPLATE_VIS __destructor;
724 #define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \
725 template <class... _Types> \
726 class _LIBCPP_TEMPLATE_VIS __destructor<__traits<_Types...>, \
727 destructible_trait> \
728 : public __base<destructible_trait, _Types...> { \
729 using __base_type = __base<destructible_trait, _Types...>; \
730 using __index_t = typename __base_type::__index_t; \
733 using __base_type::__base_type; \
734 using __base_type::operator=; \
736 __destructor(const __destructor&) = default; \
737 __destructor(__destructor&&) = default; \
739 __destructor& operator=(const __destructor&) = default; \
740 __destructor& operator=(__destructor&&) = default; \
743 inline _LIBCPP_INLINE_VISIBILITY \
747 _LIBCPP_VARIANT_DESTRUCTOR(
748 _Trait::_TriviallyAvailable,
749 ~__destructor() = default;,
750 void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
752 _LIBCPP_VARIANT_DESTRUCTOR(
754 ~__destructor() { __destroy(); },
755 void __destroy() noexcept {
756 if (!this->valueless_by_exception()) {
757 __visitation::__base::__visit_alt(
758 [](auto& __alt) noexcept {
759 using __alt_type = __uncvref_t<decltype(__alt)>;
764 this->__index = __variant_npos<__index_t>;
767 _LIBCPP_VARIANT_DESTRUCTOR(
768 _Trait::_Unavailable,
769 ~__destructor() = delete;,
770 void __destroy() noexcept = delete;);
772 #undef _LIBCPP_VARIANT_DESTRUCTOR
774 template <class _Traits>
775 class _LIBCPP_TEMPLATE_VIS __constructor : public __destructor<_Traits> {
776 using __base_type = __destructor<_Traits>;
779 using __base_type::__base_type;
780 using __base_type::operator=;
783 template <size_t _Ip, class _Tp, class... _Args>
784 inline _LIBCPP_INLINE_VISIBILITY
785 static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
786 ::new ((void*)_VSTD::addressof(__a))
787 __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
791 template <class _Rhs>
792 inline _LIBCPP_INLINE_VISIBILITY
793 static void __generic_construct(__constructor& __lhs, _Rhs&& __rhs) {
795 if (!__rhs.valueless_by_exception()) {
796 __visitation::__base::__visit_alt_at(
798 [](auto& __lhs_alt, auto&& __rhs_alt) {
801 _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
803 __lhs, _VSTD::forward<_Rhs>(__rhs));
804 __lhs.__index = __rhs.index();
809 template <class _Traits, _Trait = _Traits::__move_constructible_trait>
810 class _LIBCPP_TEMPLATE_VIS __move_constructor;
812 #define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \
814 template <class... _Types> \
815 class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \
816 move_constructible_trait> \
817 : public __constructor<__traits<_Types...>> { \
818 using __base_type = __constructor<__traits<_Types...>>; \
821 using __base_type::__base_type; \
822 using __base_type::operator=; \
824 __move_constructor(const __move_constructor&) = default; \
826 ~__move_constructor() = default; \
827 __move_constructor& operator=(const __move_constructor&) = default; \
828 __move_constructor& operator=(__move_constructor&&) = default; \
831 _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
832 _Trait::_TriviallyAvailable,
833 __move_constructor(__move_constructor&& __that) = default;);
835 _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
837 __move_constructor(__move_constructor&& __that) noexcept(
838 __all<is_nothrow_move_constructible_v<_Types>...>::value)
839 : __move_constructor(__valueless_t{}) {
840 this->__generic_construct(*this, _VSTD::move(__that));
843 _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
844 _Trait::_Unavailable,
845 __move_constructor(__move_constructor&&) = delete;);
847 #undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
849 template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
850 class _LIBCPP_TEMPLATE_VIS __copy_constructor;
852 #define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \
854 template <class... _Types> \
855 class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \
856 copy_constructible_trait> \
857 : public __move_constructor<__traits<_Types...>> { \
858 using __base_type = __move_constructor<__traits<_Types...>>; \
861 using __base_type::__base_type; \
862 using __base_type::operator=; \
865 __copy_constructor(__copy_constructor&&) = default; \
866 ~__copy_constructor() = default; \
867 __copy_constructor& operator=(const __copy_constructor&) = default; \
868 __copy_constructor& operator=(__copy_constructor&&) = default; \
871 _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
872 _Trait::_TriviallyAvailable,
873 __copy_constructor(const __copy_constructor& __that) = default;);
875 _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
877 __copy_constructor(const __copy_constructor& __that)
878 : __copy_constructor(__valueless_t{}) {
879 this->__generic_construct(*this, __that);
882 _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
883 _Trait::_Unavailable,
884 __copy_constructor(const __copy_constructor&) = delete;);
886 #undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
888 template <class _Traits>
889 class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
890 using __base_type = __copy_constructor<_Traits>;
893 using __base_type::__base_type;
894 using __base_type::operator=;
896 template <size_t _Ip, class... _Args>
897 inline _LIBCPP_INLINE_VISIBILITY
898 auto& __emplace(_Args&&... __args) {
900 auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
901 _VSTD::forward<_Args>(__args)...);
907 template <size_t _Ip, class _Tp, class _Arg>
908 inline _LIBCPP_INLINE_VISIBILITY
909 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
910 if (this->index() == _Ip) {
911 __a.__value = _VSTD::forward<_Arg>(__arg);
914 void operator()(true_type) const {
915 __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
917 void operator()(false_type) const {
918 __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
920 __assignment* __this;
922 } __impl{this, _VSTD::forward<_Arg>(__arg)};
923 __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
924 !is_nothrow_move_constructible_v<_Tp>>{});
928 template <class _That>
929 inline _LIBCPP_INLINE_VISIBILITY
930 void __generic_assign(_That&& __that) {
931 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
933 } else if (__that.valueless_by_exception()) {
936 __visitation::__base::__visit_alt_at(
938 [this](auto& __this_alt, auto&& __that_alt) {
941 _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
943 *this, _VSTD::forward<_That>(__that));
948 template <class _Traits, _Trait = _Traits::__move_assignable_trait>
949 class _LIBCPP_TEMPLATE_VIS __move_assignment;
951 #define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \
953 template <class... _Types> \
954 class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \
955 move_assignable_trait> \
956 : public __assignment<__traits<_Types...>> { \
957 using __base_type = __assignment<__traits<_Types...>>; \
960 using __base_type::__base_type; \
961 using __base_type::operator=; \
963 __move_assignment(const __move_assignment&) = default; \
964 __move_assignment(__move_assignment&&) = default; \
965 ~__move_assignment() = default; \
966 __move_assignment& operator=(const __move_assignment&) = default; \
970 _LIBCPP_VARIANT_MOVE_ASSIGNMENT(
971 _Trait::_TriviallyAvailable,
972 __move_assignment& operator=(__move_assignment&& __that) = default;);
974 _LIBCPP_VARIANT_MOVE_ASSIGNMENT(
976 __move_assignment& operator=(__move_assignment&& __that) noexcept(
977 __all<(is_nothrow_move_constructible_v<_Types> &&
978 is_nothrow_move_assignable_v<_Types>)...>::value) {
979 this->__generic_assign(_VSTD::move(__that));
983 _LIBCPP_VARIANT_MOVE_ASSIGNMENT(
984 _Trait::_Unavailable,
985 __move_assignment& operator=(__move_assignment&&) = delete;);
987 #undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
989 template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
990 class _LIBCPP_TEMPLATE_VIS __copy_assignment;
992 #define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \
994 template <class... _Types> \
995 class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \
996 copy_assignable_trait> \
997 : public __move_assignment<__traits<_Types...>> { \
998 using __base_type = __move_assignment<__traits<_Types...>>; \
1001 using __base_type::__base_type; \
1002 using __base_type::operator=; \
1004 __copy_assignment(const __copy_assignment&) = default; \
1005 __copy_assignment(__copy_assignment&&) = default; \
1006 ~__copy_assignment() = default; \
1008 __copy_assignment& operator=(__copy_assignment&&) = default; \
1011 _LIBCPP_VARIANT_COPY_ASSIGNMENT(
1012 _Trait::_TriviallyAvailable,
1013 __copy_assignment& operator=(const __copy_assignment& __that) = default;);
1015 _LIBCPP_VARIANT_COPY_ASSIGNMENT(
1017 __copy_assignment& operator=(const __copy_assignment& __that) {
1018 this->__generic_assign(__that);
1022 _LIBCPP_VARIANT_COPY_ASSIGNMENT(
1023 _Trait::_Unavailable,
1024 __copy_assignment& operator=(const __copy_assignment&) = delete;);
1026 #undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1028 template <class... _Types>
1029 class _LIBCPP_TEMPLATE_VIS __impl
1030 : public __copy_assignment<__traits<_Types...>> {
1031 using __base_type = __copy_assignment<__traits<_Types...>>;
1034 using __base_type::__base_type;
1035 using __base_type::operator=;
1037 template <size_t _Ip, class _Arg>
1038 inline _LIBCPP_INLINE_VISIBILITY
1039 void __assign(_Arg&& __arg) {
1040 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
1041 _VSTD::forward<_Arg>(__arg));
1044 inline _LIBCPP_INLINE_VISIBILITY
1045 void __swap(__impl& __that) {
1046 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1048 } else if (this->index() == __that.index()) {
1049 __visitation::__base::__visit_alt_at(
1051 [](auto& __this_alt, auto& __that_alt) {
1053 swap(__this_alt.__value, __that_alt.__value);
1058 __impl* __lhs = this;
1059 __impl* __rhs = _VSTD::addressof(__that);
1060 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1061 _VSTD::swap(__lhs, __rhs);
1063 __impl __tmp(_VSTD::move(*__rhs));
1064 #ifndef _LIBCPP_NO_EXCEPTIONS
1065 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1066 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1067 // into `__rhs` and provide the strong exception safety guarentee.
1069 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1071 if (__tmp.__move_nothrow()) {
1072 this->__generic_construct(*__rhs, _VSTD::move(__tmp));
1077 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1079 this->__generic_construct(*__lhs, _VSTD::move(__tmp));
1084 inline _LIBCPP_INLINE_VISIBILITY
1085 bool __move_nothrow() const {
1086 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1087 return this->valueless_by_exception() || __results[this->index()];
1091 template <class... _Types>
1095 struct __overload<> { void operator()() const; };
1097 template <class _Tp, class... _Types>
1098 struct __overload<_Tp, _Types...> : __overload<_Types...> {
1099 using __overload<_Types...>::operator();
1100 __identity<_Tp> operator()(_Tp) const;
1103 template <class _Tp, class... _Types>
1104 using __best_match_t = typename result_of_t<__overload<_Types...>(_Tp&&)>::type;
1106 } // __variant_detail
1108 template <class... _Types>
1109 class _LIBCPP_TEMPLATE_VIS variant
1110 : private __sfinae_ctor_base<
1111 __all<is_copy_constructible_v<_Types>...>::value,
1112 __all<is_move_constructible_v<_Types>...>::value>,
1113 private __sfinae_assign_base<
1114 __all<(is_copy_constructible_v<_Types> &&
1115 is_copy_assignable_v<_Types>)...>::value,
1116 __all<(is_move_constructible_v<_Types> &&
1117 is_move_assignable_v<_Types>)...>::value> {
1118 static_assert(0 < sizeof...(_Types),
1119 "variant must consist of at least one alternative.");
1121 static_assert(__all<!is_array_v<_Types>...>::value,
1122 "variant can not have an array type as an alternative.");
1124 static_assert(__all<!is_reference_v<_Types>...>::value,
1125 "variant can not have a reference type as an alternative.");
1127 static_assert(__all<!is_void_v<_Types>...>::value,
1128 "variant can not have a void type as an alternative.");
1130 using __first_type = variant_alternative_t<0, variant>;
1133 template <bool _Dummy = true,
1134 enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1137 inline _LIBCPP_INLINE_VISIBILITY
1138 constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1139 : __impl(in_place_index<0>) {}
1141 variant(const variant&) = default;
1142 variant(variant&&) = default;
1146 enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
1147 enable_if_t<!__is_inplace_type<__uncvref_t<_Arg>>::value, int> = 0,
1148 enable_if_t<!__is_inplace_index<__uncvref_t<_Arg>>::value, int> = 0,
1149 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1151 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1152 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1153 inline _LIBCPP_INLINE_VISIBILITY
1154 constexpr variant(_Arg&& __arg) noexcept(
1155 is_nothrow_constructible_v<_Tp, _Arg>)
1156 : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
1158 template <size_t _Ip, class... _Args,
1159 class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
1160 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1161 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1162 inline _LIBCPP_INLINE_VISIBILITY
1163 explicit constexpr variant(
1164 in_place_index_t<_Ip>,
1165 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1166 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1172 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1173 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1174 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1176 inline _LIBCPP_INLINE_VISIBILITY
1177 explicit constexpr variant(
1178 in_place_index_t<_Ip>,
1179 initializer_list<_Up> __il,
1180 _Args&&... __args) noexcept(
1181 is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1182 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1188 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1189 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1190 inline _LIBCPP_INLINE_VISIBILITY
1191 explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1192 is_nothrow_constructible_v<_Tp, _Args...>)
1193 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1200 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1201 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1203 inline _LIBCPP_INLINE_VISIBILITY
1204 explicit constexpr variant(
1205 in_place_type_t<_Tp>,
1206 initializer_list<_Up> __il,
1207 _Args&&... __args) noexcept(
1208 is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1209 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1211 ~variant() = default;
1213 variant& operator=(const variant&) = default;
1214 variant& operator=(variant&&) = default;
1218 enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
1219 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1221 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1222 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
1224 inline _LIBCPP_INLINE_VISIBILITY
1225 variant& operator=(_Arg&& __arg) noexcept(
1226 is_nothrow_assignable_v<_Tp&, _Arg> &&
1227 is_nothrow_constructible_v<_Tp, _Arg>) {
1228 __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
1235 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1236 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1237 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1238 inline _LIBCPP_INLINE_VISIBILITY
1239 _Tp& emplace(_Args&&... __args) {
1240 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
1247 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1248 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1249 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1251 inline _LIBCPP_INLINE_VISIBILITY
1252 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1253 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
1260 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1261 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1262 inline _LIBCPP_INLINE_VISIBILITY
1263 _Tp& emplace(_Args&&... __args) {
1264 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
1272 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1273 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1275 inline _LIBCPP_INLINE_VISIBILITY
1276 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1277 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
1280 inline _LIBCPP_INLINE_VISIBILITY
1281 constexpr bool valueless_by_exception() const noexcept {
1282 return __impl.valueless_by_exception();
1285 inline _LIBCPP_INLINE_VISIBILITY
1286 constexpr size_t index() const noexcept { return __impl.index(); }
1292 __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1293 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1295 inline _LIBCPP_INLINE_VISIBILITY
1296 void swap(variant& __that) noexcept(
1297 __all<(is_nothrow_move_constructible_v<_Types> &&
1298 is_nothrow_swappable_v<_Types>)...>::value) {
1299 __impl.__swap(__that.__impl);
1303 __variant_detail::__impl<_Types...> __impl;
1305 friend struct __variant_detail::__access::__variant;
1306 friend struct __variant_detail::__visitation::__variant;
1309 template <size_t _Ip, class... _Types>
1310 inline _LIBCPP_INLINE_VISIBILITY
1311 constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1312 return __v.index() == _Ip;
1315 template <class _Tp, class... _Types>
1316 inline _LIBCPP_INLINE_VISIBILITY
1317 constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1318 return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1321 template <size_t _Ip, class _Vp>
1322 inline _LIBCPP_INLINE_VISIBILITY
1323 static constexpr auto&& __generic_get(_Vp&& __v) {
1324 using __variant_detail::__access::__variant;
1325 if (!__holds_alternative<_Ip>(__v)) {
1326 __throw_bad_variant_access();
1328 return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
1331 template <size_t _Ip, class... _Types>
1332 inline _LIBCPP_INLINE_VISIBILITY
1333 constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
1334 variant<_Types...>& __v) {
1335 static_assert(_Ip < sizeof...(_Types));
1336 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1337 return __generic_get<_Ip>(__v);
1340 template <size_t _Ip, class... _Types>
1341 inline _LIBCPP_INLINE_VISIBILITY
1342 constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
1343 variant<_Types...>&& __v) {
1344 static_assert(_Ip < sizeof...(_Types));
1345 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1346 return __generic_get<_Ip>(_VSTD::move(__v));
1349 template <size_t _Ip, class... _Types>
1350 inline _LIBCPP_INLINE_VISIBILITY
1351 constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
1352 const variant<_Types...>& __v) {
1353 static_assert(_Ip < sizeof...(_Types));
1354 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1355 return __generic_get<_Ip>(__v);
1358 template <size_t _Ip, class... _Types>
1359 inline _LIBCPP_INLINE_VISIBILITY
1360 constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
1361 const variant<_Types...>&& __v) {
1362 static_assert(_Ip < sizeof...(_Types));
1363 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1364 return __generic_get<_Ip>(_VSTD::move(__v));
1367 template <class _Tp, class... _Types>
1368 inline _LIBCPP_INLINE_VISIBILITY
1369 constexpr _Tp& get(variant<_Types...>& __v) {
1370 static_assert(!is_void_v<_Tp>);
1371 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1374 template <class _Tp, class... _Types>
1375 inline _LIBCPP_INLINE_VISIBILITY
1376 constexpr _Tp&& get(variant<_Types...>&& __v) {
1377 static_assert(!is_void_v<_Tp>);
1378 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1382 template <class _Tp, class... _Types>
1383 inline _LIBCPP_INLINE_VISIBILITY
1384 constexpr const _Tp& get(const variant<_Types...>& __v) {
1385 static_assert(!is_void_v<_Tp>);
1386 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1389 template <class _Tp, class... _Types>
1390 inline _LIBCPP_INLINE_VISIBILITY
1391 constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1392 static_assert(!is_void_v<_Tp>);
1393 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1397 template <size_t _Ip, class _Vp>
1398 inline _LIBCPP_INLINE_VISIBILITY
1399 constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1400 using __variant_detail::__access::__variant;
1401 return __v && __holds_alternative<_Ip>(*__v)
1402 ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
1406 template <size_t _Ip, class... _Types>
1407 inline _LIBCPP_INLINE_VISIBILITY
1408 constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1409 get_if(variant<_Types...>* __v) noexcept {
1410 static_assert(_Ip < sizeof...(_Types));
1411 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1412 return __generic_get_if<_Ip>(__v);
1415 template <size_t _Ip, class... _Types>
1416 inline _LIBCPP_INLINE_VISIBILITY
1417 constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1418 get_if(const variant<_Types...>* __v) noexcept {
1419 static_assert(_Ip < sizeof...(_Types));
1420 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1421 return __generic_get_if<_Ip>(__v);
1424 template <class _Tp, class... _Types>
1425 inline _LIBCPP_INLINE_VISIBILITY
1426 constexpr add_pointer_t<_Tp>
1427 get_if(variant<_Types...>* __v) noexcept {
1428 static_assert(!is_void_v<_Tp>);
1429 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1432 template <class _Tp, class... _Types>
1433 inline _LIBCPP_INLINE_VISIBILITY
1434 constexpr add_pointer_t<const _Tp>
1435 get_if(const variant<_Types...>* __v) noexcept {
1436 static_assert(!is_void_v<_Tp>);
1437 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1440 template <class... _Types>
1441 inline _LIBCPP_INLINE_VISIBILITY
1442 constexpr bool operator==(const variant<_Types...>& __lhs,
1443 const variant<_Types...>& __rhs) {
1444 using __variant_detail::__visitation::__variant;
1445 if (__lhs.index() != __rhs.index()) return false;
1446 if (__lhs.valueless_by_exception()) return true;
1447 return __variant::__visit_value_at(__lhs.index(), equal_to<>{}, __lhs, __rhs);
1450 template <class... _Types>
1451 inline _LIBCPP_INLINE_VISIBILITY
1452 constexpr bool operator!=(const variant<_Types...>& __lhs,
1453 const variant<_Types...>& __rhs) {
1454 using __variant_detail::__visitation::__variant;
1455 if (__lhs.index() != __rhs.index()) return true;
1456 if (__lhs.valueless_by_exception()) return false;
1457 return __variant::__visit_value_at(
1458 __lhs.index(), not_equal_to<>{}, __lhs, __rhs);
1461 template <class... _Types>
1462 inline _LIBCPP_INLINE_VISIBILITY
1463 constexpr bool operator<(const variant<_Types...>& __lhs,
1464 const variant<_Types...>& __rhs) {
1465 using __variant_detail::__visitation::__variant;
1466 if (__rhs.valueless_by_exception()) return false;
1467 if (__lhs.valueless_by_exception()) return true;
1468 if (__lhs.index() < __rhs.index()) return true;
1469 if (__lhs.index() > __rhs.index()) return false;
1470 return __variant::__visit_value_at(__lhs.index(), less<>{}, __lhs, __rhs);
1473 template <class... _Types>
1474 inline _LIBCPP_INLINE_VISIBILITY
1475 constexpr bool operator>(const variant<_Types...>& __lhs,
1476 const variant<_Types...>& __rhs) {
1477 using __variant_detail::__visitation::__variant;
1478 if (__lhs.valueless_by_exception()) return false;
1479 if (__rhs.valueless_by_exception()) return true;
1480 if (__lhs.index() > __rhs.index()) return true;
1481 if (__lhs.index() < __rhs.index()) return false;
1482 return __variant::__visit_value_at(__lhs.index(), greater<>{}, __lhs, __rhs);
1485 template <class... _Types>
1486 inline _LIBCPP_INLINE_VISIBILITY
1487 constexpr bool operator<=(const variant<_Types...>& __lhs,
1488 const variant<_Types...>& __rhs) {
1489 using __variant_detail::__visitation::__variant;
1490 if (__lhs.valueless_by_exception()) return true;
1491 if (__rhs.valueless_by_exception()) return false;
1492 if (__lhs.index() < __rhs.index()) return true;
1493 if (__lhs.index() > __rhs.index()) return false;
1494 return __variant::__visit_value_at(
1495 __lhs.index(), less_equal<>{}, __lhs, __rhs);
1498 template <class... _Types>
1499 inline _LIBCPP_INLINE_VISIBILITY
1500 constexpr bool operator>=(const variant<_Types...>& __lhs,
1501 const variant<_Types...>& __rhs) {
1502 using __variant_detail::__visitation::__variant;
1503 if (__rhs.valueless_by_exception()) return true;
1504 if (__lhs.valueless_by_exception()) return false;
1505 if (__lhs.index() > __rhs.index()) return true;
1506 if (__lhs.index() < __rhs.index()) return false;
1507 return __variant::__visit_value_at(
1508 __lhs.index(), greater_equal<>{}, __lhs, __rhs);
1511 template <class _Visitor, class... _Vs>
1512 inline _LIBCPP_INLINE_VISIBILITY
1513 constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
1514 using __variant_detail::__visitation::__variant;
1515 bool __results[] = {__vs.valueless_by_exception()...};
1516 for (bool __result : __results) {
1518 __throw_bad_variant_access();
1521 return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
1522 _VSTD::forward<_Vs>(__vs)...);
1525 struct _LIBCPP_TEMPLATE_VIS monostate {};
1527 inline _LIBCPP_INLINE_VISIBILITY
1528 constexpr bool operator<(monostate, monostate) noexcept { return false; }
1530 inline _LIBCPP_INLINE_VISIBILITY
1531 constexpr bool operator>(monostate, monostate) noexcept { return false; }
1533 inline _LIBCPP_INLINE_VISIBILITY
1534 constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1536 inline _LIBCPP_INLINE_VISIBILITY
1537 constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1539 inline _LIBCPP_INLINE_VISIBILITY
1540 constexpr bool operator==(monostate, monostate) noexcept { return true; }
1542 inline _LIBCPP_INLINE_VISIBILITY
1543 constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1545 template <class... _Types>
1546 inline _LIBCPP_INLINE_VISIBILITY
1547 auto swap(variant<_Types...>& __lhs,
1548 variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1549 -> decltype(__lhs.swap(__rhs)) {
1553 template <class... _Types>
1554 struct _LIBCPP_TEMPLATE_VIS hash<
1555 __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
1556 using argument_type = variant<_Types...>;
1557 using result_type = size_t;
1559 inline _LIBCPP_INLINE_VISIBILITY
1560 result_type operator()(const argument_type& __v) const {
1561 using __variant_detail::__visitation::__variant;
1563 __v.valueless_by_exception()
1564 ? 299792458 // Random value chosen by the universe upon creation
1565 : __variant::__visit_alt(
1566 [](const auto& __alt) {
1567 using __alt_type = __uncvref_t<decltype(__alt)>;
1568 using __value_type = remove_const_t<
1569 typename __alt_type::__value_type>;
1570 return hash<__value_type>{}(__alt.__value);
1573 return __hash_combine(__res, hash<size_t>{}(__v.index()));
1578 struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
1579 using argument_type = monostate;
1580 using result_type = size_t;
1582 inline _LIBCPP_INLINE_VISIBILITY
1583 result_type operator()(const argument_type&) const _NOEXCEPT {
1584 return 66740831; // return a fundamentally attractive random value.
1588 #endif // _LIBCPP_STD_VER > 14
1590 _LIBCPP_END_NAMESPACE_STD
1594 #endif // _LIBCPP_VARIANT