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