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