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