Home | History | Annotate | Download | only in v1
      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 
     17 namespace 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 
    215 namespace std { // explicitly not using versioning namespace
    216 
    217 class _LIBCPP_EXCEPTION_ABI bad_variant_access : public exception {
    218 public:
    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
    229 inline _LIBCPP_INLINE_VISIBILITY
    230 void __throw_bad_variant_access() {
    231 #ifndef _LIBCPP_NO_EXCEPTIONS
    232         throw bad_variant_access();
    233 #else
    234         _VSTD::abort();
    235 #endif
    236 }
    237 
    238 template <class... _Types>
    239 class _LIBCPP_TEMPLATE_VIS variant;
    240 
    241 template <class _Tp>
    242 struct _LIBCPP_TEMPLATE_VIS variant_size;
    243 
    244 template <class _Tp>
    245 constexpr size_t variant_size_v = variant_size<_Tp>::value;
    246 
    247 template <class _Tp>
    248 struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
    249 
    250 template <class _Tp>
    251 struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
    252 
    253 template <class _Tp>
    254 struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
    255     : variant_size<_Tp> {};
    256 
    257 template <class... _Types>
    258 struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
    259     : integral_constant<size_t, sizeof...(_Types)> {};
    260 
    261 template <size_t _Ip, class _Tp>
    262 struct _LIBCPP_TEMPLATE_VIS variant_alternative;
    263 
    264 template <size_t _Ip, class _Tp>
    265 using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
    266 
    267 template <size_t _Ip, class _Tp>
    268 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
    269     : add_const<variant_alternative_t<_Ip, _Tp>> {};
    270 
    271 template <size_t _Ip, class _Tp>
    272 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
    273     : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
    274 
    275 template <size_t _Ip, class _Tp>
    276 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
    277     : add_cv<variant_alternative_t<_Ip, _Tp>> {};
    278 
    279 template <size_t _Ip, class... _Types>
    280 struct _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 
    285 constexpr size_t variant_npos = static_cast<size_t>(-1);
    286 constexpr unsigned int __variant_npos = static_cast<unsigned int>(-1);
    287 
    288 namespace __find_detail {
    289 
    290 template <class _Tp, class... _Types>
    291 inline _LIBCPP_INLINE_VISIBILITY
    292 constexpr 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 
    306 template <size_t _Index>
    307 struct __find_unambiguous_index_sfinae_impl
    308     : integral_constant<size_t, _Index> {};
    309 
    310 template <>
    311 struct __find_unambiguous_index_sfinae_impl<__not_found> {};
    312 
    313 template <>
    314 struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
    315 
    316 template <class _Tp, class... _Types>
    317 struct __find_unambiguous_index_sfinae
    318     : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
    319 
    320 } // namespace __find_detail
    321 
    322 namespace __variant_detail {
    323 
    324 struct __valueless_t {};
    325 
    326 enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
    327 
    328 template <typename _Tp,
    329           template <typename> class _IsTriviallyAvailable,
    330           template <typename> class _IsAvailable>
    331 constexpr _Trait __trait =
    332     _IsTriviallyAvailable<_Tp>::value
    333         ? _Trait::_TriviallyAvailable
    334         : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
    335 
    336 inline _LIBCPP_INLINE_VISIBILITY
    337 constexpr _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 
    347 template <typename... _Types>
    348 struct __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 
    371 namespace __access {
    372 
    373 struct __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 
    387 struct __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 
    396 struct __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 
    406 namespace __visitation {
    407 
    408 struct __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 
    432 private:
    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 
    520 struct __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 
    557 private:
    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 
    587 template <size_t _Index, class _Tp>
    588 struct _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 
    599 template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
    600 union _LIBCPP_TEMPLATE_VIS __union;
    601 
    602 template <_Trait _DestructibleTrait, size_t _Index>
    603 union _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 
    647 template <_Trait _DestructibleTrait, class... _Types>
    648 class _LIBCPP_TEMPLATE_VIS __base {
    649 public:
    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 
    671 protected:
    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 
    694 template <class _Traits, _Trait = _Traits::__destructible_trait>
    695 class _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 
    746 template <class _Traits>
    747 class _LIBCPP_TEMPLATE_VIS __constructor : public __destructor<_Traits> {
    748   using __base_type = __destructor<_Traits>;
    749 
    750 public:
    751   using __base_type::__base_type;
    752   using __base_type::operator=;
    753 
    754 protected:
    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 
    781 template <class _Traits, _Trait = _Traits::__move_constructible_trait>
    782 class _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 
    821 template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
    822 class _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 
    860 template <class _Traits>
    861 class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
    862   using __base_type = __copy_constructor<_Traits>;
    863 
    864 public:
    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 
    878 protected:
    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 
    920 template <class _Traits, _Trait = _Traits::__move_assignable_trait>
    921 class _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 
    961 template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
    962 class _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 
   1000 template <class... _Types>
   1001 class _LIBCPP_TEMPLATE_VIS __impl
   1002     : public __copy_assignment<__traits<_Types...>> {
   1003   using __base_type = __copy_assignment<__traits<_Types...>>;
   1004 
   1005 public:
   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 
   1055 private:
   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 
   1063 template <class... _Types>
   1064 struct __overload;
   1065 
   1066 template <>
   1067 struct __overload<> { void operator()() const; };
   1068 
   1069 template <class _Tp, class... _Types>
   1070 struct __overload<_Tp, _Types...> : __overload<_Types...> {
   1071   using __overload<_Types...>::operator();
   1072   __identity<_Tp> operator()(_Tp) const;
   1073 };
   1074 
   1075 template <class _Tp, class... _Types>
   1076 using __best_match_t = typename result_of_t<__overload<_Types...>(_Tp&&)>::type;
   1077 
   1078 } // __variant_detail
   1079 
   1080 template <class... _Types>
   1081 class _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 
   1104 public:
   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       class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
   1120       size_t _Ip =
   1121           __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
   1122       enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
   1123   inline _LIBCPP_INLINE_VISIBILITY
   1124   constexpr variant(_Arg&& __arg) noexcept(
   1125       is_nothrow_constructible_v<_Tp, _Arg>)
   1126       : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
   1127 
   1128   template <size_t _Ip, class... _Args,
   1129             class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
   1130             class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
   1131             enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
   1132   inline _LIBCPP_INLINE_VISIBILITY
   1133   explicit constexpr variant(
   1134       in_place_index_t<_Ip>,
   1135       _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
   1136       : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
   1137 
   1138   template <
   1139       size_t _Ip,
   1140       class _Up,
   1141       class... _Args,
   1142       enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
   1143       class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
   1144       enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
   1145                   int> = 0>
   1146   inline _LIBCPP_INLINE_VISIBILITY
   1147   explicit constexpr variant(
   1148       in_place_index_t<_Ip>,
   1149       initializer_list<_Up> __il,
   1150       _Args&&... __args) noexcept(
   1151       is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
   1152       : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
   1153 
   1154   template <
   1155       class _Tp,
   1156       class... _Args,
   1157       size_t _Ip =
   1158           __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
   1159       enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
   1160   inline _LIBCPP_INLINE_VISIBILITY
   1161   explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
   1162       is_nothrow_constructible_v<_Tp, _Args...>)
   1163       : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
   1164 
   1165   template <
   1166       class _Tp,
   1167       class _Up,
   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, initializer_list<_Up>&, _Args...>,
   1172                   int> = 0>
   1173   inline _LIBCPP_INLINE_VISIBILITY
   1174   explicit constexpr variant(
   1175       in_place_type_t<_Tp>,
   1176       initializer_list<_Up> __il,
   1177       _Args&&... __args) noexcept(
   1178       is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
   1179       : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
   1180 
   1181   ~variant() = default;
   1182 
   1183   variant& operator=(const variant&) = default;
   1184   variant& operator=(variant&&) = default;
   1185 
   1186   template <
   1187       class _Arg,
   1188       enable_if_t<!is_same_v<decay_t<_Arg>, variant>, int> = 0,
   1189       class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
   1190       size_t _Ip =
   1191           __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
   1192       enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
   1193                   int> = 0>
   1194   inline _LIBCPP_INLINE_VISIBILITY
   1195   variant& operator=(_Arg&& __arg) noexcept(
   1196       is_nothrow_assignable_v<_Tp&, _Arg> &&
   1197       is_nothrow_constructible_v<_Tp, _Arg>) {
   1198     __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
   1199     return *this;
   1200   }
   1201 
   1202   template <
   1203       size_t _Ip,
   1204       class... _Args,
   1205       enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
   1206       class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
   1207       enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
   1208   inline _LIBCPP_INLINE_VISIBILITY
   1209   _Tp& emplace(_Args&&... __args) {
   1210     return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
   1211   }
   1212 
   1213   template <
   1214       size_t _Ip,
   1215       class _Up,
   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, initializer_list<_Up>&, _Args...>,
   1220                   int> = 0>
   1221   inline _LIBCPP_INLINE_VISIBILITY
   1222   _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
   1223     return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
   1224   }
   1225 
   1226   template <
   1227       class _Tp,
   1228       class... _Args,
   1229       size_t _Ip =
   1230           __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
   1231       enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
   1232   inline _LIBCPP_INLINE_VISIBILITY
   1233   _Tp& emplace(_Args&&... __args) {
   1234     return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
   1235   }
   1236 
   1237   template <
   1238       class _Tp,
   1239       class _Up,
   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, initializer_list<_Up>&, _Args...>,
   1244                   int> = 0>
   1245   inline _LIBCPP_INLINE_VISIBILITY
   1246   _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
   1247     return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
   1248   }
   1249 
   1250   inline _LIBCPP_INLINE_VISIBILITY
   1251   constexpr bool valueless_by_exception() const noexcept {
   1252     return __impl.valueless_by_exception();
   1253   }
   1254 
   1255   inline _LIBCPP_INLINE_VISIBILITY
   1256   constexpr size_t index() const noexcept { return __impl.index(); }
   1257 
   1258   template <
   1259       bool _Dummy = true,
   1260       enable_if_t<
   1261           __all<(
   1262               __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
   1263               __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
   1264           int> = 0>
   1265   inline _LIBCPP_INLINE_VISIBILITY
   1266   void swap(variant& __that) noexcept(
   1267       __all<(is_nothrow_move_constructible_v<_Types> &&
   1268              is_nothrow_swappable_v<_Types>)...>::value) {
   1269     __impl.__swap(__that.__impl);
   1270   }
   1271 
   1272 private:
   1273   __variant_detail::__impl<_Types...> __impl;
   1274 
   1275   friend struct __variant_detail::__access::__variant;
   1276   friend struct __variant_detail::__visitation::__variant;
   1277 };
   1278 
   1279 template <size_t _Ip, class... _Types>
   1280 inline _LIBCPP_INLINE_VISIBILITY
   1281 constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
   1282   return __v.index() == _Ip;
   1283 }
   1284 
   1285 template <class _Tp, class... _Types>
   1286 inline _LIBCPP_INLINE_VISIBILITY
   1287 constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
   1288   return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
   1289 }
   1290 
   1291 template <size_t _Ip, class _Vp>
   1292 inline _LIBCPP_INLINE_VISIBILITY
   1293 static constexpr auto&& __generic_get(_Vp&& __v) {
   1294   using __variant_detail::__access::__variant;
   1295   if (!__holds_alternative<_Ip>(__v)) {
   1296     __throw_bad_variant_access();
   1297   }
   1298   return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
   1299 }
   1300 
   1301 template <size_t _Ip, class... _Types>
   1302 inline _LIBCPP_INLINE_VISIBILITY
   1303 constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
   1304     variant<_Types...>& __v) {
   1305   static_assert(_Ip < sizeof...(_Types));
   1306   static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
   1307   return __generic_get<_Ip>(__v);
   1308 }
   1309 
   1310 template <size_t _Ip, class... _Types>
   1311 inline _LIBCPP_INLINE_VISIBILITY
   1312 constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
   1313     variant<_Types...>&& __v) {
   1314   static_assert(_Ip < sizeof...(_Types));
   1315   static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
   1316   return __generic_get<_Ip>(_VSTD::move(__v));
   1317 }
   1318 
   1319 template <size_t _Ip, class... _Types>
   1320 inline _LIBCPP_INLINE_VISIBILITY
   1321 constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
   1322     const variant<_Types...>& __v) {
   1323   static_assert(_Ip < sizeof...(_Types));
   1324   static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
   1325   return __generic_get<_Ip>(__v);
   1326 }
   1327 
   1328 template <size_t _Ip, class... _Types>
   1329 inline _LIBCPP_INLINE_VISIBILITY
   1330 constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
   1331     const variant<_Types...>&& __v) {
   1332   static_assert(_Ip < sizeof...(_Types));
   1333   static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
   1334   return __generic_get<_Ip>(_VSTD::move(__v));
   1335 }
   1336 
   1337 template <class _Tp, class... _Types>
   1338 inline _LIBCPP_INLINE_VISIBILITY
   1339 constexpr _Tp& get(variant<_Types...>& __v) {
   1340   static_assert(!is_void_v<_Tp>);
   1341   return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
   1342 }
   1343 
   1344 template <class _Tp, class... _Types>
   1345 inline _LIBCPP_INLINE_VISIBILITY
   1346 constexpr _Tp&& get(variant<_Types...>&& __v) {
   1347   static_assert(!is_void_v<_Tp>);
   1348   return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
   1349       _VSTD::move(__v));
   1350 }
   1351 
   1352 template <class _Tp, class... _Types>
   1353 inline _LIBCPP_INLINE_VISIBILITY
   1354 constexpr const _Tp& get(const variant<_Types...>& __v) {
   1355   static_assert(!is_void_v<_Tp>);
   1356   return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
   1357 }
   1358 
   1359 template <class _Tp, class... _Types>
   1360 inline _LIBCPP_INLINE_VISIBILITY
   1361 constexpr const _Tp&& get(const variant<_Types...>&& __v) {
   1362   static_assert(!is_void_v<_Tp>);
   1363   return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
   1364       _VSTD::move(__v));
   1365 }
   1366 
   1367 template <size_t _Ip, class _Vp>
   1368 inline _LIBCPP_INLINE_VISIBILITY
   1369 constexpr auto* __generic_get_if(_Vp* __v) noexcept {
   1370   using __variant_detail::__access::__variant;
   1371   return __v && __holds_alternative<_Ip>(*__v)
   1372              ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
   1373              : nullptr;
   1374 }
   1375 
   1376 template <size_t _Ip, class... _Types>
   1377 inline _LIBCPP_INLINE_VISIBILITY
   1378 constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
   1379 get_if(variant<_Types...>* __v) noexcept {
   1380   static_assert(_Ip < sizeof...(_Types));
   1381   static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
   1382   return __generic_get_if<_Ip>(__v);
   1383 }
   1384 
   1385 template <size_t _Ip, class... _Types>
   1386 inline _LIBCPP_INLINE_VISIBILITY
   1387 constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
   1388 get_if(const variant<_Types...>* __v) noexcept {
   1389   static_assert(_Ip < sizeof...(_Types));
   1390   static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
   1391   return __generic_get_if<_Ip>(__v);
   1392 }
   1393 
   1394 template <class _Tp, class... _Types>
   1395 inline _LIBCPP_INLINE_VISIBILITY
   1396 constexpr add_pointer_t<_Tp>
   1397 get_if(variant<_Types...>* __v) noexcept {
   1398   static_assert(!is_void_v<_Tp>);
   1399   return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
   1400 }
   1401 
   1402 template <class _Tp, class... _Types>
   1403 inline _LIBCPP_INLINE_VISIBILITY
   1404 constexpr add_pointer_t<const _Tp>
   1405 get_if(const variant<_Types...>* __v) noexcept {
   1406   static_assert(!is_void_v<_Tp>);
   1407   return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
   1408 }
   1409 
   1410 template <class... _Types>
   1411 inline _LIBCPP_INLINE_VISIBILITY
   1412 constexpr bool operator==(const variant<_Types...>& __lhs,
   1413                           const variant<_Types...>& __rhs) {
   1414   using __variant_detail::__visitation::__variant;
   1415   if (__lhs.index() != __rhs.index()) return false;
   1416   if (__lhs.valueless_by_exception()) return true;
   1417   return __variant::__visit_value_at(__lhs.index(), equal_to<>{}, __lhs, __rhs);
   1418 }
   1419 
   1420 template <class... _Types>
   1421 inline _LIBCPP_INLINE_VISIBILITY
   1422 constexpr bool operator!=(const variant<_Types...>& __lhs,
   1423                           const variant<_Types...>& __rhs) {
   1424   using __variant_detail::__visitation::__variant;
   1425   if (__lhs.index() != __rhs.index()) return true;
   1426   if (__lhs.valueless_by_exception()) return false;
   1427   return __variant::__visit_value_at(
   1428       __lhs.index(), not_equal_to<>{}, __lhs, __rhs);
   1429 }
   1430 
   1431 template <class... _Types>
   1432 inline _LIBCPP_INLINE_VISIBILITY
   1433 constexpr bool operator<(const variant<_Types...>& __lhs,
   1434                          const variant<_Types...>& __rhs) {
   1435   using __variant_detail::__visitation::__variant;
   1436   if (__rhs.valueless_by_exception()) return false;
   1437   if (__lhs.valueless_by_exception()) return true;
   1438   if (__lhs.index() < __rhs.index()) return true;
   1439   if (__lhs.index() > __rhs.index()) return false;
   1440   return __variant::__visit_value_at(__lhs.index(), less<>{}, __lhs, __rhs);
   1441 }
   1442 
   1443 template <class... _Types>
   1444 inline _LIBCPP_INLINE_VISIBILITY
   1445 constexpr bool operator>(const variant<_Types...>& __lhs,
   1446                          const variant<_Types...>& __rhs) {
   1447   using __variant_detail::__visitation::__variant;
   1448   if (__lhs.valueless_by_exception()) return false;
   1449   if (__rhs.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(), greater<>{}, __lhs, __rhs);
   1453 }
   1454 
   1455 template <class... _Types>
   1456 inline _LIBCPP_INLINE_VISIBILITY
   1457 constexpr bool operator<=(const variant<_Types...>& __lhs,
   1458                           const variant<_Types...>& __rhs) {
   1459   using __variant_detail::__visitation::__variant;
   1460   if (__lhs.valueless_by_exception()) return true;
   1461   if (__rhs.valueless_by_exception()) return false;
   1462   if (__lhs.index() < __rhs.index()) return true;
   1463   if (__lhs.index() > __rhs.index()) return false;
   1464   return __variant::__visit_value_at(
   1465       __lhs.index(), less_equal<>{}, __lhs, __rhs);
   1466 }
   1467 
   1468 template <class... _Types>
   1469 inline _LIBCPP_INLINE_VISIBILITY
   1470 constexpr bool operator>=(const variant<_Types...>& __lhs,
   1471                           const variant<_Types...>& __rhs) {
   1472   using __variant_detail::__visitation::__variant;
   1473   if (__rhs.valueless_by_exception()) return true;
   1474   if (__lhs.valueless_by_exception()) return false;
   1475   if (__lhs.index() > __rhs.index()) return true;
   1476   if (__lhs.index() < __rhs.index()) return false;
   1477   return __variant::__visit_value_at(
   1478       __lhs.index(), greater_equal<>{}, __lhs, __rhs);
   1479 }
   1480 
   1481 template <class _Visitor, class... _Vs>
   1482 inline _LIBCPP_INLINE_VISIBILITY
   1483 constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
   1484   using __variant_detail::__visitation::__variant;
   1485   bool __results[] = {__vs.valueless_by_exception()...};
   1486   for (bool __result : __results) {
   1487     if (__result) {
   1488       __throw_bad_variant_access();
   1489     }
   1490   }
   1491   return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
   1492                                   _VSTD::forward<_Vs>(__vs)...);
   1493 }
   1494 
   1495 struct _LIBCPP_TEMPLATE_VIS monostate {};
   1496 
   1497 inline _LIBCPP_INLINE_VISIBILITY
   1498 constexpr bool operator<(monostate, monostate) noexcept { return false; }
   1499 
   1500 inline _LIBCPP_INLINE_VISIBILITY
   1501 constexpr bool operator>(monostate, monostate) noexcept { return false; }
   1502 
   1503 inline _LIBCPP_INLINE_VISIBILITY
   1504 constexpr bool operator<=(monostate, monostate) noexcept { return true; }
   1505 
   1506 inline _LIBCPP_INLINE_VISIBILITY
   1507 constexpr bool operator>=(monostate, monostate) noexcept { return true; }
   1508 
   1509 inline _LIBCPP_INLINE_VISIBILITY
   1510 constexpr bool operator==(monostate, monostate) noexcept { return true; }
   1511 
   1512 inline _LIBCPP_INLINE_VISIBILITY
   1513 constexpr bool operator!=(monostate, monostate) noexcept { return false; }
   1514 
   1515 template <class... _Types>
   1516 inline _LIBCPP_INLINE_VISIBILITY
   1517 auto swap(variant<_Types...>& __lhs,
   1518           variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
   1519     -> decltype(__lhs.swap(__rhs)) {
   1520   __lhs.swap(__rhs);
   1521 }
   1522 
   1523 template <class... _Types>
   1524 struct _LIBCPP_TEMPLATE_VIS hash<
   1525     __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
   1526   using argument_type = variant<_Types...>;
   1527   using result_type = size_t;
   1528 
   1529   inline _LIBCPP_INLINE_VISIBILITY
   1530   result_type operator()(const argument_type& __v) const {
   1531     using __variant_detail::__visitation::__variant;
   1532     size_t __res =
   1533         __v.valueless_by_exception()
   1534                ? 299792458 // Random value chosen by the universe upon creation
   1535                : __variant::__visit_alt(
   1536                      [](const auto& __alt) {
   1537                        using __alt_type = decay_t<decltype(__alt)>;
   1538                        using __value_type = remove_const_t<
   1539                          typename __alt_type::__value_type>;
   1540                        return hash<__value_type>{}(__alt.__value);
   1541                      },
   1542                      __v);
   1543     return __hash_combine(__res, hash<size_t>{}(__v.index()));
   1544   }
   1545 };
   1546 
   1547 template <>
   1548 struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
   1549   using argument_type = monostate;
   1550   using result_type = size_t;
   1551 
   1552   inline _LIBCPP_INLINE_VISIBILITY
   1553   result_type operator()(const argument_type&) const _NOEXCEPT {
   1554     return 66740831; // return a fundamentally attractive random value.
   1555   }
   1556 };
   1557 
   1558 #endif  // _LIBCPP_STD_VER > 14
   1559 
   1560 _LIBCPP_END_NAMESPACE_STD
   1561 
   1562 #endif  // _LIBCPP_VARIANT
   1563