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