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