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