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