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