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