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