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