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
45 constexpr ~variant(); // constexpr since c++20
47 // 20.7.2.3, assignment
48 constexpr variant& operator=(const variant&);
49 constexpr variant& operator=(variant&&) noexcept(see below);
52 constexpr variant& operator=(T&&) noexcept(see below); // constexpr since c++20
54 // 20.7.2.4, modifiers
55 template <class T, class... Args>
56 constexpr T& emplace(Args&&...); // constexpr since c++20
58 template <class T, class U, class... Args>
59 constexpr T& emplace(initializer_list<U>, Args&&...); // constexpr since c++20
61 template <size_t I, class... Args>
62 constexpr variant_alternative_t<I, variant>& emplace(Args&&...); // constexpr since c++20
64 template <size_t I, class U, class... Args>
65 constexpr variant_alternative_t<I, variant>&
66 emplace(initializer_list<U>, Args&&...); // constexpr since c++20
68 // 20.7.2.5, value status
69 constexpr bool valueless_by_exception() const noexcept;
70 constexpr size_t index() const noexcept;
73 void swap(variant&) noexcept(see below);
75 // [variant.visit], visitation
76 template<class Self, class Visitor>
77 constexpr decltype(auto) visit(this Self&&, Visitor&&); // Since C++26
78 template<class R, class Self, class Visitor>
79 constexpr R visit(this Self&&, Visitor&&); // Since C++26
82 // 20.7.3, variant helper classes
83 template <class T> struct variant_size; // undefined
86 inline constexpr size_t variant_size_v = variant_size<T>::value;
88 template <class T> struct variant_size<const T>;
89 template <class T> struct variant_size<volatile T>;
90 template <class T> struct variant_size<const volatile T>;
92 template <class... Types>
93 struct variant_size<variant<Types...>>;
95 template <size_t I, class T> struct variant_alternative; // undefined
97 template <size_t I, class T>
98 using variant_alternative_t = typename variant_alternative<I, T>::type;
100 template <size_t I, class T> struct variant_alternative<I, const T>;
101 template <size_t I, class T> struct variant_alternative<I, volatile T>;
102 template <size_t I, class T> struct variant_alternative<I, const volatile T>;
104 template <size_t I, class... Types>
105 struct variant_alternative<I, variant<Types...>>;
107 inline constexpr size_t variant_npos = -1;
109 // 20.7.4, value access
110 template <class T, class... Types>
111 constexpr bool holds_alternative(const variant<Types...>&) noexcept;
113 template <size_t I, class... Types>
114 constexpr variant_alternative_t<I, variant<Types...>>&
115 get(variant<Types...>&);
117 template <size_t I, class... Types>
118 constexpr variant_alternative_t<I, variant<Types...>>&&
119 get(variant<Types...>&&);
121 template <size_t I, class... Types>
122 constexpr variant_alternative_t<I, variant<Types...>> const&
123 get(const variant<Types...>&);
125 template <size_t I, class... Types>
126 constexpr variant_alternative_t<I, variant<Types...>> const&&
127 get(const variant<Types...>&&);
129 template <class T, class... Types>
130 constexpr T& get(variant<Types...>&);
132 template <class T, class... Types>
133 constexpr T&& get(variant<Types...>&&);
135 template <class T, class... Types>
136 constexpr const T& get(const variant<Types...>&);
138 template <class T, class... Types>
139 constexpr const T&& get(const variant<Types...>&&);
141 template <size_t I, class... Types>
142 constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
143 get_if(variant<Types...>*) noexcept;
145 template <size_t I, class... Types>
146 constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
147 get_if(const variant<Types...>*) noexcept;
149 template <class T, class... Types>
150 constexpr add_pointer_t<T>
151 get_if(variant<Types...>*) noexcept;
153 template <class T, class... Types>
154 constexpr add_pointer_t<const T>
155 get_if(const variant<Types...>*) noexcept;
157 // 20.7.5, relational operators
158 template <class... Types>
159 constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
161 template <class... Types>
162 constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
164 template <class... Types>
165 constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
167 template <class... Types>
168 constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
170 template <class... Types>
171 constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
173 template <class... Types>
174 constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
176 template <class... Types> requires (three_way_comparable<Types> && ...)
177 constexpr common_comparison_category_t<compare_three_way_result_t<Types>...>
178 operator<=>(const variant<Types...>&, const variant<Types...>&); // since C++20
180 // 20.7.6, visitation
181 template <class Visitor, class... Variants>
182 constexpr see below visit(Visitor&&, Variants&&...);
184 template <class R, class Visitor, class... Variants>
185 constexpr R visit(Visitor&&, Variants&&...); // since C++20
187 // 20.7.7, class monostate
190 // 20.7.8, monostate relational operators
191 constexpr bool operator==(monostate, monostate) noexcept;
192 constexpr bool operator!=(monostate, monostate) noexcept; // until C++20
193 constexpr bool operator<(monostate, monostate) noexcept; // until C++20
194 constexpr bool operator>(monostate, monostate) noexcept; // until C++20
195 constexpr bool operator<=(monostate, monostate) noexcept; // until C++20
196 constexpr bool operator>=(monostate, monostate) noexcept; // until C++20
197 constexpr strong_ordering operator<=>(monostate, monostate) noexcept; // since C++20
199 // 20.7.9, specialized algorithms
200 template <class... Types>
201 void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
203 // 20.7.10, class bad_variant_access
204 class bad_variant_access;
206 // 20.7.11, hash support
207 template <class T> struct hash;
208 template <class... Types> struct hash<variant<Types...>>;
209 template <> struct hash<monostate>;
215 #include <__cxx03/__compare/common_comparison_category.h>
216 #include <__cxx03/__compare/compare_three_way_result.h>
217 #include <__cxx03/__compare/three_way_comparable.h>
218 #include <__cxx03/__config>
219 #include <__cxx03/__exception/exception.h>
220 #include <__cxx03/__functional/hash.h>
221 #include <__cxx03/__functional/invoke.h>
222 #include <__cxx03/__functional/operations.h>
223 #include <__cxx03/__functional/unary_function.h>
224 #include <__cxx03/__memory/addressof.h>
225 #include <__cxx03/__memory/construct_at.h>
226 #include <__cxx03/__tuple/find_index.h>
227 #include <__cxx03/__tuple/sfinae_helpers.h>
228 #include <__cxx03/__type_traits/add_const.h>
229 #include <__cxx03/__type_traits/add_cv.h>
230 #include <__cxx03/__type_traits/add_pointer.h>
231 #include <__cxx03/__type_traits/add_volatile.h>
232 #include <__cxx03/__type_traits/common_type.h>
233 #include <__cxx03/__type_traits/conjunction.h>
234 #include <__cxx03/__type_traits/dependent_type.h>
235 #include <__cxx03/__type_traits/is_array.h>
236 #include <__cxx03/__type_traits/is_constructible.h>
237 #include <__cxx03/__type_traits/is_destructible.h>
238 #include <__cxx03/__type_traits/is_nothrow_assignable.h>
239 #include <__cxx03/__type_traits/is_nothrow_constructible.h>
240 #include <__cxx03/__type_traits/is_reference.h>
241 #include <__cxx03/__type_traits/is_trivially_assignable.h>
242 #include <__cxx03/__type_traits/is_trivially_constructible.h>
243 #include <__cxx03/__type_traits/is_trivially_destructible.h>
244 #include <__cxx03/__type_traits/is_trivially_relocatable.h>
245 #include <__cxx03/__type_traits/is_void.h>
246 #include <__cxx03/__type_traits/remove_const.h>
247 #include <__cxx03/__type_traits/remove_cvref.h>
248 #include <__cxx03/__type_traits/type_identity.h>
249 #include <__cxx03/__type_traits/void_t.h>
250 #include <__cxx03/__utility/declval.h>
251 #include <__cxx03/__utility/forward.h>
252 #include <__cxx03/__utility/forward_like.h>
253 #include <__cxx03/__utility/in_place.h>
254 #include <__cxx03/__utility/integer_sequence.h>
255 #include <__cxx03/__utility/move.h>
256 #include <__cxx03/__utility/swap.h>
257 #include <__cxx03/__variant/monostate.h>
258 #include <__cxx03/__verbose_abort>
259 #include <__cxx03/initializer_list>
260 #include <__cxx03/limits>
261 #include <__cxx03/new>
262 #include <__cxx03/version>
264 // standard-mandated includes
267 #include <__cxx03/compare>
269 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
270 # pragma GCC system_header
274 #include <__cxx03/__undef_macros>
276 namespace std { // explicitly not using versioning namespace
278 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
280 const char* what() const _NOEXCEPT override;
285 _LIBCPP_BEGIN_NAMESPACE_STD
287 #if _LIBCPP_STD_VER >= 17
289 // Light N-dimensional array of function pointers. Used in place of std::array to avoid
290 // adding a dependency.
291 template <class _Tp, size_t _Size>
293 static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit");
294 _Tp __buf_[_Size] = {};
296 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator[](size_t __n) const noexcept { return __buf_[__n]; }
299 _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS void
300 __throw_bad_variant_access() {
301 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
302 throw bad_variant_access();
304 _LIBCPP_VERBOSE_ABORT("bad_variant_access was thrown in -fno-exceptions mode");
308 template <class... _Types>
309 class _LIBCPP_TEMPLATE_VIS variant;
312 struct _LIBCPP_TEMPLATE_VIS variant_size;
315 inline constexpr size_t variant_size_v = variant_size<_Tp>::value;
318 struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
321 struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
324 struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp> : variant_size<_Tp> {};
326 template <class... _Types>
327 struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>> : integral_constant<size_t, sizeof...(_Types)> {};
329 template <size_t _Ip, class _Tp>
330 struct _LIBCPP_TEMPLATE_VIS variant_alternative;
332 template <size_t _Ip, class _Tp>
333 using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
335 template <size_t _Ip, class _Tp>
336 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp> : add_const<variant_alternative_t<_Ip, _Tp>> {};
338 template <size_t _Ip, class _Tp>
339 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp> : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
341 template <size_t _Ip, class _Tp>
342 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp> : add_cv<variant_alternative_t<_Ip, _Tp>> {};
344 template <size_t _Ip, class... _Types>
345 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
346 static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
347 using type = __type_pack_element<_Ip, _Types...>;
350 inline constexpr size_t variant_npos = static_cast<size_t>(-1);
352 template <size_t _NumAlternatives>
353 _LIBCPP_HIDE_FROM_ABI constexpr auto __choose_index_type() {
354 # ifdef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
355 if constexpr (_NumAlternatives < numeric_limits<unsigned char>::max())
356 return static_cast<unsigned char>(0);
357 else if constexpr (_NumAlternatives < numeric_limits<unsigned short>::max())
358 return static_cast<unsigned short>(0);
360 # endif // _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
361 return static_cast<unsigned int>(0);
364 template <size_t _NumAlts>
365 using __variant_index_t = decltype(std::__choose_index_type<_NumAlts>());
367 template <class _IndexType>
368 constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
370 template <class... _Types>
371 class _LIBCPP_TEMPLATE_VIS variant;
373 template <class... _Types>
374 _LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>& __as_variant(variant<_Types...>& __vs) noexcept {
378 template <class... _Types>
379 _LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>& __as_variant(const variant<_Types...>& __vs) noexcept {
383 template <class... _Types>
384 _LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>&& __as_variant(variant<_Types...>&& __vs) noexcept {
385 return std::move(__vs);
388 template <class... _Types>
389 _LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>&& __as_variant(const variant<_Types...>&& __vs) noexcept {
390 return std::move(__vs);
393 namespace __find_detail {
395 template <class _Tp, class... _Types>
396 _LIBCPP_HIDE_FROM_ABI constexpr size_t __find_index() {
397 constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
398 size_t __result = __not_found;
399 for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
400 if (__matches[__i]) {
401 if (__result != __not_found) {
410 template <size_t _Index>
411 struct __find_unambiguous_index_sfinae_impl : integral_constant<size_t, _Index> {};
414 struct __find_unambiguous_index_sfinae_impl<__not_found> {};
417 struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
419 template <class _Tp, class... _Types>
420 struct __find_unambiguous_index_sfinae : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
422 } // namespace __find_detail
424 namespace __variant_detail {
426 struct __valueless_t {};
428 enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
430 template <typename _Tp, template <typename> class _IsTriviallyAvailable, template <typename> class _IsAvailable>
431 constexpr _Trait __trait =
432 _IsTriviallyAvailable<_Tp>::value ? _Trait::_TriviallyAvailable
433 : _IsAvailable<_Tp>::value
435 : _Trait::_Unavailable;
437 _LIBCPP_HIDE_FROM_ABI constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
438 _Trait __result = _Trait::_TriviallyAvailable;
439 for (_Trait __t : __traits) {
440 if (static_cast<int>(__t) > static_cast<int>(__result)) {
447 template <typename... _Types>
449 static constexpr _Trait __copy_constructible_trait =
450 __variant_detail::__common_trait({__trait<_Types, is_trivially_copy_constructible, is_copy_constructible>...});
452 static constexpr _Trait __move_constructible_trait =
453 __variant_detail::__common_trait({__trait<_Types, is_trivially_move_constructible, is_move_constructible>...});
455 static constexpr _Trait __copy_assignable_trait = __variant_detail::__common_trait(
456 {__copy_constructible_trait, __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
458 static constexpr _Trait __move_assignable_trait = __variant_detail::__common_trait(
459 {__move_constructible_trait, __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
461 static constexpr _Trait __destructible_trait =
462 __variant_detail::__common_trait({__trait<_Types, is_trivially_destructible, is_destructible>...});
469 _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
470 return std::forward<_Vp>(__v).__head;
473 template <class _Vp, size_t _Ip>
474 _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
475 return __get_alt(std::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
480 template <size_t _Ip, class _Vp>
481 _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) {
482 return __union::__get_alt(std::forward<_Vp>(__v).__data, in_place_index<_Ip>);
487 template <size_t _Ip, class _Vp>
488 _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) {
489 return __base::__get_alt<_Ip>(std::forward<_Vp>(__v).__impl_);
493 } // namespace __access
495 namespace __visitation {
498 template <class _Visitor, class... _Vs>
499 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
500 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
501 constexpr auto __fdiagonal = __make_fdiagonal<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
502 return __fdiagonal[__index](std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...);
505 template <class _Visitor, class... _Vs>
506 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) {
507 constexpr auto __fmatrix = __make_fmatrix<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
508 return __at(__fmatrix, __vs.index()...)(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...);
513 _LIBCPP_HIDE_FROM_ABI static constexpr const _Tp& __at(const _Tp& __elem) {
517 template <class _Tp, size_t _Np, typename... _Indices>
518 _LIBCPP_HIDE_FROM_ABI static constexpr auto&&
519 __at(const __farray<_Tp, _Np>& __elems, size_t __index, _Indices... __indices) {
520 return __at(__elems[__index], __indices...);
523 template <class _Fp, class... _Fs>
524 static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_visitor_return_type_check() {
526 __all<is_same_v<_Fp, _Fs>...>::value, "`std::visit` requires the visitor to have a single return type.");
529 template <class... _Fs>
530 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_farray(_Fs&&... __fs) {
531 __std_visit_visitor_return_type_check<__remove_cvref_t<_Fs>...>();
532 using __result = __farray<common_type_t<__remove_cvref_t<_Fs>...>, sizeof...(_Fs)>;
533 return __result{{std::forward<_Fs>(__fs)...}};
536 template <size_t... _Is>
537 struct __dispatcher {
538 template <class _Fp, class... _Vs>
539 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
540 return std::__invoke(static_cast<_Fp>(__f), __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
544 template <class _Fp, class... _Vs, size_t... _Is>
545 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_dispatch(index_sequence<_Is...>) {
546 return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
549 template <size_t _Ip, class _Fp, class... _Vs>
550 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl() {
551 return __make_dispatch<_Fp, _Vs...>(index_sequence<((void)__type_identity<_Vs>{}, _Ip)...>{});
554 template <class _Fp, class... _Vs, size_t... _Is>
555 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
556 return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
559 template <class _Fp, class _Vp, class... _Vs>
560 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal() {
561 constexpr size_t __np = __remove_cvref_t<_Vp>::__size();
562 static_assert(__all<(__np == __remove_cvref_t<_Vs>::__size())...>::value);
563 return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<__np>{});
566 template <class _Fp, class... _Vs, size_t... _Is>
567 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
568 return __make_dispatch<_Fp, _Vs...>(__is);
571 template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
572 _LIBCPP_HIDE_FROM_ABI static constexpr auto
573 __make_fmatrix_impl(index_sequence<_Is...>, index_sequence<_Js...>, _Ls... __ls) {
574 return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(index_sequence<_Is..., _Js>{}, __ls...)...);
577 template <class _Fp, class... _Vs>
578 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix() {
579 return __make_fmatrix_impl<_Fp, _Vs...>(
580 index_sequence<>{}, make_index_sequence<__remove_cvref_t<_Vs>::__size()>{}...);
585 template <class _Visitor, class... _Vs>
586 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
587 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
588 return __base::__visit_alt_at(__index, std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__impl_...);
591 template <class _Visitor, class... _Vs>
592 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) {
593 return __base::__visit_alt(
594 std::forward<_Visitor>(__visitor), std::__as_variant(std::forward<_Vs>(__vs)).__impl_...);
597 template <class _Visitor, class... _Vs>
598 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
599 __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
600 return __visit_alt_at(__index, __make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
603 template <class _Visitor, class... _Vs>
604 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_value(_Visitor&& __visitor, _Vs&&... __vs) {
605 return __visit_alt(__make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
608 # if _LIBCPP_STD_VER >= 20
609 template <class _Rp, class _Visitor, class... _Vs>
610 _LIBCPP_HIDE_FROM_ABI static constexpr _Rp __visit_value(_Visitor&& __visitor, _Vs&&... __vs) {
611 return __visit_alt(__make_value_visitor<_Rp>(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
616 template <class _Visitor, class... _Values>
617 static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_exhaustive_visitor_check() {
618 static_assert(is_invocable_v<_Visitor, _Values...>, "`std::visit` requires the visitor to be exhaustive.");
621 template <class _Visitor>
622 struct __value_visitor {
623 template <class... _Alts>
624 _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Alts&&... __alts) const {
625 __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>();
626 return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
628 _Visitor&& __visitor;
631 # if _LIBCPP_STD_VER >= 20
632 template <class _Rp, class _Visitor>
633 struct __value_visitor_return_type {
634 template <class... _Alts>
635 _LIBCPP_HIDE_FROM_ABI constexpr _Rp operator()(_Alts&&... __alts) const {
636 __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>();
637 if constexpr (is_void_v<_Rp>) {
638 std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
640 return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
644 _Visitor&& __visitor;
648 template <class _Visitor>
649 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
650 return __value_visitor<_Visitor>{std::forward<_Visitor>(__visitor)};
653 # if _LIBCPP_STD_VER >= 20
654 template <class _Rp, class _Visitor>
655 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
656 return __value_visitor_return_type<_Rp, _Visitor>{std::forward<_Visitor>(__visitor)};
661 } // namespace __visitation
663 // Adding semi-colons in macro expansions helps clang-format to do a better job.
664 // This macro is used to avoid compilation errors due to "stray" semi-colons.
665 # define _LIBCPP_EAT_SEMICOLON static_assert(true, "")
667 template <size_t _Index, class _Tp>
668 struct _LIBCPP_TEMPLATE_VIS __alt {
669 using __value_type = _Tp;
670 static constexpr size_t __index = _Index;
672 template <class... _Args>
673 _LIBCPP_HIDE_FROM_ABI explicit constexpr __alt(in_place_t, _Args&&... __args)
674 : __value(std::forward<_Args>(__args)...) {}
676 __value_type __value;
679 template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
680 union _LIBCPP_TEMPLATE_VIS __union;
682 template <_Trait _DestructibleTrait, size_t _Index>
683 union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
685 # define _LIBCPP_VARIANT_UNION(destructible_trait, destructor_definition) \
686 template <size_t _Index, class _Tp, class... _Types> \
687 union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, _Index, _Tp, _Types...> { \
689 _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
691 template <class... _Args> \
692 _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
693 : __head(in_place, std::forward<_Args>(__args)...) {} \
695 template <size_t _Ip, class... _Args> \
696 _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
697 : __tail(in_place_index<_Ip - 1>, std::forward<_Args>(__args)...) {} \
699 _LIBCPP_HIDE_FROM_ABI __union(const __union&) = default; \
700 _LIBCPP_HIDE_FROM_ABI __union(__union&&) = default; \
701 _LIBCPP_HIDE_FROM_ABI __union& operator=(const __union&) = default; \
702 _LIBCPP_HIDE_FROM_ABI __union& operator=(__union&&) = default; \
703 destructor_definition; \
707 __alt<_Index, _Tp> __head; \
708 __union<destructible_trait, _Index + 1, _Types...> __tail; \
710 friend struct __access::__union; \
713 _LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable,
714 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() = default);
715 _LIBCPP_VARIANT_UNION(
716 _Trait::_Available, _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() {} _LIBCPP_EAT_SEMICOLON);
717 _LIBCPP_VARIANT_UNION(_Trait::_Unavailable, _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() = delete);
719 # undef _LIBCPP_VARIANT_UNION
721 template <_Trait _DestructibleTrait, class... _Types>
722 class _LIBCPP_TEMPLATE_VIS __base {
724 using __index_t = __variant_index_t<sizeof...(_Types)>;
726 _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(__valueless_t __tag) noexcept
727 : __data(__tag), __index(__variant_npos<__index_t>) {}
729 template <size_t _Ip, class... _Args>
730 _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
731 : __data(in_place_index<_Ip>, std::forward<_Args>(__args)...), __index(_Ip) {}
733 _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept { return index() == variant_npos; }
735 _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept {
736 return __index == __variant_npos<__index_t> ? variant_npos : __index;
740 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() & { return *this; }
742 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() && { return std::move(*this); }
744 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const& { return *this; }
746 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const&& { return std::move(*this); }
748 _LIBCPP_HIDE_FROM_ABI static constexpr size_t __size() { return sizeof...(_Types); }
750 __union<_DestructibleTrait, 0, _Types...> __data;
753 friend struct __access::__base;
754 friend struct __visitation::__base;
757 template <class _Traits, _Trait = _Traits::__destructible_trait>
758 class _LIBCPP_TEMPLATE_VIS __dtor;
760 # define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor_definition, destroy) \
761 template <class... _Types> \
762 class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, destructible_trait> \
763 : public __base<destructible_trait, _Types...> { \
764 using __base_type = __base<destructible_trait, _Types...>; \
765 using __index_t = typename __base_type::__index_t; \
768 using __base_type::__base_type; \
769 using __base_type::operator=; \
770 _LIBCPP_HIDE_FROM_ABI __dtor(const __dtor&) = default; \
771 _LIBCPP_HIDE_FROM_ABI __dtor(__dtor&&) = default; \
772 _LIBCPP_HIDE_FROM_ABI __dtor& operator=(const __dtor&) = default; \
773 _LIBCPP_HIDE_FROM_ABI __dtor& operator=(__dtor&&) = default; \
774 destructor_definition; \
780 _LIBCPP_VARIANT_DESTRUCTOR(
781 _Trait::_TriviallyAvailable,
782 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() = default,
783 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept {
784 this->__index = __variant_npos<__index_t>;
785 } _LIBCPP_EAT_SEMICOLON);
787 _LIBCPP_VARIANT_DESTRUCTOR(
789 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() { __destroy(); } _LIBCPP_EAT_SEMICOLON,
790 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept {
791 if (!this->valueless_by_exception()) {
792 __visitation::__base::__visit_alt(
793 [](auto& __alt) noexcept {
794 using __alt_type = __remove_cvref_t<decltype(__alt)>;
799 this->__index = __variant_npos<__index_t>;
800 } _LIBCPP_EAT_SEMICOLON);
802 _LIBCPP_VARIANT_DESTRUCTOR(_Trait::_Unavailable,
803 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() = delete,
804 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept = delete);
806 # undef _LIBCPP_VARIANT_DESTRUCTOR
808 template <class _Traits>
809 class _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
810 using __base_type = __dtor<_Traits>;
813 using __base_type::__base_type;
814 using __base_type::operator=;
817 template <class _Rhs>
818 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
820 if (!__rhs.valueless_by_exception()) {
821 auto __rhs_index = __rhs.index();
822 __visitation::__base::__visit_alt_at(
824 [&__lhs](auto&& __rhs_alt) {
825 std::__construct_at(std::addressof(__lhs.__data),
826 in_place_index<__decay_t<decltype(__rhs_alt)>::__index>,
827 std::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
829 std::forward<_Rhs>(__rhs));
830 __lhs.__index = __rhs_index;
835 template <class _Traits, _Trait = _Traits::__move_constructible_trait>
836 class _LIBCPP_TEMPLATE_VIS __move_constructor;
838 # define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, move_constructor_definition) \
839 template <class... _Types> \
840 class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, move_constructible_trait> \
841 : public __ctor<__traits<_Types...>> { \
842 using __base_type = __ctor<__traits<_Types...>>; \
845 using __base_type::__base_type; \
846 using __base_type::operator=; \
848 _LIBCPP_HIDE_FROM_ABI __move_constructor(const __move_constructor&) = default; \
849 _LIBCPP_HIDE_FROM_ABI ~__move_constructor() = default; \
850 _LIBCPP_HIDE_FROM_ABI __move_constructor& operator=(const __move_constructor&) = default; \
851 _LIBCPP_HIDE_FROM_ABI __move_constructor& operator=(__move_constructor&&) = default; \
852 move_constructor_definition; \
855 _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
856 _Trait::_TriviallyAvailable,
857 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&& __that) = default);
859 _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
861 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&& __that) noexcept(
862 __all<is_nothrow_move_constructible_v<_Types>...>::value)
863 : __move_constructor(__valueless_t{}) {
864 this->__generic_construct(*this, std::move(__that));
865 } _LIBCPP_EAT_SEMICOLON);
867 _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
868 _Trait::_Unavailable,
869 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&&) = delete);
871 # undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
873 template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
874 class _LIBCPP_TEMPLATE_VIS __copy_constructor;
876 # define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, copy_constructor_definition) \
877 template <class... _Types> \
878 class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, copy_constructible_trait> \
879 : public __move_constructor<__traits<_Types...>> { \
880 using __base_type = __move_constructor<__traits<_Types...>>; \
883 using __base_type::__base_type; \
884 using __base_type::operator=; \
886 _LIBCPP_HIDE_FROM_ABI __copy_constructor(__copy_constructor&&) = default; \
887 _LIBCPP_HIDE_FROM_ABI ~__copy_constructor() = default; \
888 _LIBCPP_HIDE_FROM_ABI __copy_constructor& operator=(const __copy_constructor&) = default; \
889 _LIBCPP_HIDE_FROM_ABI __copy_constructor& operator=(__copy_constructor&&) = default; \
890 copy_constructor_definition; \
893 _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
894 _Trait::_TriviallyAvailable,
895 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor& __that) = default);
897 _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
899 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor& __that)
900 : __copy_constructor(__valueless_t{}) { this->__generic_construct(*this, __that); } _LIBCPP_EAT_SEMICOLON);
902 _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
903 _Trait::_Unavailable,
904 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor&) = delete);
906 # undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
908 template <class _Traits>
909 class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
910 using __base_type = __copy_constructor<_Traits>;
913 using __base_type::__base_type;
914 using __base_type::operator=;
916 template <size_t _Ip, class... _Args>
917 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto& __emplace(_Args&&... __args) {
919 std::__construct_at(std::addressof(this->__data), in_place_index<_Ip>, std::forward<_Args>(__args)...);
921 return __access::__base::__get_alt<_Ip>(*this).__value;
925 template <size_t _Ip, class _Tp, class _Arg>
926 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
927 if (this->index() == _Ip) {
928 __a.__value = std::forward<_Arg>(__arg);
931 _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()(true_type) const {
932 __this->__emplace<_Ip>(std::forward<_Arg>(__arg));
934 _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()(false_type) const {
935 __this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg)));
937 __assignment* __this;
939 } __impl{this, std::forward<_Arg>(__arg)};
940 __impl(bool_constant < is_nothrow_constructible_v<_Tp, _Arg> || !is_nothrow_move_constructible_v < _Tp >> {});
944 template <class _That>
945 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __generic_assign(_That&& __that) {
946 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
948 } else if (__that.valueless_by_exception()) {
951 __visitation::__base::__visit_alt_at(
953 [this](auto& __this_alt, auto&& __that_alt) {
954 this->__assign_alt(__this_alt, std::forward<decltype(__that_alt)>(__that_alt).__value);
957 std::forward<_That>(__that));
962 template <class _Traits, _Trait = _Traits::__move_assignable_trait>
963 class _LIBCPP_TEMPLATE_VIS __move_assignment;
965 # define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, move_assignment_definition) \
966 template <class... _Types> \
967 class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, move_assignable_trait> \
968 : public __assignment<__traits<_Types...>> { \
969 using __base_type = __assignment<__traits<_Types...>>; \
972 using __base_type::__base_type; \
973 using __base_type::operator=; \
975 _LIBCPP_HIDE_FROM_ABI __move_assignment(const __move_assignment&) = default; \
976 _LIBCPP_HIDE_FROM_ABI __move_assignment(__move_assignment&&) = default; \
977 _LIBCPP_HIDE_FROM_ABI ~__move_assignment() = default; \
978 _LIBCPP_HIDE_FROM_ABI __move_assignment& operator=(const __move_assignment&) = default; \
979 move_assignment_definition; \
982 _LIBCPP_VARIANT_MOVE_ASSIGNMENT(_Trait::_TriviallyAvailable,
983 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment& operator=(
984 __move_assignment&& __that) = default);
986 _LIBCPP_VARIANT_MOVE_ASSIGNMENT(
988 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment&
989 operator=(__move_assignment&& __that) noexcept(
990 __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_move_assignable_v<_Types>)...>::value) {
991 this->__generic_assign(std::move(__that));
993 } _LIBCPP_EAT_SEMICOLON);
995 _LIBCPP_VARIANT_MOVE_ASSIGNMENT(
996 _Trait::_Unavailable,
997 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment& operator=(__move_assignment&&) = delete);
999 # undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
1001 template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
1002 class _LIBCPP_TEMPLATE_VIS __copy_assignment;
1004 # define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, copy_assignment_definition) \
1005 template <class... _Types> \
1006 class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, copy_assignable_trait> \
1007 : public __move_assignment<__traits<_Types...>> { \
1008 using __base_type = __move_assignment<__traits<_Types...>>; \
1011 using __base_type::__base_type; \
1012 using __base_type::operator=; \
1014 _LIBCPP_HIDE_FROM_ABI __copy_assignment(const __copy_assignment&) = default; \
1015 _LIBCPP_HIDE_FROM_ABI __copy_assignment(__copy_assignment&&) = default; \
1016 _LIBCPP_HIDE_FROM_ABI ~__copy_assignment() = default; \
1017 _LIBCPP_HIDE_FROM_ABI __copy_assignment& operator=(__copy_assignment&&) = default; \
1018 copy_assignment_definition; \
1021 _LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_TriviallyAvailable,
1022 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment& operator=(
1023 const __copy_assignment& __that) = default);
1025 _LIBCPP_VARIANT_COPY_ASSIGNMENT(
1027 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment&
1028 operator=(const __copy_assignment& __that) {
1029 this->__generic_assign(__that);
1031 } _LIBCPP_EAT_SEMICOLON);
1033 _LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_Unavailable,
1034 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment& operator=(
1035 const __copy_assignment&) = delete);
1037 # undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1039 template <class... _Types>
1040 class _LIBCPP_TEMPLATE_VIS __impl : public __copy_assignment<__traits<_Types...>> {
1041 using __base_type = __copy_assignment<__traits<_Types...>>;
1044 using __base_type::__base_type; // get in_place_index_t constructor & friends
1045 _LIBCPP_HIDE_FROM_ABI __impl(__impl const&) = default;
1046 _LIBCPP_HIDE_FROM_ABI __impl(__impl&&) = default;
1047 _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl const&) = default;
1048 _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl&&) = default;
1050 template <size_t _Ip, class _Arg>
1051 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign(_Arg&& __arg) {
1052 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this), std::forward<_Arg>(__arg));
1055 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __swap(__impl& __that) {
1056 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1058 } else if (this->index() == __that.index()) {
1059 __visitation::__base::__visit_alt_at(
1061 [](auto& __this_alt, auto& __that_alt) {
1063 swap(__this_alt.__value, __that_alt.__value);
1068 __impl* __lhs = this;
1069 __impl* __rhs = std::addressof(__that);
1070 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1071 std::swap(__lhs, __rhs);
1073 __impl __tmp(std::move(*__rhs));
1074 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1075 if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
1076 this->__generic_construct(*__rhs, std::move(*__lhs));
1078 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1079 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1080 // into `__rhs` and provide the strong exception safety guarantee.
1082 this->__generic_construct(*__rhs, std::move(*__lhs));
1084 if (__tmp.__move_nothrow()) {
1085 this->__generic_construct(*__rhs, std::move(__tmp));
1091 // this isn't consolidated with the `if constexpr` branch above due to
1092 // `throw` being ill-formed with exceptions disabled even when discarded.
1093 this->__generic_construct(*__rhs, std::move(*__lhs));
1095 this->__generic_construct(*__lhs, std::move(__tmp));
1100 constexpr inline _LIBCPP_HIDE_FROM_ABI bool __move_nothrow() const {
1101 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1102 return this->valueless_by_exception() || __results[this->index()];
1106 struct __no_narrowing_check {
1107 template <class _Dest, class _Source>
1108 using _Apply = __type_identity<_Dest>;
1111 struct __narrowing_check {
1112 template <class _Dest>
1113 static auto __test_impl(_Dest (&&)[1]) -> __type_identity<_Dest>;
1114 template <class _Dest, class _Source>
1115 using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({std::declval<_Source>()}));
1118 template <class _Dest, class _Source>
1119 using __check_for_narrowing _LIBCPP_NODEBUG =
1120 typename _If< is_arithmetic<_Dest>::value, __narrowing_check, __no_narrowing_check >::template _Apply<_Dest,
1123 template <class _Tp, size_t _Idx>
1125 template <class _Up>
1126 auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
1129 template <class... _Bases>
1130 struct __all_overloads : _Bases... {
1131 void operator()() const;
1132 using _Bases::operator()...;
1135 template <class _IdxSeq>
1136 struct __make_overloads_imp;
1138 template <size_t... _Idx>
1139 struct __make_overloads_imp<__tuple_indices<_Idx...> > {
1140 template <class... _Types>
1141 using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>;
1144 template <class... _Types>
1145 using _MakeOverloads _LIBCPP_NODEBUG =
1146 typename __make_overloads_imp< __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
1148 template <class _Tp, class... _Types>
1149 using __best_match_t = typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
1151 } // namespace __variant_detail
1153 template <class _Visitor, class... _Vs, typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>>
1154 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr decltype(auto)
1155 visit(_Visitor&& __visitor, _Vs&&... __vs);
1157 # if _LIBCPP_STD_VER >= 20
1158 template <class _Rp,
1161 typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>>
1162 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp
1163 visit(_Visitor&& __visitor, _Vs&&... __vs);
1166 template <class... _Types>
1167 class _LIBCPP_TEMPLATE_VIS _LIBCPP_DECLSPEC_EMPTY_BASES variant
1168 : private __sfinae_ctor_base< __all<is_copy_constructible_v<_Types>...>::value,
1169 __all<is_move_constructible_v<_Types>...>::value>,
1170 private __sfinae_assign_base<
1171 __all<(is_copy_constructible_v<_Types> && is_copy_assignable_v<_Types>)...>::value,
1172 __all<(is_move_constructible_v<_Types> && is_move_assignable_v<_Types>)...>::value> {
1173 static_assert(0 < sizeof...(_Types), "variant must consist of at least one alternative.");
1175 static_assert(__all<!is_array_v<_Types>...>::value, "variant can not have an array type as an alternative.");
1177 static_assert(__all<!is_reference_v<_Types>...>::value, "variant can not have a reference type as an alternative.");
1179 static_assert(__all<!is_void_v<_Types>...>::value, "variant can not have a void type as an alternative.");
1181 using __first_type = variant_alternative_t<0, variant>;
1184 using __trivially_relocatable =
1185 conditional_t<_And<__libcpp_is_trivially_relocatable<_Types>...>::value, variant, void>;
1187 template <bool _Dummy = true,
1188 enable_if_t<__dependent_type<is_default_constructible<__first_type>, _Dummy>::value, int> = 0>
1189 _LIBCPP_HIDE_FROM_ABI constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1190 : __impl_(in_place_index<0>) {}
1192 _LIBCPP_HIDE_FROM_ABI constexpr variant(const variant&) = default;
1193 _LIBCPP_HIDE_FROM_ABI constexpr variant(variant&&) = default;
1195 template < class _Arg,
1196 enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1197 enable_if_t<!__is_inplace_type<__remove_cvref_t<_Arg>>::value, int> = 0,
1198 enable_if_t<!__is_inplace_index<__remove_cvref_t<_Arg>>::value, int> = 0,
1199 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1200 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1201 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1202 _LIBCPP_HIDE_FROM_ABI constexpr variant(_Arg&& __arg) noexcept(is_nothrow_constructible_v<_Tp, _Arg>)
1203 : __impl_(in_place_index<_Ip>, std::forward<_Arg>(__arg)) {}
1205 template <size_t _Ip,
1207 class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
1208 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1209 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1210 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_index_t<_Ip>, _Args&&... __args) noexcept(
1211 is_nothrow_constructible_v<_Tp, _Args...>)
1212 : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
1214 template < size_t _Ip,
1217 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1218 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1219 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1220 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
1221 in_place_index_t<_Ip>,
1222 initializer_list<_Up> __il,
1223 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1224 : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
1226 template < class _Tp,
1228 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1229 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1230 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1231 is_nothrow_constructible_v<_Tp, _Args...>)
1232 : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
1234 template < class _Tp,
1237 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1238 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1239 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
1240 in_place_type_t<_Tp>,
1241 initializer_list<_Up> __il,
1242 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1243 : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
1245 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~variant() = default;
1247 _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(const variant&) = default;
1248 _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(variant&&) = default;
1250 template < class _Arg,
1251 enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1252 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1253 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1254 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>, int> = 0>
1255 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 variant&
1256 operator=(_Arg&& __arg) noexcept(is_nothrow_assignable_v<_Tp&, _Arg> && is_nothrow_constructible_v<_Tp, _Arg>) {
1257 __impl_.template __assign<_Ip>(std::forward<_Arg>(__arg));
1261 template < size_t _Ip,
1263 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1264 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1265 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1266 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
1267 return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
1270 template < size_t _Ip,
1273 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1274 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1275 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1276 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1277 return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
1280 template < class _Tp,
1282 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1283 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1284 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
1285 return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
1288 template < class _Tp,
1291 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1292 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1293 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1294 return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
1297 _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept {
1298 return __impl_.valueless_by_exception();
1301 _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { return __impl_.index(); }
1303 template < bool _Dummy = true,
1304 enable_if_t< __all<(__dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1305 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1307 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(variant& __that) noexcept(
1308 __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_swappable_v<_Types>)...>::value) {
1309 __impl_.__swap(__that.__impl_);
1312 # if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_HAS_EXPLICIT_THIS_PARAMETER)
1313 // Helper class to implement [variant.visit]/10
1314 // Constraints: The call to visit does not use an explicit template-argument-list
1315 // that begins with a type template-argument.
1316 struct __variant_visit_barrier_tag {
1317 _LIBCPP_HIDE_FROM_ABI explicit __variant_visit_barrier_tag() = default;
1320 template <__variant_visit_barrier_tag = __variant_visit_barrier_tag{}, class _Self, class _Visitor>
1321 _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) visit(this _Self&& __self, _Visitor&& __visitor) {
1322 using _VariantT = _OverrideRef<_Self&&, _CopyConst<remove_reference_t<_Self>, variant>>;
1323 return std::visit(std::forward<_Visitor>(__visitor), (_VariantT)__self);
1326 template <class _Rp, class _Self, class _Visitor>
1327 _LIBCPP_HIDE_FROM_ABI constexpr _Rp visit(this _Self&& __self, _Visitor&& __visitor) {
1328 using _VariantT = _OverrideRef<_Self&&, _CopyConst<remove_reference_t<_Self>, variant>>;
1329 return std::visit<_Rp>(std::forward<_Visitor>(__visitor), (_VariantT)__self);
1334 __variant_detail::__impl<_Types...> __impl_;
1336 friend struct __variant_detail::__access::__variant;
1337 friend struct __variant_detail::__visitation::__variant;
1340 template <size_t _Ip, class... _Types>
1341 _LIBCPP_HIDE_FROM_ABI constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1342 return __v.index() == _Ip;
1345 template <class _Tp, class... _Types>
1346 _LIBCPP_HIDE_FROM_ABI constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1347 return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1350 template <size_t _Ip, class _Vp>
1351 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr auto&& __generic_get(_Vp&& __v) {
1352 using __variant_detail::__access::__variant;
1353 if (!std::__holds_alternative<_Ip>(__v)) {
1354 __throw_bad_variant_access();
1356 return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
1359 template <size_t _Ip, class... _Types>
1360 _LIBCPP_HIDE_FROM_ABI
1361 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&
1362 get(variant<_Types...>& __v) {
1363 static_assert(_Ip < sizeof...(_Types));
1364 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1365 return std::__generic_get<_Ip>(__v);
1368 template <size_t _Ip, class... _Types>
1369 _LIBCPP_HIDE_FROM_ABI
1370 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&&
1371 get(variant<_Types...>&& __v) {
1372 static_assert(_Ip < sizeof...(_Types));
1373 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1374 return std::__generic_get<_Ip>(std::move(__v));
1377 template <size_t _Ip, class... _Types>
1378 _LIBCPP_HIDE_FROM_ABI
1379 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&
1380 get(const variant<_Types...>& __v) {
1381 static_assert(_Ip < sizeof...(_Types));
1382 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1383 return std::__generic_get<_Ip>(__v);
1386 template <size_t _Ip, class... _Types>
1387 _LIBCPP_HIDE_FROM_ABI
1388 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&&
1389 get(const variant<_Types...>&& __v) {
1390 static_assert(_Ip < sizeof...(_Types));
1391 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1392 return std::__generic_get<_Ip>(std::move(__v));
1395 template <class _Tp, class... _Types>
1396 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp& get(variant<_Types...>& __v) {
1397 static_assert(!is_void_v<_Tp>);
1398 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1401 template <class _Tp, class... _Types>
1402 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp&& get(variant<_Types...>&& __v) {
1403 static_assert(!is_void_v<_Tp>);
1404 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
1407 template <class _Tp, class... _Types>
1408 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp&
1409 get(const variant<_Types...>& __v) {
1410 static_assert(!is_void_v<_Tp>);
1411 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1414 template <class _Tp, class... _Types>
1415 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp&&
1416 get(const variant<_Types...>&& __v) {
1417 static_assert(!is_void_v<_Tp>);
1418 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
1421 template <size_t _Ip, class _Vp>
1422 _LIBCPP_HIDE_FROM_ABI constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1423 using __variant_detail::__access::__variant;
1424 return __v && std::__holds_alternative<_Ip>(*__v) ? std::addressof(__variant::__get_alt<_Ip>(*__v).__value) : nullptr;
1427 template <size_t _Ip, class... _Types>
1428 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1429 get_if(variant<_Types...>* __v) noexcept {
1430 static_assert(_Ip < sizeof...(_Types));
1431 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1432 return std::__generic_get_if<_Ip>(__v);
1435 template <size_t _Ip, class... _Types>
1436 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1437 get_if(const variant<_Types...>* __v) noexcept {
1438 static_assert(_Ip < sizeof...(_Types));
1439 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1440 return std::__generic_get_if<_Ip>(__v);
1443 template <class _Tp, class... _Types>
1444 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> get_if(variant<_Types...>* __v) noexcept {
1445 static_assert(!is_void_v<_Tp>);
1446 return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1449 template <class _Tp, class... _Types>
1450 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const _Tp> get_if(const variant<_Types...>* __v) noexcept {
1451 static_assert(!is_void_v<_Tp>);
1452 return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1455 template <class _Operator>
1456 struct __convert_to_bool {
1457 template <class _T1, class _T2>
1458 _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_T1&& __t1, _T2&& __t2) const {
1459 static_assert(is_convertible<decltype(_Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2))), bool>::value,
1460 "the relational operator does not return a type which is implicitly convertible to bool");
1461 return _Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2));
1465 template <class... _Types>
1466 _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1467 using __variant_detail::__visitation::__variant;
1468 if (__lhs.index() != __rhs.index())
1470 if (__lhs.valueless_by_exception())
1472 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
1475 # if _LIBCPP_STD_VER >= 20
1477 template <class... _Types>
1478 requires(three_way_comparable<_Types> && ...)
1479 _LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...>
1480 operator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1481 using __variant_detail::__visitation::__variant;
1482 using __result_t = common_comparison_category_t<compare_three_way_result_t<_Types>...>;
1483 if (__lhs.valueless_by_exception() && __rhs.valueless_by_exception())
1484 return strong_ordering::equal;
1485 if (__lhs.valueless_by_exception())
1486 return strong_ordering::less;
1487 if (__rhs.valueless_by_exception())
1488 return strong_ordering::greater;
1489 if (auto __c = __lhs.index() <=> __rhs.index(); __c != 0)
1491 auto __three_way = []<class _Type>(const _Type& __v, const _Type& __w) -> __result_t { return __v <=> __w; };
1492 return __variant::__visit_value_at(__lhs.index(), __three_way, __lhs, __rhs);
1495 # endif // _LIBCPP_STD_VER >= 20
1497 template <class... _Types>
1498 _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1499 using __variant_detail::__visitation::__variant;
1500 if (__lhs.index() != __rhs.index())
1502 if (__lhs.valueless_by_exception())
1504 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
1507 template <class... _Types>
1508 _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1509 using __variant_detail::__visitation::__variant;
1510 if (__rhs.valueless_by_exception())
1512 if (__lhs.valueless_by_exception())
1514 if (__lhs.index() < __rhs.index())
1516 if (__lhs.index() > __rhs.index())
1518 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
1521 template <class... _Types>
1522 _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1523 using __variant_detail::__visitation::__variant;
1524 if (__lhs.valueless_by_exception())
1526 if (__rhs.valueless_by_exception())
1528 if (__lhs.index() > __rhs.index())
1530 if (__lhs.index() < __rhs.index())
1532 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
1535 template <class... _Types>
1536 _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1537 using __variant_detail::__visitation::__variant;
1538 if (__lhs.valueless_by_exception())
1540 if (__rhs.valueless_by_exception())
1542 if (__lhs.index() < __rhs.index())
1544 if (__lhs.index() > __rhs.index())
1546 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
1549 template <class... _Types>
1550 _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1551 using __variant_detail::__visitation::__variant;
1552 if (__rhs.valueless_by_exception())
1554 if (__lhs.valueless_by_exception())
1556 if (__lhs.index() > __rhs.index())
1558 if (__lhs.index() < __rhs.index())
1560 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
1563 template <class... _Vs>
1564 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr void __throw_if_valueless(_Vs&&... __vs) {
1565 const bool __valueless = (... || std::__as_variant(__vs).valueless_by_exception());
1567 __throw_bad_variant_access();
1571 template < class _Visitor, class... _Vs, typename>
1572 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr decltype(auto)
1573 visit(_Visitor&& __visitor, _Vs&&... __vs) {
1574 using __variant_detail::__visitation::__variant;
1575 std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1576 return __variant::__visit_value(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
1579 # if _LIBCPP_STD_VER >= 20
1580 template < class _Rp, class _Visitor, class... _Vs, typename>
1581 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp
1582 visit(_Visitor&& __visitor, _Vs&&... __vs) {
1583 using __variant_detail::__visitation::__variant;
1584 std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1585 return __variant::__visit_value<_Rp>(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
1589 template <class... _Types>
1590 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto
1591 swap(variant<_Types...>& __lhs,
1592 variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs))) -> decltype(__lhs.swap(__rhs)) {
1593 return __lhs.swap(__rhs);
1596 template <class... _Types>
1597 struct _LIBCPP_TEMPLATE_VIS hash< __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
1598 using argument_type = variant<_Types...>;
1599 using result_type = size_t;
1601 _LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type& __v) const {
1602 using __variant_detail::__visitation::__variant;
1604 __v.valueless_by_exception()
1605 ? 299792458 // Random value chosen by the universe upon creation
1606 : __variant::__visit_alt(
1607 [](const auto& __alt) {
1608 using __alt_type = __remove_cvref_t<decltype(__alt)>;
1609 using __value_type = remove_const_t< typename __alt_type::__value_type>;
1610 return hash<__value_type>{}(__alt.__value);
1613 return std::__hash_combine(__res, hash<size_t>{}(__v.index()));
1617 // __unchecked_get is the same as std::get, except, it is UB to use it with the wrong
1618 // type whereas std::get will throw or returning nullptr. This makes it faster than
1620 template <size_t _Ip, class _Vp>
1621 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(_Vp&& __v) noexcept {
1622 using __variant_detail::__access::__variant;
1623 return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
1626 template <class _Tp, class... _Types>
1627 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept {
1628 return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1631 template <class _Tp, class... _Types>
1632 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept {
1633 return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1636 #endif // _LIBCPP_STD_VER >= 17
1638 _LIBCPP_END_NAMESPACE_STD
1642 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1643 # include <__cxx03/exception>
1644 # include <__cxx03/tuple>
1645 # include <__cxx03/type_traits>
1646 # include <__cxx03/typeinfo>
1647 # include <__cxx03/utility>
1650 #endif // _LIBCPP_VARIANT