2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_VARIANT
11 #define _LIBCPP_VARIANT
18 // 20.7.2, class template variant
19 template <class... Types>
23 // 20.7.2.1, constructors
24 constexpr variant() noexcept(see below);
25 constexpr variant(const variant&);
26 constexpr variant(variant&&) noexcept(see below);
28 template <class T> constexpr variant(T&&) noexcept(see below);
30 template <class T, class... Args>
31 constexpr explicit variant(in_place_type_t<T>, Args&&...);
33 template <class T, class U, class... Args>
34 constexpr explicit variant(
35 in_place_type_t<T>, initializer_list<U>, Args&&...);
37 template <size_t I, class... Args>
38 constexpr explicit variant(in_place_index_t<I>, Args&&...);
40 template <size_t I, class U, class... Args>
41 constexpr explicit variant(
42 in_place_index_t<I>, initializer_list<U>, Args&&...);
44 // 20.7.2.2, destructor
47 // 20.7.2.3, assignment
48 constexpr variant& operator=(const variant&);
49 constexpr variant& operator=(variant&&) noexcept(see below);
51 template <class T> variant& operator=(T&&) noexcept(see below);
53 // 20.7.2.4, modifiers
54 template <class T, class... Args>
55 T& emplace(Args&&...);
57 template <class T, class U, class... Args>
58 T& emplace(initializer_list<U>, Args&&...);
60 template <size_t I, class... Args>
61 variant_alternative_t<I, variant>& emplace(Args&&...);
63 template <size_t I, class U, class... Args>
64 variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
66 // 20.7.2.5, value status
67 constexpr bool valueless_by_exception() const noexcept;
68 constexpr size_t index() const noexcept;
71 void swap(variant&) noexcept(see below);
74 // 20.7.3, variant helper classes
75 template <class T> struct variant_size; // undefined
78 inline constexpr size_t variant_size_v = variant_size<T>::value;
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>;
84 template <class... Types>
85 struct variant_size<variant<Types...>>;
87 template <size_t I, class T> struct variant_alternative; // undefined
89 template <size_t I, class T>
90 using variant_alternative_t = typename variant_alternative<I, T>::type;
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>;
96 template <size_t I, class... Types>
97 struct variant_alternative<I, variant<Types...>>;
99 inline constexpr size_t variant_npos = -1;
101 // 20.7.4, value access
102 template <class T, class... Types>
103 constexpr bool holds_alternative(const variant<Types...>&) noexcept;
105 template <size_t I, class... Types>
106 constexpr variant_alternative_t<I, variant<Types...>>&
107 get(variant<Types...>&);
109 template <size_t I, class... Types>
110 constexpr variant_alternative_t<I, variant<Types...>>&&
111 get(variant<Types...>&&);
113 template <size_t I, class... Types>
114 constexpr variant_alternative_t<I, variant<Types...>> const&
115 get(const variant<Types...>&);
117 template <size_t I, class... Types>
118 constexpr variant_alternative_t<I, variant<Types...>> const&&
119 get(const variant<Types...>&&);
121 template <class T, class... Types>
122 constexpr T& get(variant<Types...>&);
124 template <class T, class... Types>
125 constexpr T&& get(variant<Types...>&&);
127 template <class T, class... Types>
128 constexpr const T& get(const variant<Types...>&);
130 template <class T, class... Types>
131 constexpr const T&& get(const variant<Types...>&&);
133 template <size_t I, class... Types>
134 constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
135 get_if(variant<Types...>*) noexcept;
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;
141 template <class T, class... Types>
142 constexpr add_pointer_t<T>
143 get_if(variant<Types...>*) noexcept;
145 template <class T, class... Types>
146 constexpr add_pointer_t<const T>
147 get_if(const variant<Types...>*) noexcept;
149 // 20.7.5, relational operators
150 template <class... Types>
151 constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
153 template <class... Types>
154 constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
156 template <class... Types>
157 constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
159 template <class... Types>
160 constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
162 template <class... Types>
163 constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
165 template <class... Types>
166 constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
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
172 // 20.7.6, visitation
173 template <class Visitor, class... Variants>
174 constexpr see below visit(Visitor&&, Variants&&...);
176 template <class R, class Visitor, class... Variants>
177 constexpr R visit(Visitor&&, Variants&&...); // since C++20
179 // 20.7.7, class monostate
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
191 // 20.7.9, specialized algorithms
192 template <class... Types>
193 void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
195 // 20.7.10, class bad_variant_access
196 class bad_variant_access;
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>;
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>
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>
249 // standard-mandated includes
254 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
255 # pragma GCC system_header
259 #include <__undef_macros>
261 namespace std { // explicitly not using versioning namespace
263 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
265 const char* what() const _NOEXCEPT override;
270 _LIBCPP_BEGIN_NAMESPACE_STD
272 #if _LIBCPP_STD_VER >= 17
274 // Light N-dimensional array of function pointers. Used in place of std::array to avoid
275 // adding a dependency.
276 template<class _Tp, size_t _Size>
278 static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit");
279 _Tp __buf_[_Size] = {};
281 _LIBCPP_INLINE_VISIBILITY constexpr
282 const _Tp &operator[](size_t __n) const noexcept {
288 inline _LIBCPP_HIDE_FROM_ABI
289 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
290 void __throw_bad_variant_access() {
291 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
292 throw bad_variant_access();
294 _LIBCPP_VERBOSE_ABORT("bad_variant_access was thrown in -fno-exceptions mode");
298 template <class... _Types>
299 class _LIBCPP_TEMPLATE_VIS variant;
302 struct _LIBCPP_TEMPLATE_VIS variant_size;
305 inline constexpr size_t variant_size_v = variant_size<_Tp>::value;
308 struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
311 struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
314 struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
315 : variant_size<_Tp> {};
317 template <class... _Types>
318 struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
319 : integral_constant<size_t, sizeof...(_Types)> {};
321 template <size_t _Ip, class _Tp>
322 struct _LIBCPP_TEMPLATE_VIS variant_alternative;
324 template <size_t _Ip, class _Tp>
325 using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
327 template <size_t _Ip, class _Tp>
328 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
329 : add_const<variant_alternative_t<_Ip, _Tp>> {};
331 template <size_t _Ip, class _Tp>
332 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
333 : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
335 template <size_t _Ip, class _Tp>
336 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
337 : add_cv<variant_alternative_t<_Ip, _Tp>> {};
339 template <size_t _Ip, class... _Types>
340 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
341 static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
342 using type = __type_pack_element<_Ip, _Types...>;
345 inline constexpr size_t variant_npos = static_cast<size_t>(-1);
347 _LIBCPP_HIDE_FROM_ABI constexpr int __choose_index_type(unsigned int __num_elem) {
348 if (__num_elem < numeric_limits<unsigned char>::max())
350 if (__num_elem < numeric_limits<unsigned short>::max())
355 template <size_t _NumAlts>
356 using __variant_index_t =
357 #ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
360 std::tuple_element_t<
361 __choose_index_type(_NumAlts),
362 std::tuple<unsigned char, unsigned short, unsigned int>
366 template <class _IndexType>
367 constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
369 template <class... _Types>
370 class _LIBCPP_TEMPLATE_VIS variant;
372 template <class... _Types>
373 _LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&
374 __as_variant(variant<_Types...>& __vs) noexcept {
378 template <class... _Types>
379 _LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&
380 __as_variant(const variant<_Types...>& __vs) noexcept {
384 template <class... _Types>
385 _LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&&
386 __as_variant(variant<_Types...>&& __vs) noexcept {
387 return _VSTD::move(__vs);
390 template <class... _Types>
391 _LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&&
392 __as_variant(const variant<_Types...>&& __vs) noexcept {
393 return _VSTD::move(__vs);
396 namespace __find_detail {
398 template <class _Tp, class... _Types>
399 _LIBCPP_HIDE_FROM_ABI
400 constexpr size_t __find_index() {
401 constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
402 size_t __result = __not_found;
403 for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
404 if (__matches[__i]) {
405 if (__result != __not_found) {
414 template <size_t _Index>
415 struct __find_unambiguous_index_sfinae_impl
416 : integral_constant<size_t, _Index> {};
419 struct __find_unambiguous_index_sfinae_impl<__not_found> {};
422 struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
424 template <class _Tp, class... _Types>
425 struct __find_unambiguous_index_sfinae
426 : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
428 } // namespace __find_detail
430 namespace __variant_detail {
432 struct __valueless_t {};
434 enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
436 template <typename _Tp,
437 template <typename> class _IsTriviallyAvailable,
438 template <typename> class _IsAvailable>
439 constexpr _Trait __trait =
440 _IsTriviallyAvailable<_Tp>::value
441 ? _Trait::_TriviallyAvailable
442 : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
444 _LIBCPP_HIDE_FROM_ABI
445 constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
446 _Trait __result = _Trait::_TriviallyAvailable;
447 for (_Trait __t : __traits) {
448 if (static_cast<int>(__t) > static_cast<int>(__result)) {
455 template <typename... _Types>
457 static constexpr _Trait __copy_constructible_trait =
458 __variant_detail::__common_trait({__trait<_Types,
459 is_trivially_copy_constructible,
460 is_copy_constructible>...});
462 static constexpr _Trait __move_constructible_trait =
463 __variant_detail::__common_trait({__trait<_Types,
464 is_trivially_move_constructible,
465 is_move_constructible>...});
467 static constexpr _Trait __copy_assignable_trait = __variant_detail::__common_trait(
468 {__copy_constructible_trait,
469 __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
471 static constexpr _Trait __move_assignable_trait = __variant_detail::__common_trait(
472 {__move_constructible_trait,
473 __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
475 static constexpr _Trait __destructible_trait = __variant_detail::__common_trait(
476 {__trait<_Types, is_trivially_destructible, is_destructible>...});
483 _LIBCPP_HIDE_FROM_ABI
484 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
485 return _VSTD::forward<_Vp>(__v).__head;
488 template <class _Vp, size_t _Ip>
489 _LIBCPP_HIDE_FROM_ABI
490 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
491 return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
496 template <size_t _Ip, class _Vp>
497 _LIBCPP_HIDE_FROM_ABI
498 static constexpr auto&& __get_alt(_Vp&& __v) {
499 return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
500 in_place_index<_Ip>);
505 template <size_t _Ip, class _Vp>
506 _LIBCPP_HIDE_FROM_ABI
507 static constexpr auto&& __get_alt(_Vp&& __v) {
508 return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl_);
512 } // namespace __access
514 namespace __visitation {
517 template <class _Visitor, class... _Vs>
518 _LIBCPP_HIDE_FROM_ABI
519 static constexpr decltype(auto)
520 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
521 constexpr auto __fdiagonal =
522 __make_fdiagonal<_Visitor&&,
523 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
524 return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
525 _VSTD::forward<_Vs>(__vs).__as_base()...);
528 template <class _Visitor, class... _Vs>
529 _LIBCPP_HIDE_FROM_ABI
530 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
532 constexpr auto __fmatrix =
533 __make_fmatrix<_Visitor&&,
534 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
535 return __at(__fmatrix, __vs.index()...)(
536 _VSTD::forward<_Visitor>(__visitor),
537 _VSTD::forward<_Vs>(__vs).__as_base()...);
542 _LIBCPP_HIDE_FROM_ABI
543 static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
545 template <class _Tp, size_t _Np, typename... _Indices>
546 _LIBCPP_HIDE_FROM_ABI
547 static constexpr auto&& __at(const __farray<_Tp, _Np>& __elems,
548 size_t __index, _Indices... __indices) {
549 return __at(__elems[__index], __indices...);
552 template <class _Fp, class... _Fs>
553 static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_visitor_return_type_check() {
555 __all<is_same_v<_Fp, _Fs>...>::value,
556 "`std::visit` requires the visitor to have a single return type.");
559 template <class... _Fs>
560 _LIBCPP_HIDE_FROM_ABI
561 static constexpr auto __make_farray(_Fs&&... __fs) {
562 __std_visit_visitor_return_type_check<__remove_cvref_t<_Fs>...>();
563 using __result = __farray<common_type_t<__remove_cvref_t<_Fs>...>, sizeof...(_Fs)>;
564 return __result{{_VSTD::forward<_Fs>(__fs)...}};
567 template <size_t... _Is>
568 struct __dispatcher {
569 template <class _Fp, class... _Vs>
570 _LIBCPP_HIDE_FROM_ABI
571 static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
572 return _VSTD::__invoke(
573 static_cast<_Fp>(__f),
574 __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
578 template <class _Fp, class... _Vs, size_t... _Is>
579 _LIBCPP_HIDE_FROM_ABI
580 static constexpr auto __make_dispatch(index_sequence<_Is...>) {
581 return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
584 template <size_t _Ip, class _Fp, class... _Vs>
585 _LIBCPP_HIDE_FROM_ABI
586 static constexpr auto __make_fdiagonal_impl() {
587 return __make_dispatch<_Fp, _Vs...>(
588 index_sequence<((void)__type_identity<_Vs>{}, _Ip)...>{});
591 template <class _Fp, class... _Vs, size_t... _Is>
592 _LIBCPP_HIDE_FROM_ABI
593 static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
594 return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
597 template <class _Fp, class _Vp, class... _Vs>
598 _LIBCPP_HIDE_FROM_ABI
599 static constexpr auto __make_fdiagonal() {
600 constexpr size_t __np = __remove_cvref_t<_Vp>::__size();
601 static_assert(__all<(__np == __remove_cvref_t<_Vs>::__size())...>::value);
602 return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<__np>{});
605 template <class _Fp, class... _Vs, size_t... _Is>
606 _LIBCPP_HIDE_FROM_ABI
607 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
608 return __make_dispatch<_Fp, _Vs...>(__is);
611 template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
612 _LIBCPP_HIDE_FROM_ABI
613 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
614 index_sequence<_Js...>,
616 return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
617 index_sequence<_Is..., _Js>{}, __ls...)...);
620 template <class _Fp, class... _Vs>
621 _LIBCPP_HIDE_FROM_ABI
622 static constexpr auto __make_fmatrix() {
623 return __make_fmatrix_impl<_Fp, _Vs...>(
624 index_sequence<>{}, make_index_sequence<__remove_cvref_t<_Vs>::__size()>{}...);
629 template <class _Visitor, class... _Vs>
630 _LIBCPP_HIDE_FROM_ABI
631 static constexpr decltype(auto)
632 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
633 return __base::__visit_alt_at(__index,
634 _VSTD::forward<_Visitor>(__visitor),
635 _VSTD::forward<_Vs>(__vs).__impl_...);
638 template <class _Visitor, class... _Vs>
639 _LIBCPP_HIDE_FROM_ABI
640 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
642 return __base::__visit_alt(
643 _VSTD::forward<_Visitor>(__visitor),
644 _VSTD::__as_variant(_VSTD::forward<_Vs>(__vs)).__impl_...);
647 template <class _Visitor, class... _Vs>
648 _LIBCPP_HIDE_FROM_ABI
649 static constexpr decltype(auto)
650 __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
651 return __visit_alt_at(
653 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
654 _VSTD::forward<_Vs>(__vs)...);
657 template <class _Visitor, class... _Vs>
658 _LIBCPP_HIDE_FROM_ABI
659 static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
662 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
663 _VSTD::forward<_Vs>(__vs)...);
666 #if _LIBCPP_STD_VER >= 20
667 template <class _Rp, class _Visitor, class... _Vs>
668 _LIBCPP_HIDE_FROM_ABI
669 static constexpr _Rp __visit_value(_Visitor&& __visitor,
672 __make_value_visitor<_Rp>(_VSTD::forward<_Visitor>(__visitor)),
673 _VSTD::forward<_Vs>(__vs)...);
678 template <class _Visitor, class... _Values>
679 static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_exhaustive_visitor_check() {
680 static_assert(is_invocable_v<_Visitor, _Values...>,
681 "`std::visit` requires the visitor to be exhaustive.");
684 template <class _Visitor>
685 struct __value_visitor {
686 template <class... _Alts>
687 _LIBCPP_HIDE_FROM_ABI
688 constexpr decltype(auto) operator()(_Alts&&... __alts) const {
689 __std_visit_exhaustive_visitor_check<
691 decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
692 return _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
693 _VSTD::forward<_Alts>(__alts).__value...);
695 _Visitor&& __visitor;
698 #if _LIBCPP_STD_VER >= 20
699 template <class _Rp, class _Visitor>
700 struct __value_visitor_return_type {
701 template <class... _Alts>
702 _LIBCPP_HIDE_FROM_ABI
703 constexpr _Rp operator()(_Alts&&... __alts) const {
704 __std_visit_exhaustive_visitor_check<
706 decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
707 if constexpr (is_void_v<_Rp>) {
708 _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
709 _VSTD::forward<_Alts>(__alts).__value...);
712 return _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
713 _VSTD::forward<_Alts>(__alts).__value...);
717 _Visitor&& __visitor;
721 template <class _Visitor>
722 _LIBCPP_HIDE_FROM_ABI
723 static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
724 return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
727 #if _LIBCPP_STD_VER >= 20
728 template <class _Rp, class _Visitor>
729 _LIBCPP_HIDE_FROM_ABI
730 static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
731 return __value_visitor_return_type<_Rp, _Visitor>{_VSTD::forward<_Visitor>(__visitor)};
736 } // namespace __visitation
738 template <size_t _Index, class _Tp>
739 struct _LIBCPP_TEMPLATE_VIS __alt {
740 using __value_type = _Tp;
742 template <class... _Args>
743 _LIBCPP_HIDE_FROM_ABI
744 explicit constexpr __alt(in_place_t, _Args&&... __args)
745 : __value(_VSTD::forward<_Args>(__args)...) {}
747 __value_type __value;
750 template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
751 union _LIBCPP_TEMPLATE_VIS __union;
753 template <_Trait _DestructibleTrait, size_t _Index>
754 union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
756 #define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \
757 template <size_t _Index, class _Tp, class... _Types> \
758 union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \
763 _LIBCPP_HIDE_FROM_ABI \
764 explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
766 template <class... _Args> \
767 _LIBCPP_HIDE_FROM_ABI \
768 explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
769 : __head(in_place, _VSTD::forward<_Args>(__args)...) {} \
771 template <size_t _Ip, class... _Args> \
772 _LIBCPP_HIDE_FROM_ABI \
773 explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
774 : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
776 __union(const __union&) = default; \
777 __union(__union&&) = default; \
781 __union& operator=(const __union&) = default; \
782 __union& operator=(__union&&) = default; \
786 __alt<_Index, _Tp> __head; \
787 __union<destructible_trait, _Index + 1, _Types...> __tail; \
789 friend struct __access::__union; \
792 _LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
793 _LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
794 _LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
796 #undef _LIBCPP_VARIANT_UNION
798 template <_Trait _DestructibleTrait, class... _Types>
799 class _LIBCPP_TEMPLATE_VIS __base {
801 using __index_t = __variant_index_t<sizeof...(_Types)>;
803 _LIBCPP_HIDE_FROM_ABI
804 explicit constexpr __base(__valueless_t __tag) noexcept
805 : __data(__tag), __index(__variant_npos<__index_t>) {}
807 template <size_t _Ip, class... _Args>
808 _LIBCPP_HIDE_FROM_ABI
809 explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
811 __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
814 _LIBCPP_HIDE_FROM_ABI
815 constexpr bool valueless_by_exception() const noexcept {
816 return index() == variant_npos;
819 _LIBCPP_HIDE_FROM_ABI
820 constexpr size_t index() const noexcept {
821 return __index == __variant_npos<__index_t> ? variant_npos : __index;
825 _LIBCPP_HIDE_FROM_ABI
826 constexpr auto&& __as_base() & { return *this; }
828 _LIBCPP_HIDE_FROM_ABI
829 constexpr auto&& __as_base() && { return _VSTD::move(*this); }
831 _LIBCPP_HIDE_FROM_ABI
832 constexpr auto&& __as_base() const & { return *this; }
834 _LIBCPP_HIDE_FROM_ABI
835 constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
837 _LIBCPP_HIDE_FROM_ABI
838 static constexpr size_t __size() { return sizeof...(_Types); }
840 __union<_DestructibleTrait, 0, _Types...> __data;
843 friend struct __access::__base;
844 friend struct __visitation::__base;
847 template <class _Traits, _Trait = _Traits::__destructible_trait>
848 class _LIBCPP_TEMPLATE_VIS __dtor;
850 #define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \
851 template <class... _Types> \
852 class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, \
853 destructible_trait> \
854 : public __base<destructible_trait, _Types...> { \
855 using __base_type = __base<destructible_trait, _Types...>; \
856 using __index_t = typename __base_type::__index_t; \
859 using __base_type::__base_type; \
860 using __base_type::operator=; \
862 __dtor(const __dtor&) = default; \
863 __dtor(__dtor&&) = default; \
865 __dtor& operator=(const __dtor&) = default; \
866 __dtor& operator=(__dtor&&) = default; \
869 inline _LIBCPP_HIDE_FROM_ABI \
873 _LIBCPP_VARIANT_DESTRUCTOR(
874 _Trait::_TriviallyAvailable,
875 ~__dtor() = default;,
876 void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
878 _LIBCPP_VARIANT_DESTRUCTOR(
880 ~__dtor() { __destroy(); },
881 void __destroy() noexcept {
882 if (!this->valueless_by_exception()) {
883 __visitation::__base::__visit_alt(
884 [](auto& __alt) noexcept {
885 using __alt_type = __remove_cvref_t<decltype(__alt)>;
890 this->__index = __variant_npos<__index_t>;
893 _LIBCPP_VARIANT_DESTRUCTOR(
894 _Trait::_Unavailable,
896 void __destroy() noexcept = delete;);
898 #undef _LIBCPP_VARIANT_DESTRUCTOR
900 template <class _Traits>
901 class _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
902 using __base_type = __dtor<_Traits>;
905 using __base_type::__base_type;
906 using __base_type::operator=;
909 template <size_t _Ip, class _Tp, class... _Args>
910 _LIBCPP_HIDE_FROM_ABI
911 static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
912 ::new ((void*)_VSTD::addressof(__a))
913 __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
917 template <class _Rhs>
918 _LIBCPP_HIDE_FROM_ABI
919 static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
921 if (!__rhs.valueless_by_exception()) {
922 __visitation::__base::__visit_alt_at(
924 [](auto& __lhs_alt, auto&& __rhs_alt) {
927 _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
929 __lhs, _VSTD::forward<_Rhs>(__rhs));
930 __lhs.__index = __rhs.index();
935 template <class _Traits, _Trait = _Traits::__move_constructible_trait>
936 class _LIBCPP_TEMPLATE_VIS __move_constructor;
938 #define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \
940 template <class... _Types> \
941 class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \
942 move_constructible_trait> \
943 : public __ctor<__traits<_Types...>> { \
944 using __base_type = __ctor<__traits<_Types...>>; \
947 using __base_type::__base_type; \
948 using __base_type::operator=; \
950 __move_constructor(const __move_constructor&) = default; \
952 ~__move_constructor() = default; \
953 __move_constructor& operator=(const __move_constructor&) = default; \
954 __move_constructor& operator=(__move_constructor&&) = default; \
957 _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
958 _Trait::_TriviallyAvailable,
959 __move_constructor(__move_constructor&& __that) = default;);
961 _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
963 __move_constructor(__move_constructor&& __that) noexcept(
964 __all<is_nothrow_move_constructible_v<_Types>...>::value)
965 : __move_constructor(__valueless_t{}) {
966 this->__generic_construct(*this, _VSTD::move(__that));
969 _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
970 _Trait::_Unavailable,
971 __move_constructor(__move_constructor&&) = delete;);
973 #undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
975 template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
976 class _LIBCPP_TEMPLATE_VIS __copy_constructor;
978 #define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \
980 template <class... _Types> \
981 class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \
982 copy_constructible_trait> \
983 : public __move_constructor<__traits<_Types...>> { \
984 using __base_type = __move_constructor<__traits<_Types...>>; \
987 using __base_type::__base_type; \
988 using __base_type::operator=; \
991 __copy_constructor(__copy_constructor&&) = default; \
992 ~__copy_constructor() = default; \
993 __copy_constructor& operator=(const __copy_constructor&) = default; \
994 __copy_constructor& operator=(__copy_constructor&&) = default; \
997 _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
998 _Trait::_TriviallyAvailable,
999 __copy_constructor(const __copy_constructor& __that) = default;);
1001 _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
1003 __copy_constructor(const __copy_constructor& __that)
1004 : __copy_constructor(__valueless_t{}) {
1005 this->__generic_construct(*this, __that);
1008 _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
1009 _Trait::_Unavailable,
1010 __copy_constructor(const __copy_constructor&) = delete;);
1012 #undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
1014 template <class _Traits>
1015 class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
1016 using __base_type = __copy_constructor<_Traits>;
1019 using __base_type::__base_type;
1020 using __base_type::operator=;
1022 template <size_t _Ip, class... _Args>
1023 _LIBCPP_HIDE_FROM_ABI
1024 auto& __emplace(_Args&&... __args) {
1026 auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
1027 _VSTD::forward<_Args>(__args)...);
1028 this->__index = _Ip;
1033 template <size_t _Ip, class _Tp, class _Arg>
1034 _LIBCPP_HIDE_FROM_ABI
1035 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
1036 if (this->index() == _Ip) {
1037 __a.__value = _VSTD::forward<_Arg>(__arg);
1040 _LIBCPP_HIDE_FROM_ABI void operator()(true_type) const {
1041 __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
1043 _LIBCPP_HIDE_FROM_ABI void operator()(false_type) const {
1044 __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
1046 __assignment* __this;
1048 } __impl{this, _VSTD::forward<_Arg>(__arg)};
1049 __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
1050 !is_nothrow_move_constructible_v<_Tp>>{});
1054 template <class _That>
1055 _LIBCPP_HIDE_FROM_ABI
1056 void __generic_assign(_That&& __that) {
1057 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1059 } else if (__that.valueless_by_exception()) {
1062 __visitation::__base::__visit_alt_at(
1064 [this](auto& __this_alt, auto&& __that_alt) {
1067 _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
1069 *this, _VSTD::forward<_That>(__that));
1074 template <class _Traits, _Trait = _Traits::__move_assignable_trait>
1075 class _LIBCPP_TEMPLATE_VIS __move_assignment;
1077 #define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \
1079 template <class... _Types> \
1080 class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \
1081 move_assignable_trait> \
1082 : public __assignment<__traits<_Types...>> { \
1083 using __base_type = __assignment<__traits<_Types...>>; \
1086 using __base_type::__base_type; \
1087 using __base_type::operator=; \
1089 __move_assignment(const __move_assignment&) = default; \
1090 __move_assignment(__move_assignment&&) = default; \
1091 ~__move_assignment() = default; \
1092 __move_assignment& operator=(const __move_assignment&) = default; \
1096 _LIBCPP_VARIANT_MOVE_ASSIGNMENT(
1097 _Trait::_TriviallyAvailable,
1098 __move_assignment& operator=(__move_assignment&& __that) = default;);
1100 _LIBCPP_VARIANT_MOVE_ASSIGNMENT(
1102 __move_assignment& operator=(__move_assignment&& __that) noexcept(
1103 __all<(is_nothrow_move_constructible_v<_Types> &&
1104 is_nothrow_move_assignable_v<_Types>)...>::value) {
1105 this->__generic_assign(_VSTD::move(__that));
1109 _LIBCPP_VARIANT_MOVE_ASSIGNMENT(
1110 _Trait::_Unavailable,
1111 __move_assignment& operator=(__move_assignment&&) = delete;);
1113 #undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
1115 template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
1116 class _LIBCPP_TEMPLATE_VIS __copy_assignment;
1118 #define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \
1120 template <class... _Types> \
1121 class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \
1122 copy_assignable_trait> \
1123 : public __move_assignment<__traits<_Types...>> { \
1124 using __base_type = __move_assignment<__traits<_Types...>>; \
1127 using __base_type::__base_type; \
1128 using __base_type::operator=; \
1130 __copy_assignment(const __copy_assignment&) = default; \
1131 __copy_assignment(__copy_assignment&&) = default; \
1132 ~__copy_assignment() = default; \
1134 __copy_assignment& operator=(__copy_assignment&&) = default; \
1137 _LIBCPP_VARIANT_COPY_ASSIGNMENT(
1138 _Trait::_TriviallyAvailable,
1139 __copy_assignment& operator=(const __copy_assignment& __that) = default;);
1141 _LIBCPP_VARIANT_COPY_ASSIGNMENT(
1143 __copy_assignment& operator=(const __copy_assignment& __that) {
1144 this->__generic_assign(__that);
1148 _LIBCPP_VARIANT_COPY_ASSIGNMENT(
1149 _Trait::_Unavailable,
1150 __copy_assignment& operator=(const __copy_assignment&) = delete;);
1152 #undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1154 template <class... _Types>
1155 class _LIBCPP_TEMPLATE_VIS __impl
1156 : public __copy_assignment<__traits<_Types...>> {
1157 using __base_type = __copy_assignment<__traits<_Types...>>;
1160 using __base_type::__base_type; // get in_place_index_t constructor & friends
1161 _LIBCPP_HIDE_FROM_ABI __impl(__impl const&) = default;
1162 _LIBCPP_HIDE_FROM_ABI __impl(__impl&&) = default;
1163 _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl const&) = default;
1164 _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl&&) = default;
1166 template <size_t _Ip, class _Arg>
1167 _LIBCPP_HIDE_FROM_ABI
1168 void __assign(_Arg&& __arg) {
1169 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
1170 _VSTD::forward<_Arg>(__arg));
1173 inline _LIBCPP_HIDE_FROM_ABI
1174 void __swap(__impl& __that) {
1175 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1177 } else if (this->index() == __that.index()) {
1178 __visitation::__base::__visit_alt_at(
1180 [](auto& __this_alt, auto& __that_alt) {
1182 swap(__this_alt.__value, __that_alt.__value);
1187 __impl* __lhs = this;
1188 __impl* __rhs = _VSTD::addressof(__that);
1189 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1190 _VSTD::swap(__lhs, __rhs);
1192 __impl __tmp(_VSTD::move(*__rhs));
1193 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1194 if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
1195 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1197 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1198 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1199 // into `__rhs` and provide the strong exception safety guarantee.
1201 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1203 if (__tmp.__move_nothrow()) {
1204 this->__generic_construct(*__rhs, _VSTD::move(__tmp));
1210 // this isn't consolidated with the `if constexpr` branch above due to
1211 // `throw` being ill-formed with exceptions disabled even when discarded.
1212 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1214 this->__generic_construct(*__lhs, _VSTD::move(__tmp));
1219 inline _LIBCPP_HIDE_FROM_ABI
1220 bool __move_nothrow() const {
1221 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1222 return this->valueless_by_exception() || __results[this->index()];
1226 struct __no_narrowing_check {
1227 template <class _Dest, class _Source>
1228 using _Apply = __type_identity<_Dest>;
1231 struct __narrowing_check {
1232 template <class _Dest>
1233 static auto __test_impl(_Dest (&&)[1]) -> __type_identity<_Dest>;
1234 template <class _Dest, class _Source>
1235 using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({std::declval<_Source>()}));
1238 template <class _Dest, class _Source>
1239 using __check_for_narrowing _LIBCPP_NODEBUG =
1241 #ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
1244 is_arithmetic<_Dest>::value,
1246 __no_narrowing_check
1247 >::template _Apply<_Dest, _Source>;
1249 template <class _Tp, size_t _Idx>
1251 template <class _Up>
1252 auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
1255 template <class _Tp, size_t>
1256 struct __overload_bool {
1257 template <class _Up, class _Ap = __remove_cvref_t<_Up>>
1258 auto operator()(bool, _Up&&) const
1259 -> enable_if_t<is_same_v<_Ap, bool>, __type_identity<_Tp>>;
1262 template <size_t _Idx>
1263 struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
1264 template <size_t _Idx>
1265 struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
1266 template <size_t _Idx>
1267 struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
1268 template <size_t _Idx>
1269 struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
1271 template <class ..._Bases>
1272 struct __all_overloads : _Bases... {
1273 void operator()() const;
1274 using _Bases::operator()...;
1277 template <class _IdxSeq>
1278 struct __make_overloads_imp;
1280 template <size_t ..._Idx>
1281 struct __make_overloads_imp<__tuple_indices<_Idx...> > {
1282 template <class ..._Types>
1283 using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>;
1286 template <class ..._Types>
1287 using _MakeOverloads _LIBCPP_NODEBUG = typename __make_overloads_imp<
1288 __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
1290 template <class _Tp, class... _Types>
1291 using __best_match_t =
1292 typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
1294 } // namespace __variant_detail
1296 template <class... _Types>
1297 class _LIBCPP_TEMPLATE_VIS _LIBCPP_DECLSPEC_EMPTY_BASES variant
1298 : private __sfinae_ctor_base<
1299 __all<is_copy_constructible_v<_Types>...>::value,
1300 __all<is_move_constructible_v<_Types>...>::value>,
1301 private __sfinae_assign_base<
1302 __all<(is_copy_constructible_v<_Types> &&
1303 is_copy_assignable_v<_Types>)...>::value,
1304 __all<(is_move_constructible_v<_Types> &&
1305 is_move_assignable_v<_Types>)...>::value> {
1306 static_assert(0 < sizeof...(_Types),
1307 "variant must consist of at least one alternative.");
1309 static_assert(__all<!is_array_v<_Types>...>::value,
1310 "variant can not have an array type as an alternative.");
1312 static_assert(__all<!is_reference_v<_Types>...>::value,
1313 "variant can not have a reference type as an alternative.");
1315 static_assert(__all<!is_void_v<_Types>...>::value,
1316 "variant can not have a void type as an alternative.");
1318 using __first_type = variant_alternative_t<0, variant>;
1321 template <bool _Dummy = true,
1322 enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1325 _LIBCPP_HIDE_FROM_ABI
1326 constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1327 : __impl_(in_place_index<0>) {}
1329 _LIBCPP_HIDE_FROM_ABI constexpr variant(const variant&) = default;
1330 _LIBCPP_HIDE_FROM_ABI constexpr variant(variant&&) = default;
1334 enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1335 enable_if_t<!__is_inplace_type<__remove_cvref_t<_Arg>>::value, int> = 0,
1336 enable_if_t<!__is_inplace_index<__remove_cvref_t<_Arg>>::value, int> = 0,
1337 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1339 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1340 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1341 _LIBCPP_HIDE_FROM_ABI
1342 constexpr variant(_Arg&& __arg) noexcept(
1343 is_nothrow_constructible_v<_Tp, _Arg>)
1344 : __impl_(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
1346 template <size_t _Ip, class... _Args,
1347 class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
1348 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1349 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1350 _LIBCPP_HIDE_FROM_ABI
1351 explicit constexpr variant(
1352 in_place_index_t<_Ip>,
1353 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1354 : __impl_(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1360 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1361 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1362 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1364 _LIBCPP_HIDE_FROM_ABI
1365 explicit constexpr variant(
1366 in_place_index_t<_Ip>,
1367 initializer_list<_Up> __il,
1368 _Args&&... __args) noexcept(
1369 is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1370 : __impl_(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1376 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1377 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1378 _LIBCPP_HIDE_FROM_ABI
1379 explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1380 is_nothrow_constructible_v<_Tp, _Args...>)
1381 : __impl_(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1388 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1389 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1391 _LIBCPP_HIDE_FROM_ABI
1392 explicit constexpr variant(
1393 in_place_type_t<_Tp>,
1394 initializer_list<_Up> __il,
1395 _Args&&... __args) noexcept(
1396 is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1397 : __impl_(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1399 _LIBCPP_HIDE_FROM_ABI ~variant() = default;
1401 _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(const variant&) = default;
1402 _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(variant&&) = default;
1406 enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1407 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1409 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1410 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
1412 _LIBCPP_HIDE_FROM_ABI
1413 variant& operator=(_Arg&& __arg) noexcept(
1414 is_nothrow_assignable_v<_Tp&, _Arg> &&
1415 is_nothrow_constructible_v<_Tp, _Arg>) {
1416 __impl_.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
1423 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1424 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1425 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1426 _LIBCPP_HIDE_FROM_ABI
1427 _Tp& emplace(_Args&&... __args) {
1428 return __impl_.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
1435 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1436 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1437 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1439 _LIBCPP_HIDE_FROM_ABI
1440 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1441 return __impl_.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
1448 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1449 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1450 _LIBCPP_HIDE_FROM_ABI
1451 _Tp& emplace(_Args&&... __args) {
1452 return __impl_.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
1460 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1461 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1463 _LIBCPP_HIDE_FROM_ABI
1464 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1465 return __impl_.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
1468 _LIBCPP_HIDE_FROM_ABI
1469 constexpr bool valueless_by_exception() const noexcept {
1470 return __impl_.valueless_by_exception();
1473 _LIBCPP_HIDE_FROM_ABI
1474 constexpr size_t index() const noexcept { return __impl_.index(); }
1480 __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1481 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1483 _LIBCPP_HIDE_FROM_ABI
1484 void swap(variant& __that) noexcept(
1485 __all<(is_nothrow_move_constructible_v<_Types> &&
1486 is_nothrow_swappable_v<_Types>)...>::value) {
1487 __impl_.__swap(__that.__impl_);
1491 __variant_detail::__impl<_Types...> __impl_;
1493 friend struct __variant_detail::__access::__variant;
1494 friend struct __variant_detail::__visitation::__variant;
1497 template <size_t _Ip, class... _Types>
1498 _LIBCPP_HIDE_FROM_ABI
1499 constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1500 return __v.index() == _Ip;
1503 template <class _Tp, class... _Types>
1504 _LIBCPP_HIDE_FROM_ABI
1505 constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1506 return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1509 template <size_t _Ip, class _Vp>
1510 _LIBCPP_HIDE_FROM_ABI
1511 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1512 constexpr auto&& __generic_get(_Vp&& __v) {
1513 using __variant_detail::__access::__variant;
1514 if (!std::__holds_alternative<_Ip>(__v)) {
1515 __throw_bad_variant_access();
1517 return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
1520 template <size_t _Ip, class... _Types>
1521 _LIBCPP_HIDE_FROM_ABI
1522 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1523 constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
1524 variant<_Types...>& __v) {
1525 static_assert(_Ip < sizeof...(_Types));
1526 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1527 return std::__generic_get<_Ip>(__v);
1530 template <size_t _Ip, class... _Types>
1531 _LIBCPP_HIDE_FROM_ABI
1532 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1533 constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
1534 variant<_Types...>&& __v) {
1535 static_assert(_Ip < sizeof...(_Types));
1536 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1537 return std::__generic_get<_Ip>(_VSTD::move(__v));
1540 template <size_t _Ip, class... _Types>
1541 _LIBCPP_HIDE_FROM_ABI
1542 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1543 constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
1544 const variant<_Types...>& __v) {
1545 static_assert(_Ip < sizeof...(_Types));
1546 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1547 return std::__generic_get<_Ip>(__v);
1550 template <size_t _Ip, class... _Types>
1551 _LIBCPP_HIDE_FROM_ABI
1552 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1553 constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
1554 const variant<_Types...>&& __v) {
1555 static_assert(_Ip < sizeof...(_Types));
1556 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1557 return std::__generic_get<_Ip>(_VSTD::move(__v));
1560 template <class _Tp, class... _Types>
1561 _LIBCPP_HIDE_FROM_ABI
1562 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1563 constexpr _Tp& get(variant<_Types...>& __v) {
1564 static_assert(!is_void_v<_Tp>);
1565 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1568 template <class _Tp, class... _Types>
1569 _LIBCPP_HIDE_FROM_ABI
1570 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1571 constexpr _Tp&& get(variant<_Types...>&& __v) {
1572 static_assert(!is_void_v<_Tp>);
1573 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1577 template <class _Tp, class... _Types>
1578 _LIBCPP_HIDE_FROM_ABI
1579 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1580 constexpr const _Tp& get(const variant<_Types...>& __v) {
1581 static_assert(!is_void_v<_Tp>);
1582 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1585 template <class _Tp, class... _Types>
1586 _LIBCPP_HIDE_FROM_ABI
1587 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1588 constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1589 static_assert(!is_void_v<_Tp>);
1590 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1594 template <size_t _Ip, class _Vp>
1595 _LIBCPP_HIDE_FROM_ABI
1596 constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1597 using __variant_detail::__access::__variant;
1598 return __v && std::__holds_alternative<_Ip>(*__v)
1599 ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
1603 template <size_t _Ip, class... _Types>
1604 _LIBCPP_HIDE_FROM_ABI
1605 constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1606 get_if(variant<_Types...>* __v) noexcept {
1607 static_assert(_Ip < sizeof...(_Types));
1608 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1609 return std::__generic_get_if<_Ip>(__v);
1612 template <size_t _Ip, class... _Types>
1613 _LIBCPP_HIDE_FROM_ABI
1614 constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1615 get_if(const variant<_Types...>* __v) noexcept {
1616 static_assert(_Ip < sizeof...(_Types));
1617 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1618 return std::__generic_get_if<_Ip>(__v);
1621 template <class _Tp, class... _Types>
1622 _LIBCPP_HIDE_FROM_ABI
1623 constexpr add_pointer_t<_Tp>
1624 get_if(variant<_Types...>* __v) noexcept {
1625 static_assert(!is_void_v<_Tp>);
1626 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1629 template <class _Tp, class... _Types>
1630 _LIBCPP_HIDE_FROM_ABI
1631 constexpr add_pointer_t<const _Tp>
1632 get_if(const variant<_Types...>* __v) noexcept {
1633 static_assert(!is_void_v<_Tp>);
1634 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1637 template <class _Operator>
1638 struct __convert_to_bool {
1639 template <class _T1, class _T2>
1640 _LIBCPP_HIDE_FROM_ABI
1641 constexpr bool operator()(_T1 && __t1, _T2&& __t2) const {
1642 static_assert(is_convertible<decltype(_Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2))), bool>::value,
1643 "the relational operator does not return a type which is implicitly convertible to bool");
1644 return _Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
1648 template <class... _Types>
1649 _LIBCPP_HIDE_FROM_ABI
1650 constexpr bool operator==(const variant<_Types...>& __lhs,
1651 const variant<_Types...>& __rhs) {
1652 using __variant_detail::__visitation::__variant;
1653 if (__lhs.index() != __rhs.index()) return false;
1654 if (__lhs.valueless_by_exception()) return true;
1655 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
1658 # if _LIBCPP_STD_VER >= 20
1660 template <class... _Types> requires (three_way_comparable<_Types> && ...)
1661 _LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...>
1662 operator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1663 using __variant_detail::__visitation::__variant;
1664 using __result_t = common_comparison_category_t<compare_three_way_result_t<_Types>...>;
1665 if (__lhs.valueless_by_exception() && __rhs.valueless_by_exception())
1666 return strong_ordering::equal;
1667 if (__lhs.valueless_by_exception())
1668 return strong_ordering::less;
1669 if (__rhs.valueless_by_exception())
1670 return strong_ordering::greater;
1671 if (auto __c = __lhs.index() <=> __rhs.index(); __c != 0)
1673 auto __three_way = []<class _Type>(const _Type& __v, const _Type& __w) -> __result_t { return __v <=> __w; };
1674 return __variant::__visit_value_at(__lhs.index(), __three_way, __lhs, __rhs);
1677 # endif // _LIBCPP_STD_VER >= 20
1679 template <class... _Types>
1680 _LIBCPP_HIDE_FROM_ABI
1681 constexpr bool operator!=(const variant<_Types...>& __lhs,
1682 const variant<_Types...>& __rhs) {
1683 using __variant_detail::__visitation::__variant;
1684 if (__lhs.index() != __rhs.index()) return true;
1685 if (__lhs.valueless_by_exception()) return false;
1686 return __variant::__visit_value_at(
1687 __lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
1690 template <class... _Types>
1691 _LIBCPP_HIDE_FROM_ABI
1692 constexpr bool operator<(const variant<_Types...>& __lhs,
1693 const variant<_Types...>& __rhs) {
1694 using __variant_detail::__visitation::__variant;
1695 if (__rhs.valueless_by_exception()) return false;
1696 if (__lhs.valueless_by_exception()) return true;
1697 if (__lhs.index() < __rhs.index()) return true;
1698 if (__lhs.index() > __rhs.index()) return false;
1699 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
1702 template <class... _Types>
1703 _LIBCPP_HIDE_FROM_ABI
1704 constexpr bool operator>(const variant<_Types...>& __lhs,
1705 const variant<_Types...>& __rhs) {
1706 using __variant_detail::__visitation::__variant;
1707 if (__lhs.valueless_by_exception()) return false;
1708 if (__rhs.valueless_by_exception()) return true;
1709 if (__lhs.index() > __rhs.index()) return true;
1710 if (__lhs.index() < __rhs.index()) return false;
1711 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
1714 template <class... _Types>
1715 _LIBCPP_HIDE_FROM_ABI
1716 constexpr bool operator<=(const variant<_Types...>& __lhs,
1717 const variant<_Types...>& __rhs) {
1718 using __variant_detail::__visitation::__variant;
1719 if (__lhs.valueless_by_exception()) return true;
1720 if (__rhs.valueless_by_exception()) return false;
1721 if (__lhs.index() < __rhs.index()) return true;
1722 if (__lhs.index() > __rhs.index()) return false;
1723 return __variant::__visit_value_at(
1724 __lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
1727 template <class... _Types>
1728 _LIBCPP_HIDE_FROM_ABI
1729 constexpr bool operator>=(const variant<_Types...>& __lhs,
1730 const variant<_Types...>& __rhs) {
1731 using __variant_detail::__visitation::__variant;
1732 if (__rhs.valueless_by_exception()) return true;
1733 if (__lhs.valueless_by_exception()) return false;
1734 if (__lhs.index() > __rhs.index()) return true;
1735 if (__lhs.index() < __rhs.index()) return false;
1736 return __variant::__visit_value_at(
1737 __lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
1740 template <class... _Vs>
1741 _LIBCPP_HIDE_FROM_ABI
1742 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1743 constexpr void __throw_if_valueless(_Vs&&... __vs) {
1744 const bool __valueless =
1745 (... || _VSTD::__as_variant(__vs).valueless_by_exception());
1747 __throw_bad_variant_access();
1752 class _Visitor, class... _Vs,
1753 typename = void_t<decltype(_VSTD::__as_variant(std::declval<_Vs>()))...> >
1754 _LIBCPP_HIDE_FROM_ABI
1755 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1756 constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
1757 using __variant_detail::__visitation::__variant;
1758 _VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
1759 return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
1760 _VSTD::forward<_Vs>(__vs)...);
1763 #if _LIBCPP_STD_VER >= 20
1765 class _Rp, class _Visitor, class... _Vs,
1766 typename = void_t<decltype(_VSTD::__as_variant(std::declval<_Vs>()))...> >
1767 _LIBCPP_HIDE_FROM_ABI
1768 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1769 constexpr _Rp visit(_Visitor&& __visitor, _Vs&&... __vs) {
1770 using __variant_detail::__visitation::__variant;
1771 _VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
1772 return __variant::__visit_value<_Rp>(_VSTD::forward<_Visitor>(__visitor),
1773 _VSTD::forward<_Vs>(__vs)...);
1777 template <class... _Types>
1778 _LIBCPP_HIDE_FROM_ABI
1779 auto swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1780 noexcept(noexcept(__lhs.swap(__rhs)))
1781 -> decltype( __lhs.swap(__rhs))
1782 { return __lhs.swap(__rhs); }
1784 template <class... _Types>
1785 struct _LIBCPP_TEMPLATE_VIS hash<
1786 __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
1787 using argument_type = variant<_Types...>;
1788 using result_type = size_t;
1790 _LIBCPP_HIDE_FROM_ABI
1791 result_type operator()(const argument_type& __v) const {
1792 using __variant_detail::__visitation::__variant;
1794 __v.valueless_by_exception()
1795 ? 299792458 // Random value chosen by the universe upon creation
1796 : __variant::__visit_alt(
1797 [](const auto& __alt) {
1798 using __alt_type = __remove_cvref_t<decltype(__alt)>;
1799 using __value_type = remove_const_t<
1800 typename __alt_type::__value_type>;
1801 return hash<__value_type>{}(__alt.__value);
1804 return std::__hash_combine(__res, hash<size_t>{}(__v.index()));
1808 // __unchecked_get is the same as std::get, except, it is UB to use it with the wrong
1809 // type whereas std::get will throw or returning nullptr. This makes it faster than
1811 template <size_t _Ip, class _Vp>
1812 _LIBCPP_HIDE_FROM_ABI
1813 constexpr auto&& __unchecked_get(_Vp&& __v) noexcept {
1814 using __variant_detail::__access::__variant;
1815 return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
1818 template <class _Tp, class... _Types>
1819 _LIBCPP_HIDE_FROM_ABI
1820 constexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept {
1821 return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1824 template <class _Tp, class... _Types>
1825 _LIBCPP_HIDE_FROM_ABI
1826 constexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept {
1827 return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1830 #endif // _LIBCPP_STD_VER >= 17
1832 _LIBCPP_END_NAMESPACE_STD
1836 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1837 # include <exception>
1838 # include <type_traits>
1839 # include <typeinfo>
1843 #endif // _LIBCPP_VARIANT