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