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