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    ~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
261namespace std { // explicitly not using versioning namespace
262
263class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
264public:
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.
276template<class _Tp, size_t _Size>
277struct __farray {
278  static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit");
279  _Tp __buf_[_Size] = {};
280
281  _LIBCPP_HIDE_FROM_ABI constexpr
282  const _Tp &operator[](size_t __n) const noexcept {
283      return __buf_[__n];
284  }
285};
286
287_LIBCPP_NORETURN
288inline _LIBCPP_HIDE_FROM_ABI
289_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
290void __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
298template <class... _Types>
299class _LIBCPP_TEMPLATE_VIS variant;
300
301template <class _Tp>
302struct _LIBCPP_TEMPLATE_VIS variant_size;
303
304template <class _Tp>
305inline constexpr size_t variant_size_v = variant_size<_Tp>::value;
306
307template <class _Tp>
308struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
309
310template <class _Tp>
311struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
312
313template <class _Tp>
314struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
315    : variant_size<_Tp> {};
316
317template <class... _Types>
318struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
319    : integral_constant<size_t, sizeof...(_Types)> {};
320
321template <size_t _Ip, class _Tp>
322struct _LIBCPP_TEMPLATE_VIS variant_alternative;
323
324template <size_t _Ip, class _Tp>
325using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
326
327template <size_t _Ip, class _Tp>
328struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
329    : add_const<variant_alternative_t<_Ip, _Tp>> {};
330
331template <size_t _Ip, class _Tp>
332struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
333    : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
334
335template <size_t _Ip, class _Tp>
336struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
337    : add_cv<variant_alternative_t<_Ip, _Tp>> {};
338
339template <size_t _Ip, class... _Types>
340struct _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
345inline 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
355template <size_t _NumAlts>
356using __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
366template <class _IndexType>
367constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
368
369template <class... _Types>
370class _LIBCPP_TEMPLATE_VIS variant;
371
372template <class... _Types>
373_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>&
374__as_variant(variant<_Types...>& __vs) noexcept {
375  return __vs;
376}
377
378template <class... _Types>
379_LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>&
380__as_variant(const variant<_Types...>& __vs) noexcept {
381  return __vs;
382}
383
384template <class... _Types>
385_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>&&
386__as_variant(variant<_Types...>&& __vs) noexcept {
387  return std::move(__vs);
388}
389
390template <class... _Types>
391_LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>&&
392__as_variant(const variant<_Types...>&& __vs) noexcept {
393  return std::move(__vs);
394}
395
396namespace __find_detail {
397
398template <class _Tp, class... _Types>
399_LIBCPP_HIDE_FROM_ABI
400constexpr 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
414template <size_t _Index>
415struct __find_unambiguous_index_sfinae_impl
416    : integral_constant<size_t, _Index> {};
417
418template <>
419struct __find_unambiguous_index_sfinae_impl<__not_found> {};
420
421template <>
422struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
423
424template <class _Tp, class... _Types>
425struct __find_unambiguous_index_sfinae
426    : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
427
428} // namespace __find_detail
429
430namespace __variant_detail {
431
432struct __valueless_t {};
433
434enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
435
436template <typename _Tp,
437          template <typename> class _IsTriviallyAvailable,
438          template <typename> class _IsAvailable>
439constexpr _Trait __trait =
440    _IsTriviallyAvailable<_Tp>::value
441        ? _Trait::_TriviallyAvailable
442        : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
443
444_LIBCPP_HIDE_FROM_ABI
445constexpr _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
455template <typename... _Types>
456struct __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
479namespace __access {
480
481struct __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 std::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(std::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
492  }
493};
494
495struct __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(std::forward<_Vp>(__v).__data,
500                              in_place_index<_Ip>);
501  }
502};
503
504struct __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>(std::forward<_Vp>(__v).__impl_);
509  }
510};
511
512} // namespace __access
513
514namespace __visitation {
515
516struct __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(std::forward<_Vs>(__vs).__as_base())...>();
524    return __fdiagonal[__index](std::forward<_Visitor>(__visitor),
525                                std::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(std::forward<_Vs>(__vs).__as_base())...>();
535    return __at(__fmatrix, __vs.index()...)(
536        std::forward<_Visitor>(__visitor),
537        std::forward<_Vs>(__vs).__as_base()...);
538  }
539
540private:
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{{std::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 std::__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
628struct __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                                  std::forward<_Visitor>(__visitor),
635                                  std::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        std::forward<_Visitor>(__visitor),
644        std::__as_variant(std::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(std::forward<_Visitor>(__visitor)),
654        std::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(std::forward<_Visitor>(__visitor)),
663        std::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>(std::forward<_Visitor>(__visitor)),
673        std::forward<_Vs>(__vs)...);
674  }
675#endif
676
677private:
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((std::forward<_Alts>(__alts).__value))...>();
692      return std::__invoke(std::forward<_Visitor>(__visitor),
693                             std::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((std::forward<_Alts>(__alts).__value))...>();
707      if constexpr (is_void_v<_Rp>) {
708        std::__invoke(std::forward<_Visitor>(__visitor),
709                        std::forward<_Alts>(__alts).__value...);
710      }
711      else {
712        return std::__invoke(std::forward<_Visitor>(__visitor),
713                               std::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>{std::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>{std::forward<_Visitor>(__visitor)};
732  }
733#endif
734};
735
736} // namespace __visitation
737
738template <size_t _Index, class _Tp>
739struct _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(std::forward<_Args>(__args)...) {}
746
747  __value_type __value;
748};
749
750template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
751union _LIBCPP_TEMPLATE_VIS __union;
752
753template <_Trait _DestructibleTrait, size_t _Index>
754union _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, std::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>, std::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
798template <_Trait _DestructibleTrait, class... _Types>
799class _LIBCPP_TEMPLATE_VIS __base {
800public:
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>, std::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
824protected:
825  _LIBCPP_HIDE_FROM_ABI
826  constexpr auto&& __as_base() & { return *this; }
827
828  _LIBCPP_HIDE_FROM_ABI
829  constexpr auto&& __as_base() && { return std::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 std::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
847template <class _Traits, _Trait = _Traits::__destructible_trait>
848class _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
900template <class _Traits>
901class _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
902  using __base_type = __dtor<_Traits>;
903
904public:
905  using __base_type::__base_type;
906  using __base_type::operator=;
907
908protected:
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*)std::addressof(__a))
913        __alt<_Ip, _Tp>(in_place, std::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                std::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
928          },
929          __lhs, std::forward<_Rhs>(__rhs));
930      __lhs.__index = __rhs.index();
931    }
932  }
933};
934
935template <class _Traits, _Trait = _Traits::__move_constructible_trait>
936class _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, std::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
975template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
976class _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
1014template <class _Traits>
1015class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
1016  using __base_type = __copy_constructor<_Traits>;
1017
1018public:
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                          std::forward<_Args>(__args)...);
1028    this->__index = _Ip;
1029    return __res;
1030  }
1031
1032protected:
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 = std::forward<_Arg>(__arg);
1038    } else {
1039      struct {
1040        _LIBCPP_HIDE_FROM_ABI void operator()(true_type) const {
1041          __this->__emplace<_Ip>(std::forward<_Arg>(__arg));
1042        }
1043        _LIBCPP_HIDE_FROM_ABI void operator()(false_type) const {
1044          __this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg)));
1045        }
1046        __assignment* __this;
1047        _Arg&& __arg;
1048      } __impl{this, std::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                std::forward<decltype(__that_alt)>(__that_alt).__value);
1068          },
1069          *this, std::forward<_That>(__that));
1070    }
1071  }
1072};
1073
1074template <class _Traits, _Trait = _Traits::__move_assignable_trait>
1075class _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(std::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
1115template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
1116class _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
1154template <class... _Types>
1155class _LIBCPP_TEMPLATE_VIS __impl
1156    : public __copy_assignment<__traits<_Types...>> {
1157  using __base_type = __copy_assignment<__traits<_Types...>>;
1158
1159public:
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                       std::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 std::swap;
1182            swap(__this_alt.__value, __that_alt.__value);
1183          },
1184          *this,
1185          __that);
1186    } else {
1187      __impl* __lhs = this;
1188      __impl* __rhs = std::addressof(__that);
1189      if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1190        std::swap(__lhs, __rhs);
1191      }
1192      __impl __tmp(std::move(*__rhs));
1193#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1194      if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
1195        this->__generic_construct(*__rhs, std::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, std::move(*__lhs));
1202        } catch (...) {
1203          if (__tmp.__move_nothrow()) {
1204            this->__generic_construct(*__rhs, std::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, std::move(*__lhs));
1213#endif
1214      this->__generic_construct(*__lhs, std::move(__tmp));
1215    }
1216  }
1217
1218private:
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
1226struct __no_narrowing_check {
1227  template <class _Dest, class _Source>
1228  using _Apply = __type_identity<_Dest>;
1229};
1230
1231struct __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
1238template <class _Dest, class _Source>
1239using __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
1249template <class _Tp, size_t _Idx>
1250struct __overload {
1251  template <class _Up>
1252  auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
1253};
1254
1255// TODO(LLVM-19): Remove all occurrences of this macro.
1256#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
1257template <class _Tp, size_t>
1258struct __overload_bool  {
1259  template <class _Up, class _Ap = __remove_cvref_t<_Up>>
1260  auto operator()(bool, _Up&&) const
1261      -> enable_if_t<is_same_v<_Ap, bool>, __type_identity<_Tp>>;
1262};
1263
1264template <size_t _Idx>
1265struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
1266template <size_t _Idx>
1267struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
1268template <size_t _Idx>
1269struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
1270template <size_t _Idx>
1271struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
1272#endif
1273
1274template <class ..._Bases>
1275struct __all_overloads : _Bases... {
1276  void operator()() const;
1277  using _Bases::operator()...;
1278};
1279
1280template <class _IdxSeq>
1281struct __make_overloads_imp;
1282
1283template <size_t ..._Idx>
1284struct __make_overloads_imp<__tuple_indices<_Idx...> > {
1285  template <class ..._Types>
1286  using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>;
1287};
1288
1289template <class ..._Types>
1290using _MakeOverloads _LIBCPP_NODEBUG = typename __make_overloads_imp<
1291    __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
1292
1293template <class _Tp, class... _Types>
1294using __best_match_t =
1295    typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
1296
1297} // namespace __variant_detail
1298
1299template <class... _Types>
1300class _LIBCPP_TEMPLATE_VIS _LIBCPP_DECLSPEC_EMPTY_BASES variant
1301    : private __sfinae_ctor_base<
1302          __all<is_copy_constructible_v<_Types>...>::value,
1303          __all<is_move_constructible_v<_Types>...>::value>,
1304      private __sfinae_assign_base<
1305          __all<(is_copy_constructible_v<_Types> &&
1306                 is_copy_assignable_v<_Types>)...>::value,
1307          __all<(is_move_constructible_v<_Types> &&
1308                 is_move_assignable_v<_Types>)...>::value> {
1309  static_assert(0 < sizeof...(_Types),
1310                "variant must consist of at least one alternative.");
1311
1312  static_assert(__all<!is_array_v<_Types>...>::value,
1313                "variant can not have an array type as an alternative.");
1314
1315  static_assert(__all<!is_reference_v<_Types>...>::value,
1316                "variant can not have a reference type as an alternative.");
1317
1318  static_assert(__all<!is_void_v<_Types>...>::value,
1319                "variant can not have a void type as an alternative.");
1320
1321  using __first_type = variant_alternative_t<0, variant>;
1322
1323public:
1324  template <bool _Dummy = true,
1325            enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1326                                         _Dummy>::value,
1327                        int> = 0>
1328  _LIBCPP_HIDE_FROM_ABI
1329  constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1330      : __impl_(in_place_index<0>) {}
1331
1332  _LIBCPP_HIDE_FROM_ABI constexpr variant(const variant&) = default;
1333  _LIBCPP_HIDE_FROM_ABI constexpr variant(variant&&) = default;
1334
1335  template <
1336      class _Arg,
1337      enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1338      enable_if_t<!__is_inplace_type<__remove_cvref_t<_Arg>>::value, int> = 0,
1339      enable_if_t<!__is_inplace_index<__remove_cvref_t<_Arg>>::value, int> = 0,
1340      class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1341      size_t _Ip =
1342          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1343      enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1344  _LIBCPP_HIDE_FROM_ABI
1345  constexpr variant(_Arg&& __arg) noexcept(
1346      is_nothrow_constructible_v<_Tp, _Arg>)
1347      : __impl_(in_place_index<_Ip>, std::forward<_Arg>(__arg)) {}
1348
1349  template <size_t _Ip, class... _Args,
1350            class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
1351            class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1352            enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1353  _LIBCPP_HIDE_FROM_ABI
1354  explicit constexpr variant(
1355      in_place_index_t<_Ip>,
1356      _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1357      : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
1358
1359  template <
1360      size_t _Ip,
1361      class _Up,
1362      class... _Args,
1363      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1364      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1365      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1366                  int> = 0>
1367  _LIBCPP_HIDE_FROM_ABI
1368  explicit constexpr variant(
1369      in_place_index_t<_Ip>,
1370      initializer_list<_Up> __il,
1371      _Args&&... __args) noexcept(
1372      is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1373      : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
1374
1375  template <
1376      class _Tp,
1377      class... _Args,
1378      size_t _Ip =
1379          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1380      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1381  _LIBCPP_HIDE_FROM_ABI
1382  explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1383      is_nothrow_constructible_v<_Tp, _Args...>)
1384      : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
1385
1386  template <
1387      class _Tp,
1388      class _Up,
1389      class... _Args,
1390      size_t _Ip =
1391          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1392      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1393                  int> = 0>
1394  _LIBCPP_HIDE_FROM_ABI
1395  explicit constexpr variant(
1396      in_place_type_t<_Tp>,
1397      initializer_list<_Up> __il,
1398      _Args&&... __args) noexcept(
1399      is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1400      : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
1401
1402  _LIBCPP_HIDE_FROM_ABI ~variant() = default;
1403
1404  _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(const variant&) = default;
1405  _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(variant&&) = default;
1406
1407  template <
1408      class _Arg,
1409      enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1410      class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1411      size_t _Ip =
1412          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1413      enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
1414                  int> = 0>
1415  _LIBCPP_HIDE_FROM_ABI
1416  variant& operator=(_Arg&& __arg) noexcept(
1417      is_nothrow_assignable_v<_Tp&, _Arg> &&
1418      is_nothrow_constructible_v<_Tp, _Arg>) {
1419    __impl_.template __assign<_Ip>(std::forward<_Arg>(__arg));
1420    return *this;
1421  }
1422
1423  template <
1424      size_t _Ip,
1425      class... _Args,
1426      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1427      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1428      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1429  _LIBCPP_HIDE_FROM_ABI
1430  _Tp& emplace(_Args&&... __args) {
1431    return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
1432  }
1433
1434  template <
1435      size_t _Ip,
1436      class _Up,
1437      class... _Args,
1438      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1439      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1440      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1441                  int> = 0>
1442  _LIBCPP_HIDE_FROM_ABI
1443  _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1444    return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
1445  }
1446
1447  template <
1448      class _Tp,
1449      class... _Args,
1450      size_t _Ip =
1451          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1452      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1453  _LIBCPP_HIDE_FROM_ABI
1454  _Tp& emplace(_Args&&... __args) {
1455    return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
1456  }
1457
1458  template <
1459      class _Tp,
1460      class _Up,
1461      class... _Args,
1462      size_t _Ip =
1463          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1464      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1465                  int> = 0>
1466  _LIBCPP_HIDE_FROM_ABI
1467  _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1468    return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
1469  }
1470
1471  _LIBCPP_HIDE_FROM_ABI
1472  constexpr bool valueless_by_exception() const noexcept {
1473    return __impl_.valueless_by_exception();
1474  }
1475
1476  _LIBCPP_HIDE_FROM_ABI
1477  constexpr size_t index() const noexcept { return __impl_.index(); }
1478
1479  template <
1480      bool _Dummy = true,
1481      enable_if_t<
1482          __all<(
1483              __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1484              __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1485          int> = 0>
1486  _LIBCPP_HIDE_FROM_ABI
1487  void swap(variant& __that) noexcept(
1488      __all<(is_nothrow_move_constructible_v<_Types> &&
1489             is_nothrow_swappable_v<_Types>)...>::value) {
1490    __impl_.__swap(__that.__impl_);
1491  }
1492
1493private:
1494  __variant_detail::__impl<_Types...> __impl_;
1495
1496  friend struct __variant_detail::__access::__variant;
1497  friend struct __variant_detail::__visitation::__variant;
1498};
1499
1500template <size_t _Ip, class... _Types>
1501_LIBCPP_HIDE_FROM_ABI
1502constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1503  return __v.index() == _Ip;
1504}
1505
1506template <class _Tp, class... _Types>
1507_LIBCPP_HIDE_FROM_ABI
1508constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1509  return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1510}
1511
1512template <size_t _Ip, class _Vp>
1513_LIBCPP_HIDE_FROM_ABI
1514_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1515constexpr auto&& __generic_get(_Vp&& __v) {
1516  using __variant_detail::__access::__variant;
1517  if (!std::__holds_alternative<_Ip>(__v)) {
1518    __throw_bad_variant_access();
1519  }
1520  return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
1521}
1522
1523template <size_t _Ip, class... _Types>
1524_LIBCPP_HIDE_FROM_ABI
1525_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1526constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
1527    variant<_Types...>& __v) {
1528  static_assert(_Ip < sizeof...(_Types));
1529  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1530  return std::__generic_get<_Ip>(__v);
1531}
1532
1533template <size_t _Ip, class... _Types>
1534_LIBCPP_HIDE_FROM_ABI
1535_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1536constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
1537    variant<_Types...>&& __v) {
1538  static_assert(_Ip < sizeof...(_Types));
1539  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1540  return std::__generic_get<_Ip>(std::move(__v));
1541}
1542
1543template <size_t _Ip, class... _Types>
1544_LIBCPP_HIDE_FROM_ABI
1545_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1546constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
1547    const variant<_Types...>& __v) {
1548  static_assert(_Ip < sizeof...(_Types));
1549  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1550  return std::__generic_get<_Ip>(__v);
1551}
1552
1553template <size_t _Ip, class... _Types>
1554_LIBCPP_HIDE_FROM_ABI
1555_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1556constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
1557    const variant<_Types...>&& __v) {
1558  static_assert(_Ip < sizeof...(_Types));
1559  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1560  return std::__generic_get<_Ip>(std::move(__v));
1561}
1562
1563template <class _Tp, class... _Types>
1564_LIBCPP_HIDE_FROM_ABI
1565_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1566constexpr _Tp& get(variant<_Types...>& __v) {
1567  static_assert(!is_void_v<_Tp>);
1568  return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1569}
1570
1571template <class _Tp, class... _Types>
1572_LIBCPP_HIDE_FROM_ABI
1573_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1574constexpr _Tp&& get(variant<_Types...>&& __v) {
1575  static_assert(!is_void_v<_Tp>);
1576  return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1577      std::move(__v));
1578}
1579
1580template <class _Tp, class... _Types>
1581_LIBCPP_HIDE_FROM_ABI
1582_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1583constexpr const _Tp& get(const variant<_Types...>& __v) {
1584  static_assert(!is_void_v<_Tp>);
1585  return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1586}
1587
1588template <class _Tp, class... _Types>
1589_LIBCPP_HIDE_FROM_ABI
1590_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1591constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1592  static_assert(!is_void_v<_Tp>);
1593  return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1594      std::move(__v));
1595}
1596
1597template <size_t _Ip, class _Vp>
1598_LIBCPP_HIDE_FROM_ABI
1599constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1600  using __variant_detail::__access::__variant;
1601  return __v && std::__holds_alternative<_Ip>(*__v)
1602             ? std::addressof(__variant::__get_alt<_Ip>(*__v).__value)
1603             : nullptr;
1604}
1605
1606template <size_t _Ip, class... _Types>
1607_LIBCPP_HIDE_FROM_ABI
1608constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1609get_if(variant<_Types...>* __v) noexcept {
1610  static_assert(_Ip < sizeof...(_Types));
1611  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1612  return std::__generic_get_if<_Ip>(__v);
1613}
1614
1615template <size_t _Ip, class... _Types>
1616_LIBCPP_HIDE_FROM_ABI
1617constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1618get_if(const variant<_Types...>* __v) noexcept {
1619  static_assert(_Ip < sizeof...(_Types));
1620  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1621  return std::__generic_get_if<_Ip>(__v);
1622}
1623
1624template <class _Tp, class... _Types>
1625_LIBCPP_HIDE_FROM_ABI
1626constexpr add_pointer_t<_Tp>
1627get_if(variant<_Types...>* __v) noexcept {
1628  static_assert(!is_void_v<_Tp>);
1629  return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1630}
1631
1632template <class _Tp, class... _Types>
1633_LIBCPP_HIDE_FROM_ABI
1634constexpr add_pointer_t<const _Tp>
1635get_if(const variant<_Types...>* __v) noexcept {
1636  static_assert(!is_void_v<_Tp>);
1637  return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1638}
1639
1640template <class _Operator>
1641struct __convert_to_bool {
1642  template <class _T1, class _T2>
1643  _LIBCPP_HIDE_FROM_ABI
1644  constexpr bool operator()(_T1 && __t1, _T2&& __t2) const {
1645    static_assert(is_convertible<decltype(_Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2))), bool>::value,
1646        "the relational operator does not return a type which is implicitly convertible to bool");
1647    return _Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2));
1648  }
1649};
1650
1651template <class... _Types>
1652_LIBCPP_HIDE_FROM_ABI
1653constexpr bool operator==(const variant<_Types...>& __lhs,
1654                          const variant<_Types...>& __rhs) {
1655  using __variant_detail::__visitation::__variant;
1656  if (__lhs.index() != __rhs.index()) return false;
1657  if (__lhs.valueless_by_exception()) return true;
1658  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
1659}
1660
1661#  if _LIBCPP_STD_VER >= 20
1662
1663template <class... _Types> requires (three_way_comparable<_Types> && ...)
1664_LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...>
1665operator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1666  using __variant_detail::__visitation::__variant;
1667  using __result_t = common_comparison_category_t<compare_three_way_result_t<_Types>...>;
1668  if (__lhs.valueless_by_exception() && __rhs.valueless_by_exception())
1669    return strong_ordering::equal;
1670  if (__lhs.valueless_by_exception())
1671    return strong_ordering::less;
1672  if (__rhs.valueless_by_exception())
1673    return strong_ordering::greater;
1674  if (auto __c = __lhs.index() <=> __rhs.index(); __c != 0)
1675    return __c;
1676  auto __three_way = []<class _Type>(const _Type& __v, const _Type& __w) -> __result_t { return __v <=> __w; };
1677  return __variant::__visit_value_at(__lhs.index(), __three_way, __lhs, __rhs);
1678}
1679
1680#  endif // _LIBCPP_STD_VER >= 20
1681
1682template <class... _Types>
1683_LIBCPP_HIDE_FROM_ABI
1684constexpr bool operator!=(const variant<_Types...>& __lhs,
1685                          const variant<_Types...>& __rhs) {
1686  using __variant_detail::__visitation::__variant;
1687  if (__lhs.index() != __rhs.index()) return true;
1688  if (__lhs.valueless_by_exception()) return false;
1689  return __variant::__visit_value_at(
1690      __lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
1691}
1692
1693template <class... _Types>
1694_LIBCPP_HIDE_FROM_ABI
1695constexpr bool operator<(const variant<_Types...>& __lhs,
1696                         const variant<_Types...>& __rhs) {
1697  using __variant_detail::__visitation::__variant;
1698  if (__rhs.valueless_by_exception()) return false;
1699  if (__lhs.valueless_by_exception()) return true;
1700  if (__lhs.index() < __rhs.index()) return true;
1701  if (__lhs.index() > __rhs.index()) return false;
1702  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
1703}
1704
1705template <class... _Types>
1706_LIBCPP_HIDE_FROM_ABI
1707constexpr bool operator>(const variant<_Types...>& __lhs,
1708                         const variant<_Types...>& __rhs) {
1709  using __variant_detail::__visitation::__variant;
1710  if (__lhs.valueless_by_exception()) return false;
1711  if (__rhs.valueless_by_exception()) return true;
1712  if (__lhs.index() > __rhs.index()) return true;
1713  if (__lhs.index() < __rhs.index()) return false;
1714  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
1715}
1716
1717template <class... _Types>
1718_LIBCPP_HIDE_FROM_ABI
1719constexpr bool operator<=(const variant<_Types...>& __lhs,
1720                          const variant<_Types...>& __rhs) {
1721  using __variant_detail::__visitation::__variant;
1722  if (__lhs.valueless_by_exception()) return true;
1723  if (__rhs.valueless_by_exception()) return false;
1724  if (__lhs.index() < __rhs.index()) return true;
1725  if (__lhs.index() > __rhs.index()) return false;
1726  return __variant::__visit_value_at(
1727      __lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
1728}
1729
1730template <class... _Types>
1731_LIBCPP_HIDE_FROM_ABI
1732constexpr bool operator>=(const variant<_Types...>& __lhs,
1733                          const variant<_Types...>& __rhs) {
1734  using __variant_detail::__visitation::__variant;
1735  if (__rhs.valueless_by_exception()) return true;
1736  if (__lhs.valueless_by_exception()) return false;
1737  if (__lhs.index() > __rhs.index()) return true;
1738  if (__lhs.index() < __rhs.index()) return false;
1739  return __variant::__visit_value_at(
1740      __lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
1741}
1742
1743template <class... _Vs>
1744_LIBCPP_HIDE_FROM_ABI
1745_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1746constexpr void __throw_if_valueless(_Vs&&... __vs) {
1747  const bool __valueless =
1748      (... || std::__as_variant(__vs).valueless_by_exception());
1749  if (__valueless) {
1750    __throw_bad_variant_access();
1751  }
1752}
1753
1754template <
1755    class _Visitor, class... _Vs,
1756    typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...> >
1757_LIBCPP_HIDE_FROM_ABI
1758_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1759constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
1760  using __variant_detail::__visitation::__variant;
1761  std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1762  return __variant::__visit_value(std::forward<_Visitor>(__visitor),
1763                                  std::forward<_Vs>(__vs)...);
1764}
1765
1766#if _LIBCPP_STD_VER >= 20
1767template <
1768    class _Rp, class _Visitor, class... _Vs,
1769    typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...> >
1770_LIBCPP_HIDE_FROM_ABI
1771_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1772constexpr _Rp visit(_Visitor&& __visitor, _Vs&&... __vs) {
1773  using __variant_detail::__visitation::__variant;
1774  std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1775  return __variant::__visit_value<_Rp>(std::forward<_Visitor>(__visitor),
1776                                       std::forward<_Vs>(__vs)...);
1777}
1778#endif
1779
1780template <class... _Types>
1781_LIBCPP_HIDE_FROM_ABI
1782auto swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1783  noexcept(noexcept(__lhs.swap(__rhs)))
1784  -> decltype(      __lhs.swap(__rhs))
1785  { return          __lhs.swap(__rhs); }
1786
1787template <class... _Types>
1788struct _LIBCPP_TEMPLATE_VIS hash<
1789    __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
1790  using argument_type = variant<_Types...>;
1791  using result_type = size_t;
1792
1793  _LIBCPP_HIDE_FROM_ABI
1794  result_type operator()(const argument_type& __v) const {
1795    using __variant_detail::__visitation::__variant;
1796    size_t __res =
1797        __v.valueless_by_exception()
1798               ? 299792458 // Random value chosen by the universe upon creation
1799               : __variant::__visit_alt(
1800                     [](const auto& __alt) {
1801                       using __alt_type = __remove_cvref_t<decltype(__alt)>;
1802                       using __value_type = remove_const_t<
1803                         typename __alt_type::__value_type>;
1804                       return hash<__value_type>{}(__alt.__value);
1805                     },
1806                     __v);
1807    return std::__hash_combine(__res, hash<size_t>{}(__v.index()));
1808  }
1809};
1810
1811// __unchecked_get is the same as std::get, except, it is UB to use it with the wrong
1812// type whereas std::get will throw or returning nullptr. This makes it faster than
1813// std::get.
1814template <size_t _Ip, class _Vp>
1815_LIBCPP_HIDE_FROM_ABI
1816constexpr auto&& __unchecked_get(_Vp&& __v) noexcept {
1817  using __variant_detail::__access::__variant;
1818  return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
1819}
1820
1821template <class _Tp, class... _Types>
1822_LIBCPP_HIDE_FROM_ABI
1823constexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept {
1824  return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1825}
1826
1827template <class _Tp, class... _Types>
1828_LIBCPP_HIDE_FROM_ABI
1829constexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept {
1830  return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1831}
1832
1833#endif // _LIBCPP_STD_VER >= 17
1834
1835_LIBCPP_END_NAMESPACE_STD
1836
1837_LIBCPP_POP_MACROS
1838
1839#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1840#  include <exception>
1841#  include <type_traits>
1842#  include <typeinfo>
1843#  include <utility>
1844#endif
1845
1846#endif // _LIBCPP_VARIANT
1847