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