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