xref: /freebsd/contrib/llvm-project/libcxx/include/variant (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_VARIANT
11#define _LIBCPP_VARIANT
12
13/*
14   variant synopsis
15
16namespace std {
17
18  // 20.7.2, class template variant
19  template <class... Types>
20  class variant {
21  public:
22
23    // 20.7.2.1, constructors
24    constexpr variant() noexcept(see below);
25    variant(const variant&);                // constexpr in C++20
26    variant(variant&&) noexcept(see below); // constexpr in C++20
27
28    template <class T> constexpr variant(T&&) noexcept(see below);
29
30    template <class T, class... Args>
31    constexpr explicit variant(in_place_type_t<T>, Args&&...);
32
33    template <class T, class U, class... Args>
34    constexpr explicit variant(
35        in_place_type_t<T>, initializer_list<U>, Args&&...);
36
37    template <size_t I, class... Args>
38    constexpr explicit variant(in_place_index_t<I>, Args&&...);
39
40    template <size_t I, class U, class... Args>
41    constexpr explicit variant(
42        in_place_index_t<I>, initializer_list<U>, Args&&...);
43
44    // 20.7.2.2, destructor
45    ~variant();
46
47    // 20.7.2.3, assignment
48    variant& operator=(const variant&);                // constexpr in C++20
49    variant& operator=(variant&&) noexcept(see below); // constexpr in C++20
50
51    template <class T> variant& operator=(T&&) noexcept(see below);
52
53    // 20.7.2.4, modifiers
54    template <class T, class... Args>
55    T& emplace(Args&&...);
56
57    template <class T, class U, class... Args>
58    T& emplace(initializer_list<U>, Args&&...);
59
60    template <size_t I, class... Args>
61    variant_alternative_t<I, variant>& emplace(Args&&...);
62
63    template <size_t I, class U, class...  Args>
64    variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
65
66    // 20.7.2.5, value status
67    constexpr bool valueless_by_exception() const noexcept;
68    constexpr size_t index() const noexcept;
69
70    // 20.7.2.6, swap
71    void swap(variant&) noexcept(see below);
72  };
73
74  // 20.7.3, variant helper classes
75  template <class T> struct variant_size; // undefined
76
77  template <class T>
78  inline constexpr size_t variant_size_v = variant_size<T>::value;
79
80  template <class T> struct variant_size<const T>;
81  template <class T> struct variant_size<volatile T>;
82  template <class T> struct variant_size<const volatile T>;
83
84  template <class... Types>
85  struct variant_size<variant<Types...>>;
86
87  template <size_t I, class T> struct variant_alternative; // undefined
88
89  template <size_t I, class T>
90  using variant_alternative_t = typename variant_alternative<I, T>::type;
91
92  template <size_t I, class T> struct variant_alternative<I, const T>;
93  template <size_t I, class T> struct variant_alternative<I, volatile T>;
94  template <size_t I, class T> struct variant_alternative<I, const volatile T>;
95
96  template <size_t I, class... Types>
97  struct variant_alternative<I, variant<Types...>>;
98
99  inline constexpr size_t variant_npos = -1;
100
101  // 20.7.4, value access
102  template <class T, class... Types>
103  constexpr bool holds_alternative(const variant<Types...>&) noexcept;
104
105  template <size_t I, class... Types>
106  constexpr variant_alternative_t<I, variant<Types...>>&
107  get(variant<Types...>&);
108
109  template <size_t I, class... Types>
110  constexpr variant_alternative_t<I, variant<Types...>>&&
111  get(variant<Types...>&&);
112
113  template <size_t I, class... Types>
114  constexpr variant_alternative_t<I, variant<Types...>> const&
115  get(const variant<Types...>&);
116
117  template <size_t I, class... Types>
118  constexpr variant_alternative_t<I, variant<Types...>> const&&
119  get(const variant<Types...>&&);
120
121  template <class T, class...  Types>
122  constexpr T& get(variant<Types...>&);
123
124  template <class T, class... Types>
125  constexpr T&& get(variant<Types...>&&);
126
127  template <class T, class... Types>
128  constexpr const T& get(const variant<Types...>&);
129
130  template <class T, class... Types>
131  constexpr const T&& get(const variant<Types...>&&);
132
133  template <size_t I, class... Types>
134  constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
135  get_if(variant<Types...>*) noexcept;
136
137  template <size_t I, class... Types>
138  constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
139  get_if(const variant<Types...>*) noexcept;
140
141  template <class T, class... Types>
142  constexpr add_pointer_t<T>
143  get_if(variant<Types...>*) noexcept;
144
145  template <class T, class... Types>
146  constexpr add_pointer_t<const T>
147  get_if(const variant<Types...>*) noexcept;
148
149  // 20.7.5, relational operators
150  template <class... Types>
151  constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
152
153  template <class... Types>
154  constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
155
156  template <class... Types>
157  constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
158
159  template <class... Types>
160  constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
161
162  template <class... Types>
163  constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
164
165  template <class... Types>
166  constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
167
168  // 20.7.6, visitation
169  template <class Visitor, class... Variants>
170  constexpr see below visit(Visitor&&, Variants&&...);
171
172  template <class R, class Visitor, class... Variants>
173  constexpr R visit(Visitor&&, Variants&&...); // since C++20
174
175  // 20.7.7, class monostate
176  struct monostate;
177
178  // 20.7.8, monostate relational operators
179  constexpr bool operator<(monostate, monostate) noexcept;
180  constexpr bool operator>(monostate, monostate) noexcept;
181  constexpr bool operator<=(monostate, monostate) noexcept;
182  constexpr bool operator>=(monostate, monostate) noexcept;
183  constexpr bool operator==(monostate, monostate) noexcept;
184  constexpr bool operator!=(monostate, monostate) noexcept;
185
186  // 20.7.9, specialized algorithms
187  template <class... Types>
188  void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
189
190  // 20.7.10, class bad_variant_access
191  class bad_variant_access;
192
193  // 20.7.11, hash support
194  template <class T> struct hash;
195  template <class... Types> struct hash<variant<Types...>>;
196  template <> struct hash<monostate>;
197
198} // namespace std
199
200*/
201
202#include <__assert> // all public C++ headers provide the assertion handler
203#include <__availability>
204#include <__config>
205#include <__functional/hash.h>
206#include <__functional/operations.h>
207#include <__functional/unary_function.h>
208#include <__tuple>
209#include <__utility/forward.h>
210#include <__utility/in_place.h>
211#include <__utility/move.h>
212#include <__utility/swap.h>
213#include <__variant/monostate.h>
214#include <exception>
215#include <initializer_list>
216#include <limits>
217#include <new>
218#include <tuple>
219#include <type_traits>
220#include <version>
221
222#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
223#  include <typeinfo>
224#  include <utility>
225#endif
226
227// standard-mandated includes
228#include <compare>
229
230#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
231#  pragma GCC system_header
232#endif
233
234_LIBCPP_PUSH_MACROS
235#include <__undef_macros>
236
237namespace std { // explicitly not using versioning namespace
238
239class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
240public:
241  virtual const char* what() const _NOEXCEPT;
242};
243
244} // namespace std
245
246_LIBCPP_BEGIN_NAMESPACE_STD
247
248#if _LIBCPP_STD_VER > 14
249
250// Light N-dimensional array of function pointers. Used in place of std::array to avoid
251// adding a dependency.
252template<class _Tp, size_t _Size>
253struct __farray {
254  static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit");
255  _Tp __buf_[_Size] = {};
256
257  _LIBCPP_INLINE_VISIBILITY constexpr
258  const _Tp &operator[](size_t __n) const noexcept {
259      return __buf_[__n];
260  }
261};
262
263_LIBCPP_NORETURN
264inline _LIBCPP_INLINE_VISIBILITY
265_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
266void __throw_bad_variant_access() {
267#ifndef _LIBCPP_NO_EXCEPTIONS
268        throw bad_variant_access();
269#else
270        _VSTD::abort();
271#endif
272}
273
274template <class... _Types>
275class _LIBCPP_TEMPLATE_VIS variant;
276
277template <class _Tp>
278struct _LIBCPP_TEMPLATE_VIS variant_size;
279
280template <class _Tp>
281inline constexpr size_t variant_size_v = variant_size<_Tp>::value;
282
283template <class _Tp>
284struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
285
286template <class _Tp>
287struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
288
289template <class _Tp>
290struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
291    : variant_size<_Tp> {};
292
293template <class... _Types>
294struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
295    : integral_constant<size_t, sizeof...(_Types)> {};
296
297template <size_t _Ip, class _Tp>
298struct _LIBCPP_TEMPLATE_VIS variant_alternative;
299
300template <size_t _Ip, class _Tp>
301using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
302
303template <size_t _Ip, class _Tp>
304struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
305    : add_const<variant_alternative_t<_Ip, _Tp>> {};
306
307template <size_t _Ip, class _Tp>
308struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
309    : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
310
311template <size_t _Ip, class _Tp>
312struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
313    : add_cv<variant_alternative_t<_Ip, _Tp>> {};
314
315template <size_t _Ip, class... _Types>
316struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
317  static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
318  using type = __type_pack_element<_Ip, _Types...>;
319};
320
321inline constexpr size_t variant_npos = static_cast<size_t>(-1);
322
323constexpr int __choose_index_type(unsigned int __num_elem) {
324  if (__num_elem < numeric_limits<unsigned char>::max())
325    return 0;
326  if (__num_elem < numeric_limits<unsigned short>::max())
327    return 1;
328  return 2;
329}
330
331template <size_t _NumAlts>
332using __variant_index_t =
333#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
334  unsigned int;
335#else
336  std::tuple_element_t<
337      __choose_index_type(_NumAlts),
338      std::tuple<unsigned char, unsigned short, unsigned int>
339  >;
340#endif
341
342template <class _IndexType>
343constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
344
345template <class... _Types>
346class _LIBCPP_TEMPLATE_VIS variant;
347
348template <class... _Types>
349_LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&
350__as_variant(variant<_Types...>& __vs) noexcept {
351  return __vs;
352}
353
354template <class... _Types>
355_LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&
356__as_variant(const variant<_Types...>& __vs) noexcept {
357  return __vs;
358}
359
360template <class... _Types>
361_LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&&
362__as_variant(variant<_Types...>&& __vs) noexcept {
363  return _VSTD::move(__vs);
364}
365
366template <class... _Types>
367_LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&&
368__as_variant(const variant<_Types...>&& __vs) noexcept {
369  return _VSTD::move(__vs);
370}
371
372namespace __find_detail {
373
374template <class _Tp, class... _Types>
375inline _LIBCPP_INLINE_VISIBILITY
376constexpr size_t __find_index() {
377  constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
378  size_t __result = __not_found;
379  for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
380    if (__matches[__i]) {
381      if (__result != __not_found) {
382        return __ambiguous;
383      }
384      __result = __i;
385    }
386  }
387  return __result;
388}
389
390template <size_t _Index>
391struct __find_unambiguous_index_sfinae_impl
392    : integral_constant<size_t, _Index> {};
393
394template <>
395struct __find_unambiguous_index_sfinae_impl<__not_found> {};
396
397template <>
398struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
399
400template <class _Tp, class... _Types>
401struct __find_unambiguous_index_sfinae
402    : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
403
404} // namespace __find_detail
405
406namespace __variant_detail {
407
408struct __valueless_t {};
409
410enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
411
412template <typename _Tp,
413          template <typename> class _IsTriviallyAvailable,
414          template <typename> class _IsAvailable>
415constexpr _Trait __trait =
416    _IsTriviallyAvailable<_Tp>::value
417        ? _Trait::_TriviallyAvailable
418        : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
419
420inline _LIBCPP_INLINE_VISIBILITY
421constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
422  _Trait __result = _Trait::_TriviallyAvailable;
423  for (_Trait __t : __traits) {
424    if (static_cast<int>(__t) > static_cast<int>(__result)) {
425      __result = __t;
426    }
427  }
428  return __result;
429}
430
431template <typename... _Types>
432struct __traits {
433  static constexpr _Trait __copy_constructible_trait =
434      __common_trait({__trait<_Types,
435                              is_trivially_copy_constructible,
436                              is_copy_constructible>...});
437
438  static constexpr _Trait __move_constructible_trait =
439      __common_trait({__trait<_Types,
440                              is_trivially_move_constructible,
441                              is_move_constructible>...});
442
443  static constexpr _Trait __copy_assignable_trait = __common_trait(
444      {__copy_constructible_trait,
445       __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
446
447  static constexpr _Trait __move_assignable_trait = __common_trait(
448      {__move_constructible_trait,
449       __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
450
451  static constexpr _Trait __destructible_trait = __common_trait(
452      {__trait<_Types, is_trivially_destructible, is_destructible>...});
453};
454
455namespace __access {
456
457struct __union {
458  template <class _Vp>
459  inline _LIBCPP_INLINE_VISIBILITY
460  static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
461    return _VSTD::forward<_Vp>(__v).__head;
462  }
463
464  template <class _Vp, size_t _Ip>
465  inline _LIBCPP_INLINE_VISIBILITY
466  static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
467    return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
468  }
469};
470
471struct __base {
472  template <size_t _Ip, class _Vp>
473  inline _LIBCPP_INLINE_VISIBILITY
474  static constexpr auto&& __get_alt(_Vp&& __v) {
475    return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
476                              in_place_index<_Ip>);
477  }
478};
479
480struct __variant {
481  template <size_t _Ip, class _Vp>
482  inline _LIBCPP_INLINE_VISIBILITY
483  static constexpr auto&& __get_alt(_Vp&& __v) {
484    return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
485  }
486};
487
488} // namespace __access
489
490namespace __visitation {
491
492struct __base {
493  template <class _Visitor, class... _Vs>
494  inline _LIBCPP_INLINE_VISIBILITY
495  static constexpr decltype(auto)
496  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
497    constexpr auto __fdiagonal =
498        __make_fdiagonal<_Visitor&&,
499                         decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
500    return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
501                                _VSTD::forward<_Vs>(__vs).__as_base()...);
502  }
503
504  template <class _Visitor, class... _Vs>
505  inline _LIBCPP_INLINE_VISIBILITY
506  static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
507                                              _Vs&&... __vs) {
508    constexpr auto __fmatrix =
509        __make_fmatrix<_Visitor&&,
510                       decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
511    return __at(__fmatrix, __vs.index()...)(
512        _VSTD::forward<_Visitor>(__visitor),
513        _VSTD::forward<_Vs>(__vs).__as_base()...);
514  }
515
516private:
517  template <class _Tp>
518  inline _LIBCPP_INLINE_VISIBILITY
519  static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
520
521  template <class _Tp, size_t _Np, typename... _Indices>
522  inline _LIBCPP_INLINE_VISIBILITY
523  static constexpr auto&& __at(const __farray<_Tp, _Np>& __elems,
524                               size_t __index, _Indices... __indices) {
525    return __at(__elems[__index], __indices...);
526  }
527
528  template <class _Fp, class... _Fs>
529  static constexpr void __std_visit_visitor_return_type_check() {
530    static_assert(
531        __all<is_same_v<_Fp, _Fs>...>::value,
532        "`std::visit` requires the visitor to have a single return type.");
533  }
534
535  template <class... _Fs>
536  inline _LIBCPP_INLINE_VISIBILITY
537  static constexpr auto __make_farray(_Fs&&... __fs) {
538    __std_visit_visitor_return_type_check<__uncvref_t<_Fs>...>();
539    using __result = __farray<common_type_t<__uncvref_t<_Fs>...>, sizeof...(_Fs)>;
540    return __result{{_VSTD::forward<_Fs>(__fs)...}};
541  }
542
543  template <size_t... _Is>
544  struct __dispatcher {
545    template <class _Fp, class... _Vs>
546    inline _LIBCPP_INLINE_VISIBILITY
547    static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
548        return _VSTD::__invoke(
549            static_cast<_Fp>(__f),
550            __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
551    }
552  };
553
554  template <class _Fp, class... _Vs, size_t... _Is>
555  inline _LIBCPP_INLINE_VISIBILITY
556  static constexpr auto __make_dispatch(index_sequence<_Is...>) {
557    return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
558  }
559
560  template <size_t _Ip, class _Fp, class... _Vs>
561  inline _LIBCPP_INLINE_VISIBILITY
562  static constexpr auto __make_fdiagonal_impl() {
563    return __make_dispatch<_Fp, _Vs...>(
564        index_sequence<((void)__type_identity<_Vs>{}, _Ip)...>{});
565  }
566
567  template <class _Fp, class... _Vs, size_t... _Is>
568  inline _LIBCPP_INLINE_VISIBILITY
569  static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
570    return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
571  }
572
573  template <class _Fp, class _Vp, class... _Vs>
574  inline _LIBCPP_INLINE_VISIBILITY
575  static constexpr auto __make_fdiagonal() {
576    constexpr size_t _Np = __uncvref_t<_Vp>::__size();
577    static_assert(__all<(_Np == __uncvref_t<_Vs>::__size())...>::value);
578    return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
579  }
580
581  template <class _Fp, class... _Vs, size_t... _Is>
582  inline _LIBCPP_INLINE_VISIBILITY
583  static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
584    return __make_dispatch<_Fp, _Vs...>(__is);
585  }
586
587  template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
588  inline _LIBCPP_INLINE_VISIBILITY
589  static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
590                                            index_sequence<_Js...>,
591                                            _Ls... __ls) {
592    return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
593        index_sequence<_Is..., _Js>{}, __ls...)...);
594  }
595
596  template <class _Fp, class... _Vs>
597  inline _LIBCPP_INLINE_VISIBILITY
598  static constexpr auto __make_fmatrix() {
599    return __make_fmatrix_impl<_Fp, _Vs...>(
600        index_sequence<>{}, make_index_sequence<__uncvref_t<_Vs>::__size()>{}...);
601  }
602};
603
604struct __variant {
605  template <class _Visitor, class... _Vs>
606  inline _LIBCPP_INLINE_VISIBILITY
607  static constexpr decltype(auto)
608  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
609    return __base::__visit_alt_at(__index,
610                                  _VSTD::forward<_Visitor>(__visitor),
611                                  _VSTD::forward<_Vs>(__vs).__impl...);
612  }
613
614  template <class _Visitor, class... _Vs>
615  inline _LIBCPP_INLINE_VISIBILITY
616  static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
617                                              _Vs&&... __vs) {
618    return __base::__visit_alt(
619        _VSTD::forward<_Visitor>(__visitor),
620        _VSTD::__as_variant(_VSTD::forward<_Vs>(__vs)).__impl...);
621  }
622
623  template <class _Visitor, class... _Vs>
624  inline _LIBCPP_INLINE_VISIBILITY
625  static constexpr decltype(auto)
626  __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
627    return __visit_alt_at(
628        __index,
629        __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
630        _VSTD::forward<_Vs>(__vs)...);
631  }
632
633  template <class _Visitor, class... _Vs>
634  inline _LIBCPP_INLINE_VISIBILITY
635  static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
636                                                _Vs&&... __vs) {
637    return __visit_alt(
638        __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
639        _VSTD::forward<_Vs>(__vs)...);
640  }
641
642#if _LIBCPP_STD_VER > 17
643  template <class _Rp, class _Visitor, class... _Vs>
644  inline _LIBCPP_INLINE_VISIBILITY
645  static constexpr _Rp __visit_value(_Visitor&& __visitor,
646                                     _Vs&&... __vs) {
647    return __visit_alt(
648        __make_value_visitor<_Rp>(_VSTD::forward<_Visitor>(__visitor)),
649        _VSTD::forward<_Vs>(__vs)...);
650  }
651#endif
652
653private:
654  template <class _Visitor, class... _Values>
655  static constexpr void __std_visit_exhaustive_visitor_check() {
656    static_assert(is_invocable_v<_Visitor, _Values...>,
657                  "`std::visit` requires the visitor to be exhaustive.");
658  }
659
660  template <class _Visitor>
661  struct __value_visitor {
662    template <class... _Alts>
663    inline _LIBCPP_INLINE_VISIBILITY
664    constexpr decltype(auto) operator()(_Alts&&... __alts) const {
665      __std_visit_exhaustive_visitor_check<
666          _Visitor,
667          decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
668      return _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
669                             _VSTD::forward<_Alts>(__alts).__value...);
670    }
671    _Visitor&& __visitor;
672  };
673
674#if _LIBCPP_STD_VER > 17
675  template <class _Rp, class _Visitor>
676  struct __value_visitor_return_type {
677    template <class... _Alts>
678    inline _LIBCPP_INLINE_VISIBILITY
679    constexpr _Rp operator()(_Alts&&... __alts) const {
680      __std_visit_exhaustive_visitor_check<
681          _Visitor,
682          decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
683      if constexpr (is_void_v<_Rp>) {
684        _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
685                        _VSTD::forward<_Alts>(__alts).__value...);
686      }
687      else {
688        return _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
689                               _VSTD::forward<_Alts>(__alts).__value...);
690      }
691    }
692
693    _Visitor&& __visitor;
694  };
695#endif
696
697  template <class _Visitor>
698  inline _LIBCPP_INLINE_VISIBILITY
699  static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
700    return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
701  }
702
703#if _LIBCPP_STD_VER > 17
704  template <class _Rp, class _Visitor>
705  inline _LIBCPP_INLINE_VISIBILITY
706  static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
707    return __value_visitor_return_type<_Rp, _Visitor>{_VSTD::forward<_Visitor>(__visitor)};
708  }
709#endif
710};
711
712} // namespace __visitation
713
714template <size_t _Index, class _Tp>
715struct _LIBCPP_TEMPLATE_VIS __alt {
716  using __value_type = _Tp;
717
718  template <class... _Args>
719  inline _LIBCPP_INLINE_VISIBILITY
720  explicit constexpr __alt(in_place_t, _Args&&... __args)
721      : __value(_VSTD::forward<_Args>(__args)...) {}
722
723  __value_type __value;
724};
725
726template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
727union _LIBCPP_TEMPLATE_VIS __union;
728
729template <_Trait _DestructibleTrait, size_t _Index>
730union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
731
732#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor)                  \
733  template <size_t _Index, class _Tp, class... _Types>                         \
734  union _LIBCPP_TEMPLATE_VIS __union<destructible_trait,                      \
735                                      _Index,                                  \
736                                      _Tp,                                     \
737                                      _Types...> {                             \
738  public:                                                                      \
739    inline _LIBCPP_INLINE_VISIBILITY                                           \
740    explicit constexpr __union(__valueless_t) noexcept : __dummy{} {}          \
741                                                                               \
742    template <class... _Args>                                                  \
743    inline _LIBCPP_INLINE_VISIBILITY                                           \
744    explicit constexpr __union(in_place_index_t<0>, _Args&&... __args)         \
745        : __head(in_place, _VSTD::forward<_Args>(__args)...) {}                \
746                                                                               \
747    template <size_t _Ip, class... _Args>                                      \
748    inline _LIBCPP_INLINE_VISIBILITY                                           \
749    explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args)       \
750        : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
751                                                                               \
752    __union(const __union&) = default;                                         \
753    __union(__union&&) = default;                                              \
754                                                                               \
755    destructor                                                                 \
756                                                                               \
757    __union& operator=(const __union&) = default;                              \
758    __union& operator=(__union&&) = default;                                   \
759                                                                               \
760  private:                                                                     \
761    char __dummy;                                                              \
762    __alt<_Index, _Tp> __head;                                                 \
763    __union<destructible_trait, _Index + 1, _Types...> __tail;                 \
764                                                                               \
765    friend struct __access::__union;                                           \
766  }
767
768_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
769_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
770_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
771
772#undef _LIBCPP_VARIANT_UNION
773
774template <_Trait _DestructibleTrait, class... _Types>
775class _LIBCPP_TEMPLATE_VIS __base {
776public:
777  using __index_t = __variant_index_t<sizeof...(_Types)>;
778
779  inline _LIBCPP_INLINE_VISIBILITY
780  explicit constexpr __base(__valueless_t __tag) noexcept
781      : __data(__tag), __index(__variant_npos<__index_t>) {}
782
783  template <size_t _Ip, class... _Args>
784  inline _LIBCPP_INLINE_VISIBILITY
785  explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
786      :
787        __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
788        __index(_Ip) {}
789
790  inline _LIBCPP_INLINE_VISIBILITY
791  constexpr bool valueless_by_exception() const noexcept {
792    return index() == variant_npos;
793  }
794
795  inline _LIBCPP_INLINE_VISIBILITY
796  constexpr size_t index() const noexcept {
797    return __index == __variant_npos<__index_t> ? variant_npos : __index;
798  }
799
800protected:
801  inline _LIBCPP_INLINE_VISIBILITY
802  constexpr auto&& __as_base() & { return *this; }
803
804  inline _LIBCPP_INLINE_VISIBILITY
805  constexpr auto&& __as_base() && { return _VSTD::move(*this); }
806
807  inline _LIBCPP_INLINE_VISIBILITY
808  constexpr auto&& __as_base() const & { return *this; }
809
810  inline _LIBCPP_INLINE_VISIBILITY
811  constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
812
813  inline _LIBCPP_INLINE_VISIBILITY
814  static constexpr size_t __size() { return sizeof...(_Types); }
815
816  __union<_DestructibleTrait, 0, _Types...> __data;
817  __index_t __index;
818
819  friend struct __access::__base;
820  friend struct __visitation::__base;
821};
822
823template <class _Traits, _Trait = _Traits::__destructible_trait>
824class _LIBCPP_TEMPLATE_VIS __dtor;
825
826#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy)    \
827  template <class... _Types>                                                   \
828  class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>,                       \
829                                    destructible_trait>                        \
830      : public __base<destructible_trait, _Types...> {                         \
831    using __base_type = __base<destructible_trait, _Types...>;                 \
832    using __index_t = typename __base_type::__index_t;                         \
833                                                                               \
834  public:                                                                      \
835    using __base_type::__base_type;                                            \
836    using __base_type::operator=;                                              \
837                                                                               \
838    __dtor(const __dtor&) = default;                                           \
839    __dtor(__dtor&&) = default;                                                \
840    destructor                                                                 \
841    __dtor& operator=(const __dtor&) = default;                                \
842    __dtor& operator=(__dtor&&) = default;                                     \
843                                                                               \
844  protected:                                                                   \
845    inline _LIBCPP_INLINE_VISIBILITY                                           \
846    destroy                                                                    \
847  }
848
849_LIBCPP_VARIANT_DESTRUCTOR(
850    _Trait::_TriviallyAvailable,
851    ~__dtor() = default;,
852    void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
853
854_LIBCPP_VARIANT_DESTRUCTOR(
855    _Trait::_Available,
856    ~__dtor() { __destroy(); },
857    void __destroy() noexcept {
858      if (!this->valueless_by_exception()) {
859        __visitation::__base::__visit_alt(
860            [](auto& __alt) noexcept {
861              using __alt_type = __uncvref_t<decltype(__alt)>;
862              __alt.~__alt_type();
863            },
864            *this);
865      }
866      this->__index = __variant_npos<__index_t>;
867    });
868
869_LIBCPP_VARIANT_DESTRUCTOR(
870    _Trait::_Unavailable,
871    ~__dtor() = delete;,
872    void __destroy() noexcept = delete;);
873
874#undef _LIBCPP_VARIANT_DESTRUCTOR
875
876template <class _Traits>
877class _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
878  using __base_type = __dtor<_Traits>;
879
880public:
881  using __base_type::__base_type;
882  using __base_type::operator=;
883
884protected:
885  template <size_t _Ip, class _Tp, class... _Args>
886  inline _LIBCPP_INLINE_VISIBILITY
887  static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
888    ::new ((void*)_VSTD::addressof(__a))
889        __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
890    return __a.__value;
891  }
892
893  template <class _Rhs>
894  inline _LIBCPP_INLINE_VISIBILITY
895  static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
896    __lhs.__destroy();
897    if (!__rhs.valueless_by_exception()) {
898      __visitation::__base::__visit_alt_at(
899          __rhs.index(),
900          [](auto& __lhs_alt, auto&& __rhs_alt) {
901            __construct_alt(
902                __lhs_alt,
903                _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
904          },
905          __lhs, _VSTD::forward<_Rhs>(__rhs));
906      __lhs.__index = __rhs.index();
907    }
908  }
909};
910
911template <class _Traits, _Trait = _Traits::__move_constructible_trait>
912class _LIBCPP_TEMPLATE_VIS __move_constructor;
913
914#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait,             \
915                                         move_constructor)                     \
916  template <class... _Types>                                                   \
917  class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>,           \
918                                                move_constructible_trait>      \
919      : public __ctor<__traits<_Types...>> {                                   \
920    using __base_type = __ctor<__traits<_Types...>>;                           \
921                                                                               \
922  public:                                                                      \
923    using __base_type::__base_type;                                            \
924    using __base_type::operator=;                                              \
925                                                                               \
926    __move_constructor(const __move_constructor&) = default;                   \
927    move_constructor                                                           \
928    ~__move_constructor() = default;                                           \
929    __move_constructor& operator=(const __move_constructor&) = default;        \
930    __move_constructor& operator=(__move_constructor&&) = default;             \
931  }
932
933_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
934    _Trait::_TriviallyAvailable,
935    __move_constructor(__move_constructor&& __that) = default;);
936
937_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
938    _Trait::_Available,
939    __move_constructor(__move_constructor&& __that) noexcept(
940        __all<is_nothrow_move_constructible_v<_Types>...>::value)
941        : __move_constructor(__valueless_t{}) {
942      this->__generic_construct(*this, _VSTD::move(__that));
943    });
944
945_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
946    _Trait::_Unavailable,
947    __move_constructor(__move_constructor&&) = delete;);
948
949#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
950
951template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
952class _LIBCPP_TEMPLATE_VIS __copy_constructor;
953
954#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait,             \
955                                         copy_constructor)                     \
956  template <class... _Types>                                                   \
957  class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>,          \
958                                                 copy_constructible_trait>     \
959      : public __move_constructor<__traits<_Types...>> {                       \
960    using __base_type = __move_constructor<__traits<_Types...>>;               \
961                                                                               \
962  public:                                                                      \
963    using __base_type::__base_type;                                            \
964    using __base_type::operator=;                                              \
965                                                                               \
966    copy_constructor                                                           \
967    __copy_constructor(__copy_constructor&&) = default;                        \
968    ~__copy_constructor() = default;                                           \
969    __copy_constructor& operator=(const __copy_constructor&) = default;        \
970    __copy_constructor& operator=(__copy_constructor&&) = default;             \
971  }
972
973_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
974    _Trait::_TriviallyAvailable,
975    __copy_constructor(const __copy_constructor& __that) = default;);
976
977_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
978    _Trait::_Available,
979    __copy_constructor(const __copy_constructor& __that)
980        : __copy_constructor(__valueless_t{}) {
981      this->__generic_construct(*this, __that);
982    });
983
984_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
985    _Trait::_Unavailable,
986    __copy_constructor(const __copy_constructor&) = delete;);
987
988#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
989
990template <class _Traits>
991class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
992  using __base_type = __copy_constructor<_Traits>;
993
994public:
995  using __base_type::__base_type;
996  using __base_type::operator=;
997
998  template <size_t _Ip, class... _Args>
999  inline _LIBCPP_INLINE_VISIBILITY
1000  auto& __emplace(_Args&&... __args) {
1001    this->__destroy();
1002    auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
1003                          _VSTD::forward<_Args>(__args)...);
1004    this->__index = _Ip;
1005    return __res;
1006  }
1007
1008protected:
1009  template <size_t _Ip, class _Tp, class _Arg>
1010  inline _LIBCPP_INLINE_VISIBILITY
1011  void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
1012    if (this->index() == _Ip) {
1013      __a.__value = _VSTD::forward<_Arg>(__arg);
1014    } else {
1015      struct {
1016        void operator()(true_type) const {
1017          __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
1018        }
1019        void operator()(false_type) const {
1020          __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
1021        }
1022        __assignment* __this;
1023        _Arg&& __arg;
1024      } __impl{this, _VSTD::forward<_Arg>(__arg)};
1025      __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
1026                           !is_nothrow_move_constructible_v<_Tp>>{});
1027    }
1028  }
1029
1030  template <class _That>
1031  inline _LIBCPP_INLINE_VISIBILITY
1032  void __generic_assign(_That&& __that) {
1033    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1034      // do nothing.
1035    } else if (__that.valueless_by_exception()) {
1036      this->__destroy();
1037    } else {
1038      __visitation::__base::__visit_alt_at(
1039          __that.index(),
1040          [this](auto& __this_alt, auto&& __that_alt) {
1041            this->__assign_alt(
1042                __this_alt,
1043                _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
1044          },
1045          *this, _VSTD::forward<_That>(__that));
1046    }
1047  }
1048};
1049
1050template <class _Traits, _Trait = _Traits::__move_assignable_trait>
1051class _LIBCPP_TEMPLATE_VIS __move_assignment;
1052
1053#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait,                 \
1054                                        move_assignment)                       \
1055  template <class... _Types>                                                   \
1056  class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>,           \
1057                                                move_assignable_trait>         \
1058      : public __assignment<__traits<_Types...>> {                             \
1059    using __base_type = __assignment<__traits<_Types...>>;                     \
1060                                                                               \
1061  public:                                                                      \
1062    using __base_type::__base_type;                                            \
1063    using __base_type::operator=;                                              \
1064                                                                               \
1065    __move_assignment(const __move_assignment&) = default;                     \
1066    __move_assignment(__move_assignment&&) = default;                          \
1067    ~__move_assignment() = default;                                            \
1068    __move_assignment& operator=(const __move_assignment&) = default;          \
1069    move_assignment                                                            \
1070  }
1071
1072_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
1073    _Trait::_TriviallyAvailable,
1074    __move_assignment& operator=(__move_assignment&& __that) = default;);
1075
1076_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
1077    _Trait::_Available,
1078    __move_assignment& operator=(__move_assignment&& __that) noexcept(
1079        __all<(is_nothrow_move_constructible_v<_Types> &&
1080               is_nothrow_move_assignable_v<_Types>)...>::value) {
1081      this->__generic_assign(_VSTD::move(__that));
1082      return *this;
1083    });
1084
1085_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
1086    _Trait::_Unavailable,
1087    __move_assignment& operator=(__move_assignment&&) = delete;);
1088
1089#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
1090
1091template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
1092class _LIBCPP_TEMPLATE_VIS __copy_assignment;
1093
1094#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait,                 \
1095                                        copy_assignment)                       \
1096  template <class... _Types>                                                   \
1097  class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>,           \
1098                                                copy_assignable_trait>         \
1099      : public __move_assignment<__traits<_Types...>> {                        \
1100    using __base_type = __move_assignment<__traits<_Types...>>;                \
1101                                                                               \
1102  public:                                                                      \
1103    using __base_type::__base_type;                                            \
1104    using __base_type::operator=;                                              \
1105                                                                               \
1106    __copy_assignment(const __copy_assignment&) = default;                     \
1107    __copy_assignment(__copy_assignment&&) = default;                          \
1108    ~__copy_assignment() = default;                                            \
1109    copy_assignment                                                            \
1110    __copy_assignment& operator=(__copy_assignment&&) = default;               \
1111  }
1112
1113_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1114    _Trait::_TriviallyAvailable,
1115    __copy_assignment& operator=(const __copy_assignment& __that) = default;);
1116
1117_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1118    _Trait::_Available,
1119    __copy_assignment& operator=(const __copy_assignment& __that) {
1120      this->__generic_assign(__that);
1121      return *this;
1122    });
1123
1124_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1125    _Trait::_Unavailable,
1126    __copy_assignment& operator=(const __copy_assignment&) = delete;);
1127
1128#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1129
1130template <class... _Types>
1131class _LIBCPP_TEMPLATE_VIS __impl
1132    : public __copy_assignment<__traits<_Types...>> {
1133  using __base_type = __copy_assignment<__traits<_Types...>>;
1134
1135public:
1136  using __base_type::__base_type; // get in_place_index_t constructor & friends
1137  __impl(__impl const&) = default;
1138  __impl(__impl&&) = default;
1139  __impl& operator=(__impl const&) = default;
1140  __impl& operator=(__impl&&) = default;
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 = __type_identity<_Dest>;
1205};
1206
1207struct __narrowing_check {
1208  template <class _Dest>
1209  static auto __test_impl(_Dest (&&)[1]) -> __type_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>, __type_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