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