• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// -*- C++ -*-
2//===------------------------------ variant -------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_VARIANT
12#define _LIBCPP_VARIANT
13
14/*
15   variant synopsis
16
17namespace std {
18
19  // 20.7.2, class template variant
20  template <class... Types>
21  class variant {
22  public:
23
24    // 20.7.2.1, constructors
25    constexpr variant() noexcept(see below);
26    variant(const variant&);                // constexpr in C++20
27    variant(variant&&) noexcept(see below); // constexpr in C++20
28
29    template <class T> constexpr variant(T&&) noexcept(see below);
30
31    template <class T, class... Args>
32    constexpr explicit variant(in_place_type_t<T>, Args&&...);
33
34    template <class T, class U, class... Args>
35    constexpr explicit variant(
36        in_place_type_t<T>, initializer_list<U>, Args&&...);
37
38    template <size_t I, class... Args>
39    constexpr explicit variant(in_place_index_t<I>, Args&&...);
40
41    template <size_t I, class U, class... Args>
42    constexpr explicit variant(
43        in_place_index_t<I>, initializer_list<U>, Args&&...);
44
45    // 20.7.2.2, destructor
46    ~variant();
47
48    // 20.7.2.3, assignment
49    variant& operator=(const variant&);                // constexpr in C++20
50    variant& operator=(variant&&) noexcept(see below); // constexpr in C++20
51
52    template <class T> variant& operator=(T&&) noexcept(see below);
53
54    // 20.7.2.4, modifiers
55    template <class T, class... Args>
56    T& emplace(Args&&...);
57
58    template <class T, class U, class... Args>
59    T& emplace(initializer_list<U>, Args&&...);
60
61    template <size_t I, class... Args>
62    variant_alternative_t<I, variant>& emplace(Args&&...);
63
64    template <size_t I, class U, class...  Args>
65    variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
66
67    // 20.7.2.5, value status
68    constexpr bool valueless_by_exception() const noexcept;
69    constexpr size_t index() const noexcept;
70
71    // 20.7.2.6, swap
72    void swap(variant&) noexcept(see below);
73  };
74
75  // 20.7.3, variant helper classes
76  template <class T> struct variant_size; // undefined
77
78  template <class T>
79  inline constexpr size_t variant_size_v = variant_size<T>::value;
80
81  template <class T> struct variant_size<const T>;
82  template <class T> struct variant_size<volatile T>;
83  template <class T> struct variant_size<const volatile T>;
84
85  template <class... Types>
86  struct variant_size<variant<Types...>>;
87
88  template <size_t I, class T> struct variant_alternative; // undefined
89
90  template <size_t I, class T>
91  using variant_alternative_t = typename variant_alternative<I, T>::type;
92
93  template <size_t I, class T> struct variant_alternative<I, const T>;
94  template <size_t I, class T> struct variant_alternative<I, volatile T>;
95  template <size_t I, class T> struct variant_alternative<I, const volatile T>;
96
97  template <size_t I, class... Types>
98  struct variant_alternative<I, variant<Types...>>;
99
100  inline constexpr size_t variant_npos = -1;
101
102  // 20.7.4, value access
103  template <class T, class... Types>
104  constexpr bool holds_alternative(const variant<Types...>&) noexcept;
105
106  template <size_t I, class... Types>
107  constexpr variant_alternative_t<I, variant<Types...>>&
108  get(variant<Types...>&);
109
110  template <size_t I, class... Types>
111  constexpr variant_alternative_t<I, variant<Types...>>&&
112  get(variant<Types...>&&);
113
114  template <size_t I, class... Types>
115  constexpr variant_alternative_t<I, variant<Types...>> const&
116  get(const variant<Types...>&);
117
118  template <size_t I, class... Types>
119  constexpr variant_alternative_t<I, variant<Types...>> const&&
120  get(const variant<Types...>&&);
121
122  template <class T, class...  Types>
123  constexpr T& get(variant<Types...>&);
124
125  template <class T, class... Types>
126  constexpr T&& get(variant<Types...>&&);
127
128  template <class T, class... Types>
129  constexpr const T& get(const variant<Types...>&);
130
131  template <class T, class... Types>
132  constexpr const T&& get(const variant<Types...>&&);
133
134  template <size_t I, class... Types>
135  constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
136  get_if(variant<Types...>*) noexcept;
137
138  template <size_t I, class... Types>
139  constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
140  get_if(const variant<Types...>*) noexcept;
141
142  template <class T, class... Types>
143  constexpr add_pointer_t<T>
144  get_if(variant<Types...>*) noexcept;
145
146  template <class T, class... Types>
147  constexpr add_pointer_t<const T>
148  get_if(const variant<Types...>*) noexcept;
149
150  // 20.7.5, relational operators
151  template <class... Types>
152  constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
153
154  template <class... Types>
155  constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
156
157  template <class... Types>
158  constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
159
160  template <class... Types>
161  constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
162
163  template <class... Types>
164  constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
165
166  template <class... Types>
167  constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
168
169  // 20.7.6, visitation
170  template <class Visitor, class... Variants>
171  constexpr see below visit(Visitor&&, Variants&&...);
172
173  // 20.7.7, class monostate
174  struct monostate;
175
176  // 20.7.8, monostate relational operators
177  constexpr bool operator<(monostate, monostate) noexcept;
178  constexpr bool operator>(monostate, monostate) noexcept;
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
184  // 20.7.9, specialized algorithms
185  template <class... Types>
186  void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
187
188  // 20.7.10, class bad_variant_access
189  class bad_variant_access;
190
191  // 20.7.11, hash support
192  template <class T> struct hash;
193  template <class... Types> struct hash<variant<Types...>>;
194  template <> struct hash<monostate>;
195
196} // namespace std
197
198*/
199
200#include <__config>
201#include <__tuple>
202#include <array>
203#include <exception>
204#include <functional>
205#include <initializer_list>
206#include <new>
207#include <tuple>
208#include <type_traits>
209#include <utility>
210#include <limits>
211#include <version>
212
213#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
214#pragma GCC system_header
215#endif
216
217_LIBCPP_PUSH_MACROS
218#include <__undef_macros>
219
220namespace std { // explicitly not using versioning namespace
221
222class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
223public:
224  virtual const char* what() const _NOEXCEPT;
225};
226
227} // namespace std
228
229_LIBCPP_BEGIN_NAMESPACE_STD
230
231#if _LIBCPP_STD_VER > 14
232
233_LIBCPP_NORETURN
234inline _LIBCPP_INLINE_VISIBILITY
235_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
236void __throw_bad_variant_access() {
237#ifndef _LIBCPP_NO_EXCEPTIONS
238        throw bad_variant_access();
239#else
240        _VSTD::abort();
241#endif
242}
243
244template <class... _Types>
245class _LIBCPP_TEMPLATE_VIS variant;
246
247template <class _Tp>
248struct _LIBCPP_TEMPLATE_VIS variant_size;
249
250template <class _Tp>
251_LIBCPP_INLINE_VAR constexpr size_t variant_size_v = variant_size<_Tp>::value;
252
253template <class _Tp>
254struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
255
256template <class _Tp>
257struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
258
259template <class _Tp>
260struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
261    : variant_size<_Tp> {};
262
263template <class... _Types>
264struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
265    : integral_constant<size_t, sizeof...(_Types)> {};
266
267template <size_t _Ip, class _Tp>
268struct _LIBCPP_TEMPLATE_VIS variant_alternative;
269
270template <size_t _Ip, class _Tp>
271using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
272
273template <size_t _Ip, class _Tp>
274struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
275    : add_const<variant_alternative_t<_Ip, _Tp>> {};
276
277template <size_t _Ip, class _Tp>
278struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
279    : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
280
281template <size_t _Ip, class _Tp>
282struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
283    : add_cv<variant_alternative_t<_Ip, _Tp>> {};
284
285template <size_t _Ip, class... _Types>
286struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
287  static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
288  using type = __type_pack_element<_Ip, _Types...>;
289};
290
291_LIBCPP_INLINE_VAR constexpr size_t variant_npos = static_cast<size_t>(-1);
292
293constexpr int __choose_index_type(unsigned int __num_elem) {
294  if (__num_elem < std::numeric_limits<unsigned char>::max())
295    return 0;
296  if (__num_elem < std::numeric_limits<unsigned short>::max())
297    return 1;
298  return 2;
299}
300
301template <size_t _NumAlts>
302using __variant_index_t =
303#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
304  unsigned int;
305#else
306  std::tuple_element_t<
307      __choose_index_type(_NumAlts),
308      std::tuple<unsigned char, unsigned short, unsigned int>
309  >;
310#endif
311
312template <class _IndexType>
313constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
314
315namespace __find_detail {
316
317template <class _Tp, class... _Types>
318inline _LIBCPP_INLINE_VISIBILITY
319constexpr size_t __find_index() {
320  constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
321  size_t __result = __not_found;
322  for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
323    if (__matches[__i]) {
324      if (__result != __not_found) {
325        return __ambiguous;
326      }
327      __result = __i;
328    }
329  }
330  return __result;
331}
332
333template <size_t _Index>
334struct __find_unambiguous_index_sfinae_impl
335    : integral_constant<size_t, _Index> {};
336
337template <>
338struct __find_unambiguous_index_sfinae_impl<__not_found> {};
339
340template <>
341struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
342
343template <class _Tp, class... _Types>
344struct __find_unambiguous_index_sfinae
345    : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
346
347} // namespace __find_detail
348
349namespace __variant_detail {
350
351struct __valueless_t {};
352
353enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
354
355template <typename _Tp,
356          template <typename> class _IsTriviallyAvailable,
357          template <typename> class _IsAvailable>
358constexpr _Trait __trait =
359    _IsTriviallyAvailable<_Tp>::value
360        ? _Trait::_TriviallyAvailable
361        : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
362
363inline _LIBCPP_INLINE_VISIBILITY
364constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
365  _Trait __result = _Trait::_TriviallyAvailable;
366  for (_Trait __t : __traits) {
367    if (static_cast<int>(__t) > static_cast<int>(__result)) {
368      __result = __t;
369    }
370  }
371  return __result;
372}
373
374template <typename... _Types>
375struct __traits {
376  static constexpr _Trait __copy_constructible_trait =
377      __common_trait({__trait<_Types,
378                              is_trivially_copy_constructible,
379                              is_copy_constructible>...});
380
381  static constexpr _Trait __move_constructible_trait =
382      __common_trait({__trait<_Types,
383                              is_trivially_move_constructible,
384                              is_move_constructible>...});
385
386  static constexpr _Trait __copy_assignable_trait = __common_trait(
387      {__copy_constructible_trait,
388       __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
389
390  static constexpr _Trait __move_assignable_trait = __common_trait(
391      {__move_constructible_trait,
392       __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
393
394  static constexpr _Trait __destructible_trait = __common_trait(
395      {__trait<_Types, is_trivially_destructible, is_destructible>...});
396};
397
398namespace __access {
399
400struct __union {
401  template <class _Vp>
402  inline _LIBCPP_INLINE_VISIBILITY
403  static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
404    return _VSTD::forward<_Vp>(__v).__head;
405  }
406
407  template <class _Vp, size_t _Ip>
408  inline _LIBCPP_INLINE_VISIBILITY
409  static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
410    return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
411  }
412};
413
414struct __base {
415  template <size_t _Ip, class _Vp>
416  inline _LIBCPP_INLINE_VISIBILITY
417  static constexpr auto&& __get_alt(_Vp&& __v) {
418    return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
419                              in_place_index<_Ip>);
420  }
421};
422
423struct __variant {
424  template <size_t _Ip, class _Vp>
425  inline _LIBCPP_INLINE_VISIBILITY
426  static constexpr auto&& __get_alt(_Vp&& __v) {
427    return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
428  }
429};
430
431} // namespace __access
432
433namespace __visitation {
434
435struct __base {
436  template <class _Visitor, class... _Vs>
437  inline _LIBCPP_INLINE_VISIBILITY
438  static constexpr decltype(auto)
439  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
440    constexpr auto __fdiagonal =
441        __make_fdiagonal<_Visitor&&,
442                         decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
443    return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
444                                _VSTD::forward<_Vs>(__vs).__as_base()...);
445  }
446
447  template <class _Visitor, class... _Vs>
448  inline _LIBCPP_INLINE_VISIBILITY
449  static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
450                                              _Vs&&... __vs) {
451    constexpr auto __fmatrix =
452        __make_fmatrix<_Visitor&&,
453                       decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
454    return __at(__fmatrix, __vs.index()...)(
455        _VSTD::forward<_Visitor>(__visitor),
456        _VSTD::forward<_Vs>(__vs).__as_base()...);
457  }
458
459private:
460  template <class _Tp>
461  inline _LIBCPP_INLINE_VISIBILITY
462  static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
463
464  template <class _Tp, size_t _Np, typename... _Indices>
465  inline _LIBCPP_INLINE_VISIBILITY
466  static constexpr auto&& __at(const array<_Tp, _Np>& __elems,
467                               size_t __index, _Indices... __indices) {
468    return __at(__elems[__index], __indices...);
469  }
470
471  template <class _Fp, class... _Fs>
472  static constexpr void __std_visit_visitor_return_type_check() {
473    static_assert(
474        __all<is_same_v<_Fp, _Fs>...>::value,
475        "`std::visit` requires the visitor to have a single return type.");
476  }
477
478  template <class... _Fs>
479  inline _LIBCPP_INLINE_VISIBILITY
480  static constexpr auto __make_farray(_Fs&&... __fs) {
481    __std_visit_visitor_return_type_check<__uncvref_t<_Fs>...>();
482    using __result = array<common_type_t<__uncvref_t<_Fs>...>, sizeof...(_Fs)>;
483    return __result{{_VSTD::forward<_Fs>(__fs)...}};
484  }
485
486  template <std::size_t... _Is>
487  struct __dispatcher {
488    template <class _Fp, class... _Vs>
489    inline _LIBCPP_INLINE_VISIBILITY
490    static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
491        return __invoke_constexpr(
492            static_cast<_Fp>(__f),
493            __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
494    }
495  };
496
497  template <class _Fp, class... _Vs, size_t... _Is>
498  inline _LIBCPP_INLINE_VISIBILITY
499  static constexpr auto __make_dispatch(index_sequence<_Is...>) {
500    return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
501  }
502
503  template <size_t _Ip, class _Fp, class... _Vs>
504  inline _LIBCPP_INLINE_VISIBILITY
505  static constexpr auto __make_fdiagonal_impl() {
506    return __make_dispatch<_Fp, _Vs...>(
507        index_sequence<(__identity<_Vs>{}, _Ip)...>{});
508  }
509
510  template <class _Fp, class... _Vs, size_t... _Is>
511  inline _LIBCPP_INLINE_VISIBILITY
512  static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
513    return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
514  }
515
516  template <class _Fp, class _Vp, class... _Vs>
517  inline _LIBCPP_INLINE_VISIBILITY
518  static constexpr auto __make_fdiagonal() {
519    constexpr size_t _Np = __uncvref_t<_Vp>::__size();
520    static_assert(__all<(_Np == __uncvref_t<_Vs>::__size())...>::value);
521    return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
522  }
523
524  template <class _Fp, class... _Vs, size_t... _Is>
525  inline _LIBCPP_INLINE_VISIBILITY
526  static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
527    return __make_dispatch<_Fp, _Vs...>(__is);
528  }
529
530  template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
531  inline _LIBCPP_INLINE_VISIBILITY
532  static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
533                                            index_sequence<_Js...>,
534                                            _Ls... __ls) {
535    return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
536        index_sequence<_Is..., _Js>{}, __ls...)...);
537  }
538
539  template <class _Fp, class... _Vs>
540  inline _LIBCPP_INLINE_VISIBILITY
541  static constexpr auto __make_fmatrix() {
542    return __make_fmatrix_impl<_Fp, _Vs...>(
543        index_sequence<>{}, make_index_sequence<__uncvref_t<_Vs>::__size()>{}...);
544  }
545};
546
547struct __variant {
548  template <class _Visitor, class... _Vs>
549  inline _LIBCPP_INLINE_VISIBILITY
550  static constexpr decltype(auto)
551  __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
552    return __base::__visit_alt_at(__index,
553                                  _VSTD::forward<_Visitor>(__visitor),
554                                  _VSTD::forward<_Vs>(__vs).__impl...);
555  }
556
557  template <class _Visitor, class... _Vs>
558  inline _LIBCPP_INLINE_VISIBILITY
559  static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
560                                              _Vs&&... __vs) {
561    return __base::__visit_alt(_VSTD::forward<_Visitor>(__visitor),
562                               _VSTD::forward<_Vs>(__vs).__impl...);
563  }
564
565  template <class _Visitor, class... _Vs>
566  inline _LIBCPP_INLINE_VISIBILITY
567  static constexpr decltype(auto)
568  __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
569    return __visit_alt_at(
570        __index,
571        __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
572        _VSTD::forward<_Vs>(__vs)...);
573  }
574
575  template <class _Visitor, class... _Vs>
576  inline _LIBCPP_INLINE_VISIBILITY
577  static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
578                                                _Vs&&... __vs) {
579    return __visit_alt(
580        __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
581        _VSTD::forward<_Vs>(__vs)...);
582  }
583
584private:
585  template <class _Visitor, class... _Values>
586  static constexpr void __std_visit_exhaustive_visitor_check() {
587    static_assert(is_invocable_v<_Visitor, _Values...>,
588                  "`std::visit` requires the visitor to be exhaustive.");
589  }
590
591  template <class _Visitor>
592  struct __value_visitor {
593    template <class... _Alts>
594    inline _LIBCPP_INLINE_VISIBILITY
595    constexpr decltype(auto) operator()(_Alts&&... __alts) const {
596      __std_visit_exhaustive_visitor_check<
597          _Visitor,
598          decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
599      return __invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
600                                _VSTD::forward<_Alts>(__alts).__value...);
601    }
602    _Visitor&& __visitor;
603  };
604
605  template <class _Visitor>
606  inline _LIBCPP_INLINE_VISIBILITY
607  static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
608    return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
609  }
610};
611
612} // namespace __visitation
613
614template <size_t _Index, class _Tp>
615struct _LIBCPP_TEMPLATE_VIS __alt {
616  using __value_type = _Tp;
617
618  template <class... _Args>
619  inline _LIBCPP_INLINE_VISIBILITY
620  explicit constexpr __alt(in_place_t, _Args&&... __args)
621      : __value(_VSTD::forward<_Args>(__args)...) {}
622
623  __value_type __value;
624};
625
626template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
627union _LIBCPP_TEMPLATE_VIS __union;
628
629template <_Trait _DestructibleTrait, size_t _Index>
630union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
631
632#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor)                  \
633  template <size_t _Index, class _Tp, class... _Types>                         \
634  union _LIBCPP_TEMPLATE_VIS __union<destructible_trait,                      \
635                                      _Index,                                  \
636                                      _Tp,                                     \
637                                      _Types...> {                             \
638  public:                                                                      \
639    inline _LIBCPP_INLINE_VISIBILITY                                           \
640    explicit constexpr __union(__valueless_t) noexcept : __dummy{} {}          \
641                                                                               \
642    template <class... _Args>                                                  \
643    inline _LIBCPP_INLINE_VISIBILITY                                           \
644    explicit constexpr __union(in_place_index_t<0>, _Args&&... __args)         \
645        : __head(in_place, _VSTD::forward<_Args>(__args)...) {}                \
646                                                                               \
647    template <size_t _Ip, class... _Args>                                      \
648    inline _LIBCPP_INLINE_VISIBILITY                                           \
649    explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args)       \
650        : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
651                                                                               \
652    __union(const __union&) = default;                                         \
653    __union(__union&&) = default;                                              \
654                                                                               \
655    destructor                                                                 \
656                                                                               \
657    __union& operator=(const __union&) = default;                              \
658    __union& operator=(__union&&) = default;                                   \
659                                                                               \
660  private:                                                                     \
661    char __dummy;                                                              \
662    __alt<_Index, _Tp> __head;                                                 \
663    __union<destructible_trait, _Index + 1, _Types...> __tail;                 \
664                                                                               \
665    friend struct __access::__union;                                           \
666  }
667
668_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
669_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
670_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
671
672#undef _LIBCPP_VARIANT_UNION
673
674template <_Trait _DestructibleTrait, class... _Types>
675class _LIBCPP_TEMPLATE_VIS __base {
676public:
677  using __index_t = __variant_index_t<sizeof...(_Types)>;
678
679  inline _LIBCPP_INLINE_VISIBILITY
680  explicit constexpr __base(__valueless_t tag) noexcept
681      : __data(tag), __index(__variant_npos<__index_t>) {}
682
683  template <size_t _Ip, class... _Args>
684  inline _LIBCPP_INLINE_VISIBILITY
685  explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
686      :
687        __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
688        __index(_Ip) {}
689
690  inline _LIBCPP_INLINE_VISIBILITY
691  constexpr bool valueless_by_exception() const noexcept {
692    return index() == variant_npos;
693  }
694
695  inline _LIBCPP_INLINE_VISIBILITY
696  constexpr size_t index() const noexcept {
697    return __index == __variant_npos<__index_t> ? variant_npos : __index;
698  }
699
700protected:
701  inline _LIBCPP_INLINE_VISIBILITY
702  constexpr auto&& __as_base() & { return *this; }
703
704  inline _LIBCPP_INLINE_VISIBILITY
705  constexpr auto&& __as_base() && { return _VSTD::move(*this); }
706
707  inline _LIBCPP_INLINE_VISIBILITY
708  constexpr auto&& __as_base() const & { return *this; }
709
710  inline _LIBCPP_INLINE_VISIBILITY
711  constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
712
713  inline _LIBCPP_INLINE_VISIBILITY
714  static constexpr size_t __size() { return sizeof...(_Types); }
715
716  __union<_DestructibleTrait, 0, _Types...> __data;
717  __index_t __index;
718
719  friend struct __access::__base;
720  friend struct __visitation::__base;
721};
722
723template <class _Traits, _Trait = _Traits::__destructible_trait>
724class _LIBCPP_TEMPLATE_VIS __destructor;
725
726#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy)    \
727  template <class... _Types>                                                   \
728  class _LIBCPP_TEMPLATE_VIS __destructor<__traits<_Types...>,                 \
729                                           destructible_trait>                 \
730      : public __base<destructible_trait, _Types...> {                         \
731    using __base_type = __base<destructible_trait, _Types...>;                 \
732    using __index_t = typename __base_type::__index_t;                         \
733                                                                               \
734  public:                                                                      \
735    using __base_type::__base_type;                                            \
736    using __base_type::operator=;                                              \
737                                                                               \
738    __destructor(const __destructor&) = default;                               \
739    __destructor(__destructor&&) = default;                                    \
740    destructor                                                                 \
741    __destructor& operator=(const __destructor&) = default;                    \
742    __destructor& operator=(__destructor&&) = default;                         \
743                                                                               \
744  protected:                                                                   \
745    inline _LIBCPP_INLINE_VISIBILITY                                           \
746    destroy                                                                    \
747  }
748
749_LIBCPP_VARIANT_DESTRUCTOR(
750    _Trait::_TriviallyAvailable,
751    ~__destructor() = default;,
752    void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
753
754_LIBCPP_VARIANT_DESTRUCTOR(
755    _Trait::_Available,
756    ~__destructor() { __destroy(); },
757    void __destroy() noexcept {
758      if (!this->valueless_by_exception()) {
759        __visitation::__base::__visit_alt(
760            [](auto& __alt) noexcept {
761              using __alt_type = __uncvref_t<decltype(__alt)>;
762              __alt.~__alt_type();
763            },
764            *this);
765      }
766      this->__index = __variant_npos<__index_t>;
767    });
768
769_LIBCPP_VARIANT_DESTRUCTOR(
770    _Trait::_Unavailable,
771    ~__destructor() = delete;,
772    void __destroy() noexcept = delete;);
773
774#undef _LIBCPP_VARIANT_DESTRUCTOR
775
776template <class _Traits>
777class _LIBCPP_TEMPLATE_VIS __constructor : public __destructor<_Traits> {
778  using __base_type = __destructor<_Traits>;
779
780public:
781  using __base_type::__base_type;
782  using __base_type::operator=;
783
784protected:
785  template <size_t _Ip, class _Tp, class... _Args>
786  inline _LIBCPP_INLINE_VISIBILITY
787  static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
788    ::new ((void*)_VSTD::addressof(__a))
789        __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
790    return __a.__value;
791  }
792
793  template <class _Rhs>
794  inline _LIBCPP_INLINE_VISIBILITY
795  static void __generic_construct(__constructor& __lhs, _Rhs&& __rhs) {
796    __lhs.__destroy();
797    if (!__rhs.valueless_by_exception()) {
798      __visitation::__base::__visit_alt_at(
799          __rhs.index(),
800          [](auto& __lhs_alt, auto&& __rhs_alt) {
801            __construct_alt(
802                __lhs_alt,
803                _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
804          },
805          __lhs, _VSTD::forward<_Rhs>(__rhs));
806      __lhs.__index = __rhs.index();
807    }
808  }
809};
810
811template <class _Traits, _Trait = _Traits::__move_constructible_trait>
812class _LIBCPP_TEMPLATE_VIS __move_constructor;
813
814#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait,             \
815                                         move_constructor)                     \
816  template <class... _Types>                                                   \
817  class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>,          \
818                                                 move_constructible_trait>     \
819      : public __constructor<__traits<_Types...>> {                            \
820    using __base_type = __constructor<__traits<_Types...>>;                    \
821                                                                               \
822  public:                                                                      \
823    using __base_type::__base_type;                                            \
824    using __base_type::operator=;                                              \
825                                                                               \
826    __move_constructor(const __move_constructor&) = default;                   \
827    move_constructor                                                           \
828    ~__move_constructor() = default;                                           \
829    __move_constructor& operator=(const __move_constructor&) = default;        \
830    __move_constructor& operator=(__move_constructor&&) = default;             \
831  }
832
833_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
834    _Trait::_TriviallyAvailable,
835    __move_constructor(__move_constructor&& __that) = default;);
836
837_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
838    _Trait::_Available,
839    __move_constructor(__move_constructor&& __that) noexcept(
840        __all<is_nothrow_move_constructible_v<_Types>...>::value)
841        : __move_constructor(__valueless_t{}) {
842      this->__generic_construct(*this, _VSTD::move(__that));
843    });
844
845_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
846    _Trait::_Unavailable,
847    __move_constructor(__move_constructor&&) = delete;);
848
849#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
850
851template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
852class _LIBCPP_TEMPLATE_VIS __copy_constructor;
853
854#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait,             \
855                                         copy_constructor)                     \
856  template <class... _Types>                                                   \
857  class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>,          \
858                                                 copy_constructible_trait>     \
859      : public __move_constructor<__traits<_Types...>> {                       \
860    using __base_type = __move_constructor<__traits<_Types...>>;               \
861                                                                               \
862  public:                                                                      \
863    using __base_type::__base_type;                                            \
864    using __base_type::operator=;                                              \
865                                                                               \
866    copy_constructor                                                           \
867    __copy_constructor(__copy_constructor&&) = default;                        \
868    ~__copy_constructor() = default;                                           \
869    __copy_constructor& operator=(const __copy_constructor&) = default;        \
870    __copy_constructor& operator=(__copy_constructor&&) = default;             \
871  }
872
873_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
874    _Trait::_TriviallyAvailable,
875    __copy_constructor(const __copy_constructor& __that) = default;);
876
877_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
878    _Trait::_Available,
879    __copy_constructor(const __copy_constructor& __that)
880        : __copy_constructor(__valueless_t{}) {
881      this->__generic_construct(*this, __that);
882    });
883
884_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
885    _Trait::_Unavailable,
886    __copy_constructor(const __copy_constructor&) = delete;);
887
888#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
889
890template <class _Traits>
891class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
892  using __base_type = __copy_constructor<_Traits>;
893
894public:
895  using __base_type::__base_type;
896  using __base_type::operator=;
897
898  template <size_t _Ip, class... _Args>
899  inline _LIBCPP_INLINE_VISIBILITY
900  auto& __emplace(_Args&&... __args) {
901    this->__destroy();
902    auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
903                          _VSTD::forward<_Args>(__args)...);
904    this->__index = _Ip;
905    return __res;
906  }
907
908protected:
909  template <size_t _Ip, class _Tp, class _Arg>
910  inline _LIBCPP_INLINE_VISIBILITY
911  void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
912    if (this->index() == _Ip) {
913      __a.__value = _VSTD::forward<_Arg>(__arg);
914    } else {
915      struct {
916        void operator()(true_type) const {
917          __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
918        }
919        void operator()(false_type) const {
920          __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
921        }
922        __assignment* __this;
923        _Arg&& __arg;
924      } __impl{this, _VSTD::forward<_Arg>(__arg)};
925      __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
926                           !is_nothrow_move_constructible_v<_Tp>>{});
927    }
928  }
929
930  template <class _That>
931  inline _LIBCPP_INLINE_VISIBILITY
932  void __generic_assign(_That&& __that) {
933    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
934      // do nothing.
935    } else if (__that.valueless_by_exception()) {
936      this->__destroy();
937    } else {
938      __visitation::__base::__visit_alt_at(
939          __that.index(),
940          [this](auto& __this_alt, auto&& __that_alt) {
941            this->__assign_alt(
942                __this_alt,
943                _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
944          },
945          *this, _VSTD::forward<_That>(__that));
946    }
947  }
948};
949
950template <class _Traits, _Trait = _Traits::__move_assignable_trait>
951class _LIBCPP_TEMPLATE_VIS __move_assignment;
952
953#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait,                 \
954                                        move_assignment)                       \
955  template <class... _Types>                                                   \
956  class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>,           \
957                                                move_assignable_trait>         \
958      : public __assignment<__traits<_Types...>> {                             \
959    using __base_type = __assignment<__traits<_Types...>>;                     \
960                                                                               \
961  public:                                                                      \
962    using __base_type::__base_type;                                            \
963    using __base_type::operator=;                                              \
964                                                                               \
965    __move_assignment(const __move_assignment&) = default;                     \
966    __move_assignment(__move_assignment&&) = default;                          \
967    ~__move_assignment() = default;                                            \
968    __move_assignment& operator=(const __move_assignment&) = default;          \
969    move_assignment                                                            \
970  }
971
972_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
973    _Trait::_TriviallyAvailable,
974    __move_assignment& operator=(__move_assignment&& __that) = default;);
975
976_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
977    _Trait::_Available,
978    __move_assignment& operator=(__move_assignment&& __that) noexcept(
979        __all<(is_nothrow_move_constructible_v<_Types> &&
980               is_nothrow_move_assignable_v<_Types>)...>::value) {
981      this->__generic_assign(_VSTD::move(__that));
982      return *this;
983    });
984
985_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
986    _Trait::_Unavailable,
987    __move_assignment& operator=(__move_assignment&&) = delete;);
988
989#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
990
991template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
992class _LIBCPP_TEMPLATE_VIS __copy_assignment;
993
994#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait,                 \
995                                        copy_assignment)                       \
996  template <class... _Types>                                                   \
997  class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>,           \
998                                                copy_assignable_trait>         \
999      : public __move_assignment<__traits<_Types...>> {                        \
1000    using __base_type = __move_assignment<__traits<_Types...>>;                \
1001                                                                               \
1002  public:                                                                      \
1003    using __base_type::__base_type;                                            \
1004    using __base_type::operator=;                                              \
1005                                                                               \
1006    __copy_assignment(const __copy_assignment&) = default;                     \
1007    __copy_assignment(__copy_assignment&&) = default;                          \
1008    ~__copy_assignment() = default;                                            \
1009    copy_assignment                                                            \
1010    __copy_assignment& operator=(__copy_assignment&&) = default;               \
1011  }
1012
1013_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1014    _Trait::_TriviallyAvailable,
1015    __copy_assignment& operator=(const __copy_assignment& __that) = default;);
1016
1017_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1018    _Trait::_Available,
1019    __copy_assignment& operator=(const __copy_assignment& __that) {
1020      this->__generic_assign(__that);
1021      return *this;
1022    });
1023
1024_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1025    _Trait::_Unavailable,
1026    __copy_assignment& operator=(const __copy_assignment&) = delete;);
1027
1028#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1029
1030template <class... _Types>
1031class _LIBCPP_TEMPLATE_VIS __impl
1032    : public __copy_assignment<__traits<_Types...>> {
1033  using __base_type = __copy_assignment<__traits<_Types...>>;
1034
1035public:
1036  using __base_type::__base_type;
1037  using __base_type::operator=;
1038
1039  template <size_t _Ip, class _Arg>
1040  inline _LIBCPP_INLINE_VISIBILITY
1041  void __assign(_Arg&& __arg) {
1042    this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
1043                       _VSTD::forward<_Arg>(__arg));
1044  }
1045
1046  inline _LIBCPP_INLINE_VISIBILITY
1047  void __swap(__impl& __that)  {
1048    if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1049      // do nothing.
1050    } else if (this->index() == __that.index()) {
1051      __visitation::__base::__visit_alt_at(
1052          this->index(),
1053          [](auto& __this_alt, auto& __that_alt) {
1054            using _VSTD::swap;
1055            swap(__this_alt.__value, __that_alt.__value);
1056          },
1057          *this,
1058          __that);
1059    } else {
1060      __impl* __lhs = this;
1061      __impl* __rhs = _VSTD::addressof(__that);
1062      if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1063        _VSTD::swap(__lhs, __rhs);
1064      }
1065      __impl __tmp(_VSTD::move(*__rhs));
1066#ifndef _LIBCPP_NO_EXCEPTIONS
1067      // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1068      // and `__tmp` is nothrow move constructible then we move `__tmp` back
1069      // into `__rhs` and provide the strong exception safety guarantee.
1070      try {
1071        this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1072      } catch (...) {
1073        if (__tmp.__move_nothrow()) {
1074          this->__generic_construct(*__rhs, _VSTD::move(__tmp));
1075        }
1076        throw;
1077      }
1078#else
1079      this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1080#endif
1081      this->__generic_construct(*__lhs, _VSTD::move(__tmp));
1082    }
1083  }
1084
1085private:
1086  inline _LIBCPP_INLINE_VISIBILITY
1087  bool __move_nothrow() const {
1088    constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1089    return this->valueless_by_exception() || __results[this->index()];
1090  }
1091};
1092
1093template <class... _Types>
1094struct __overload;
1095
1096template <>
1097struct __overload<> { void operator()() const; };
1098
1099template <class _Tp, class... _Types>
1100struct __overload<_Tp, _Types...> : __overload<_Types...> {
1101  using __overload<_Types...>::operator();
1102  __identity<_Tp> operator()(_Tp) const;
1103};
1104
1105template <class _Tp, class... _Types>
1106using __best_match_t = typename result_of_t<__overload<_Types...>(_Tp&&)>::type;
1107
1108} // __variant_detail
1109
1110template <class... _Types>
1111class _LIBCPP_TEMPLATE_VIS variant
1112    : private __sfinae_ctor_base<
1113          __all<is_copy_constructible_v<_Types>...>::value,
1114          __all<is_move_constructible_v<_Types>...>::value>,
1115      private __sfinae_assign_base<
1116          __all<(is_copy_constructible_v<_Types> &&
1117                 is_copy_assignable_v<_Types>)...>::value,
1118          __all<(is_move_constructible_v<_Types> &&
1119                 is_move_assignable_v<_Types>)...>::value> {
1120  static_assert(0 < sizeof...(_Types),
1121                "variant must consist of at least one alternative.");
1122
1123  static_assert(__all<!is_array_v<_Types>...>::value,
1124                "variant can not have an array type as an alternative.");
1125
1126  static_assert(__all<!is_reference_v<_Types>...>::value,
1127                "variant can not have a reference type as an alternative.");
1128
1129  static_assert(__all<!is_void_v<_Types>...>::value,
1130                "variant can not have a void type as an alternative.");
1131
1132  using __first_type = variant_alternative_t<0, variant>;
1133
1134public:
1135  template <bool _Dummy = true,
1136            enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1137                                         _Dummy>::value,
1138                        int> = 0>
1139  inline _LIBCPP_INLINE_VISIBILITY
1140  constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1141      : __impl(in_place_index<0>) {}
1142
1143  variant(const variant&) = default;
1144  variant(variant&&) = default;
1145
1146  template <
1147      class _Arg,
1148      enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
1149      enable_if_t<!__is_inplace_type<__uncvref_t<_Arg>>::value, int> = 0,
1150      enable_if_t<!__is_inplace_index<__uncvref_t<_Arg>>::value, int> = 0,
1151      class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1152      size_t _Ip =
1153          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1154      enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1155  inline _LIBCPP_INLINE_VISIBILITY
1156  constexpr variant(_Arg&& __arg) noexcept(
1157      is_nothrow_constructible_v<_Tp, _Arg>)
1158      : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
1159
1160  template <size_t _Ip, class... _Args,
1161            class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
1162            class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1163            enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1164  inline _LIBCPP_INLINE_VISIBILITY
1165  explicit constexpr variant(
1166      in_place_index_t<_Ip>,
1167      _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1168      : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1169
1170  template <
1171      size_t _Ip,
1172      class _Up,
1173      class... _Args,
1174      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1175      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1176      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1177                  int> = 0>
1178  inline _LIBCPP_INLINE_VISIBILITY
1179  explicit constexpr variant(
1180      in_place_index_t<_Ip>,
1181      initializer_list<_Up> __il,
1182      _Args&&... __args) noexcept(
1183      is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1184      : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1185
1186  template <
1187      class _Tp,
1188      class... _Args,
1189      size_t _Ip =
1190          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1191      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1192  inline _LIBCPP_INLINE_VISIBILITY
1193  explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1194      is_nothrow_constructible_v<_Tp, _Args...>)
1195      : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1196
1197  template <
1198      class _Tp,
1199      class _Up,
1200      class... _Args,
1201      size_t _Ip =
1202          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1203      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1204                  int> = 0>
1205  inline _LIBCPP_INLINE_VISIBILITY
1206  explicit constexpr variant(
1207      in_place_type_t<_Tp>,
1208      initializer_list<_Up> __il,
1209      _Args&&... __args) noexcept(
1210      is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1211      : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1212
1213  ~variant() = default;
1214
1215  variant& operator=(const variant&) = default;
1216  variant& operator=(variant&&) = default;
1217
1218  template <
1219      class _Arg,
1220      enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
1221      class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1222      size_t _Ip =
1223          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1224      enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
1225                  int> = 0>
1226  inline _LIBCPP_INLINE_VISIBILITY
1227  variant& operator=(_Arg&& __arg) noexcept(
1228      is_nothrow_assignable_v<_Tp&, _Arg> &&
1229      is_nothrow_constructible_v<_Tp, _Arg>) {
1230    __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
1231    return *this;
1232  }
1233
1234  template <
1235      size_t _Ip,
1236      class... _Args,
1237      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1238      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1239      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1240  inline _LIBCPP_INLINE_VISIBILITY
1241  _Tp& emplace(_Args&&... __args) {
1242    return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
1243  }
1244
1245  template <
1246      size_t _Ip,
1247      class _Up,
1248      class... _Args,
1249      enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1250      class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1251      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1252                  int> = 0>
1253  inline _LIBCPP_INLINE_VISIBILITY
1254  _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1255    return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
1256  }
1257
1258  template <
1259      class _Tp,
1260      class... _Args,
1261      size_t _Ip =
1262          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1263      enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1264  inline _LIBCPP_INLINE_VISIBILITY
1265  _Tp& emplace(_Args&&... __args) {
1266    return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
1267  }
1268
1269  template <
1270      class _Tp,
1271      class _Up,
1272      class... _Args,
1273      size_t _Ip =
1274          __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1275      enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1276                  int> = 0>
1277  inline _LIBCPP_INLINE_VISIBILITY
1278  _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1279    return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
1280  }
1281
1282  inline _LIBCPP_INLINE_VISIBILITY
1283  constexpr bool valueless_by_exception() const noexcept {
1284    return __impl.valueless_by_exception();
1285  }
1286
1287  inline _LIBCPP_INLINE_VISIBILITY
1288  constexpr size_t index() const noexcept { return __impl.index(); }
1289
1290  template <
1291      bool _Dummy = true,
1292      enable_if_t<
1293          __all<(
1294              __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1295              __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1296          int> = 0>
1297  inline _LIBCPP_INLINE_VISIBILITY
1298  void swap(variant& __that) noexcept(
1299      __all<(is_nothrow_move_constructible_v<_Types> &&
1300             is_nothrow_swappable_v<_Types>)...>::value) {
1301    __impl.__swap(__that.__impl);
1302  }
1303
1304private:
1305  __variant_detail::__impl<_Types...> __impl;
1306
1307  friend struct __variant_detail::__access::__variant;
1308  friend struct __variant_detail::__visitation::__variant;
1309};
1310
1311template <size_t _Ip, class... _Types>
1312inline _LIBCPP_INLINE_VISIBILITY
1313constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1314  return __v.index() == _Ip;
1315}
1316
1317template <class _Tp, class... _Types>
1318inline _LIBCPP_INLINE_VISIBILITY
1319constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1320  return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1321}
1322
1323template <size_t _Ip, class _Vp>
1324inline _LIBCPP_INLINE_VISIBILITY
1325_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1326constexpr auto&& __generic_get(_Vp&& __v) {
1327  using __variant_detail::__access::__variant;
1328  if (!__holds_alternative<_Ip>(__v)) {
1329    __throw_bad_variant_access();
1330  }
1331  return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
1332}
1333
1334template <size_t _Ip, class... _Types>
1335inline _LIBCPP_INLINE_VISIBILITY
1336_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1337constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
1338    variant<_Types...>& __v) {
1339  static_assert(_Ip < sizeof...(_Types));
1340  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1341  return __generic_get<_Ip>(__v);
1342}
1343
1344template <size_t _Ip, class... _Types>
1345inline _LIBCPP_INLINE_VISIBILITY
1346_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1347constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
1348    variant<_Types...>&& __v) {
1349  static_assert(_Ip < sizeof...(_Types));
1350  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1351  return __generic_get<_Ip>(_VSTD::move(__v));
1352}
1353
1354template <size_t _Ip, class... _Types>
1355inline _LIBCPP_INLINE_VISIBILITY
1356_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1357constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
1358    const variant<_Types...>& __v) {
1359  static_assert(_Ip < sizeof...(_Types));
1360  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1361  return __generic_get<_Ip>(__v);
1362}
1363
1364template <size_t _Ip, class... _Types>
1365inline _LIBCPP_INLINE_VISIBILITY
1366_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1367constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
1368    const variant<_Types...>&& __v) {
1369  static_assert(_Ip < sizeof...(_Types));
1370  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1371  return __generic_get<_Ip>(_VSTD::move(__v));
1372}
1373
1374template <class _Tp, class... _Types>
1375inline _LIBCPP_INLINE_VISIBILITY
1376_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1377constexpr _Tp& get(variant<_Types...>& __v) {
1378  static_assert(!is_void_v<_Tp>);
1379  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1380}
1381
1382template <class _Tp, class... _Types>
1383inline _LIBCPP_INLINE_VISIBILITY
1384_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1385constexpr _Tp&& get(variant<_Types...>&& __v) {
1386  static_assert(!is_void_v<_Tp>);
1387  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1388      _VSTD::move(__v));
1389}
1390
1391template <class _Tp, class... _Types>
1392inline _LIBCPP_INLINE_VISIBILITY
1393_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1394constexpr const _Tp& get(const variant<_Types...>& __v) {
1395  static_assert(!is_void_v<_Tp>);
1396  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1397}
1398
1399template <class _Tp, class... _Types>
1400inline _LIBCPP_INLINE_VISIBILITY
1401_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1402constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1403  static_assert(!is_void_v<_Tp>);
1404  return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1405      _VSTD::move(__v));
1406}
1407
1408template <size_t _Ip, class _Vp>
1409inline _LIBCPP_INLINE_VISIBILITY
1410constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1411  using __variant_detail::__access::__variant;
1412  return __v && __holds_alternative<_Ip>(*__v)
1413             ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
1414             : nullptr;
1415}
1416
1417template <size_t _Ip, class... _Types>
1418inline _LIBCPP_INLINE_VISIBILITY
1419constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1420get_if(variant<_Types...>* __v) noexcept {
1421  static_assert(_Ip < sizeof...(_Types));
1422  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1423  return __generic_get_if<_Ip>(__v);
1424}
1425
1426template <size_t _Ip, class... _Types>
1427inline _LIBCPP_INLINE_VISIBILITY
1428constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1429get_if(const variant<_Types...>* __v) noexcept {
1430  static_assert(_Ip < sizeof...(_Types));
1431  static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1432  return __generic_get_if<_Ip>(__v);
1433}
1434
1435template <class _Tp, class... _Types>
1436inline _LIBCPP_INLINE_VISIBILITY
1437constexpr add_pointer_t<_Tp>
1438get_if(variant<_Types...>* __v) noexcept {
1439  static_assert(!is_void_v<_Tp>);
1440  return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1441}
1442
1443template <class _Tp, class... _Types>
1444inline _LIBCPP_INLINE_VISIBILITY
1445constexpr add_pointer_t<const _Tp>
1446get_if(const variant<_Types...>* __v) noexcept {
1447  static_assert(!is_void_v<_Tp>);
1448  return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1449}
1450
1451template <class _Operator>
1452struct __convert_to_bool {
1453  template <class _T1, class _T2>
1454  _LIBCPP_INLINE_VISIBILITY constexpr bool operator()(_T1 && __t1, _T2&& __t2) const {
1455    static_assert(std::is_convertible<decltype(_Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2))), bool>::value,
1456        "the relational operator does not return a type which is implicitly convertible to bool");
1457    return _Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
1458  }
1459};
1460
1461template <class... _Types>
1462inline _LIBCPP_INLINE_VISIBILITY
1463constexpr bool operator==(const variant<_Types...>& __lhs,
1464                          const variant<_Types...>& __rhs) {
1465  using __variant_detail::__visitation::__variant;
1466  if (__lhs.index() != __rhs.index()) return false;
1467  if (__lhs.valueless_by_exception()) return true;
1468  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
1469}
1470
1471template <class... _Types>
1472inline _LIBCPP_INLINE_VISIBILITY
1473constexpr bool operator!=(const variant<_Types...>& __lhs,
1474                          const variant<_Types...>& __rhs) {
1475  using __variant_detail::__visitation::__variant;
1476  if (__lhs.index() != __rhs.index()) return true;
1477  if (__lhs.valueless_by_exception()) return false;
1478  return __variant::__visit_value_at(
1479      __lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
1480}
1481
1482template <class... _Types>
1483inline _LIBCPP_INLINE_VISIBILITY
1484constexpr bool operator<(const variant<_Types...>& __lhs,
1485                         const variant<_Types...>& __rhs) {
1486  using __variant_detail::__visitation::__variant;
1487  if (__rhs.valueless_by_exception()) return false;
1488  if (__lhs.valueless_by_exception()) return true;
1489  if (__lhs.index() < __rhs.index()) return true;
1490  if (__lhs.index() > __rhs.index()) return false;
1491  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
1492}
1493
1494template <class... _Types>
1495inline _LIBCPP_INLINE_VISIBILITY
1496constexpr bool operator>(const variant<_Types...>& __lhs,
1497                         const variant<_Types...>& __rhs) {
1498  using __variant_detail::__visitation::__variant;
1499  if (__lhs.valueless_by_exception()) return false;
1500  if (__rhs.valueless_by_exception()) return true;
1501  if (__lhs.index() > __rhs.index()) return true;
1502  if (__lhs.index() < __rhs.index()) return false;
1503  return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
1504}
1505
1506template <class... _Types>
1507inline _LIBCPP_INLINE_VISIBILITY
1508constexpr bool operator<=(const variant<_Types...>& __lhs,
1509                          const variant<_Types...>& __rhs) {
1510  using __variant_detail::__visitation::__variant;
1511  if (__lhs.valueless_by_exception()) return true;
1512  if (__rhs.valueless_by_exception()) return false;
1513  if (__lhs.index() < __rhs.index()) return true;
1514  if (__lhs.index() > __rhs.index()) return false;
1515  return __variant::__visit_value_at(
1516      __lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
1517}
1518
1519template <class... _Types>
1520inline _LIBCPP_INLINE_VISIBILITY
1521constexpr bool operator>=(const variant<_Types...>& __lhs,
1522                          const variant<_Types...>& __rhs) {
1523  using __variant_detail::__visitation::__variant;
1524  if (__rhs.valueless_by_exception()) return true;
1525  if (__lhs.valueless_by_exception()) return false;
1526  if (__lhs.index() > __rhs.index()) return true;
1527  if (__lhs.index() < __rhs.index()) return false;
1528  return __variant::__visit_value_at(
1529      __lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
1530}
1531
1532template <class _Visitor, class... _Vs>
1533inline _LIBCPP_INLINE_VISIBILITY
1534_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
1535constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
1536  using __variant_detail::__visitation::__variant;
1537  bool __results[] = {__vs.valueless_by_exception()...};
1538  for (bool __result : __results) {
1539    if (__result) {
1540      __throw_bad_variant_access();
1541    }
1542  }
1543  return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
1544                                  _VSTD::forward<_Vs>(__vs)...);
1545}
1546
1547struct _LIBCPP_TEMPLATE_VIS monostate {};
1548
1549inline _LIBCPP_INLINE_VISIBILITY
1550constexpr bool operator<(monostate, monostate) noexcept { return false; }
1551
1552inline _LIBCPP_INLINE_VISIBILITY
1553constexpr bool operator>(monostate, monostate) noexcept { return false; }
1554
1555inline _LIBCPP_INLINE_VISIBILITY
1556constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1557
1558inline _LIBCPP_INLINE_VISIBILITY
1559constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1560
1561inline _LIBCPP_INLINE_VISIBILITY
1562constexpr bool operator==(monostate, monostate) noexcept { return true; }
1563
1564inline _LIBCPP_INLINE_VISIBILITY
1565constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1566
1567template <class... _Types>
1568inline _LIBCPP_INLINE_VISIBILITY
1569auto swap(variant<_Types...>& __lhs,
1570          variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1571    -> decltype(__lhs.swap(__rhs)) {
1572  __lhs.swap(__rhs);
1573}
1574
1575template <class... _Types>
1576struct _LIBCPP_TEMPLATE_VIS hash<
1577    __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
1578  using argument_type = variant<_Types...>;
1579  using result_type = size_t;
1580
1581  inline _LIBCPP_INLINE_VISIBILITY
1582  result_type operator()(const argument_type& __v) const {
1583    using __variant_detail::__visitation::__variant;
1584    size_t __res =
1585        __v.valueless_by_exception()
1586               ? 299792458 // Random value chosen by the universe upon creation
1587               : __variant::__visit_alt(
1588                     [](const auto& __alt) {
1589                       using __alt_type = __uncvref_t<decltype(__alt)>;
1590                       using __value_type = remove_const_t<
1591                         typename __alt_type::__value_type>;
1592                       return hash<__value_type>{}(__alt.__value);
1593                     },
1594                     __v);
1595    return __hash_combine(__res, hash<size_t>{}(__v.index()));
1596  }
1597};
1598
1599template <>
1600struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
1601  using argument_type = monostate;
1602  using result_type = size_t;
1603
1604  inline _LIBCPP_INLINE_VISIBILITY
1605  result_type operator()(const argument_type&) const _NOEXCEPT {
1606    return 66740831; // return a fundamentally attractive random value.
1607  }
1608};
1609
1610#endif  // _LIBCPP_STD_VER > 14
1611
1612_LIBCPP_END_NAMESPACE_STD
1613
1614_LIBCPP_POP_MACROS
1615
1616#endif  // _LIBCPP_VARIANT
1617