blob: 45efc78ed19f51e59934e0321e993828fe6d1bab [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)>;
Eric Fiselier2adf0872016-12-02 23:17:33 +0000456 return __result{{_VSTD::forward<_Fs>(__fs)...}};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000457 }
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
Eric Fiselier2adf0872016-12-02 23:17:33 +0000648 : __data(tag), __index(__variant_npos) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000649
650 template <size_t _Ip, class... _Args>
651 inline _LIBCPP_INLINE_VISIBILITY
652 explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
Eric Fiselier2adf0872016-12-02 23:17:33 +0000653 :
654 __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
655 __index(_Ip) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000656
657 inline _LIBCPP_INLINE_VISIBILITY
658 constexpr bool valueless_by_exception() const noexcept {
659 return index() == variant_npos;
660 }
661
662 inline _LIBCPP_INLINE_VISIBILITY
663 constexpr size_t index() const noexcept {
664 return __index == __variant_npos ? variant_npos : __index;
665 }
666
667protected:
668 inline _LIBCPP_INLINE_VISIBILITY
669 constexpr auto&& __as_base() & { return *this; }
670
671 inline _LIBCPP_INLINE_VISIBILITY
672 constexpr auto&& __as_base() && { return _VSTD::move(*this); }
673
674 inline _LIBCPP_INLINE_VISIBILITY
675 constexpr auto&& __as_base() const & { return *this; }
676
677 inline _LIBCPP_INLINE_VISIBILITY
678 constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
679
680 inline _LIBCPP_INLINE_VISIBILITY
681 static constexpr size_t __size() { return sizeof...(_Types); }
682
683 __union<_DestructibleTrait, 0, _Types...> __data;
684 unsigned int __index;
685
686 friend struct __access::__base;
687 friend struct __visitation::__base;
688};
689
690template <class _Traits, _Trait = _Traits::__destructible_trait>
691class _LIBCPP_TYPE_VIS_ONLY __destructor;
692
693#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \
694 template <class... _Types> \
695 class _LIBCPP_TYPE_VIS_ONLY __destructor<__traits<_Types...>, \
696 destructible_trait> \
697 : public __base<destructible_trait, _Types...> { \
698 using __base_type = __base<destructible_trait, _Types...>; \
699 \
700 public: \
701 using __base_type::__base_type; \
702 using __base_type::operator=; \
703 \
704 __destructor(const __destructor&) = default; \
705 __destructor(__destructor&&) = default; \
706 destructor \
707 __destructor& operator=(const __destructor&) = default; \
708 __destructor& operator=(__destructor&&) = default; \
709 \
710 protected: \
711 inline _LIBCPP_INLINE_VISIBILITY \
712 destroy \
713 }
714
715_LIBCPP_VARIANT_DESTRUCTOR(
716 _Trait::_TriviallyAvailable,
717 ~__destructor() = default;,
718 void __destroy() noexcept { this->__index = __variant_npos; });
719
720_LIBCPP_VARIANT_DESTRUCTOR(
721 _Trait::_Available,
722 ~__destructor() { __destroy(); },
723 void __destroy() noexcept {
724 if (!this->valueless_by_exception()) {
725 __visitation::__base::__visit_alt(
726 [](auto& __alt) noexcept {
727 using __alt_type = decay_t<decltype(__alt)>;
728 __alt.~__alt_type();
729 },
730 *this);
731 }
732 this->__index = __variant_npos;
733 });
734
735_LIBCPP_VARIANT_DESTRUCTOR(
736 _Trait::_Unavailable,
737 ~__destructor() = delete;,
738 void __destroy() noexcept = delete;);
739
740#undef _LIBCPP_VARIANT_DESTRUCTOR
741
742template <class _Traits>
743class _LIBCPP_TYPE_VIS_ONLY __constructor : public __destructor<_Traits> {
744 using __base_type = __destructor<_Traits>;
745
746public:
747 using __base_type::__base_type;
748 using __base_type::operator=;
749
750protected:
751 template <size_t _Ip, class _Tp, class... _Args>
752 inline _LIBCPP_INLINE_VISIBILITY
753 static void __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
754 ::new (_VSTD::addressof(__a))
755 __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
756 }
757
758 template <class _Rhs>
759 inline _LIBCPP_INLINE_VISIBILITY
760 static void __generic_construct(__constructor& __lhs, _Rhs&& __rhs) {
761 __lhs.__destroy();
762 if (!__rhs.valueless_by_exception()) {
763 __visitation::__base::__visit_alt_at(
764 __rhs.index(),
765 [](auto& __lhs_alt, auto&& __rhs_alt) {
766 __construct_alt(
767 __lhs_alt,
768 _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
769 },
770 __lhs, _VSTD::forward<_Rhs>(__rhs));
771 __lhs.__index = __rhs.index();
772 }
773 }
774};
775
776template <class _Traits, _Trait = _Traits::__move_constructible_trait>
777class _LIBCPP_TYPE_VIS_ONLY __move_constructor;
778
779#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \
780 move_constructor) \
781 template <class... _Types> \
782 class _LIBCPP_TYPE_VIS_ONLY __move_constructor<__traits<_Types...>, \
783 move_constructible_trait> \
784 : public __constructor<__traits<_Types...>> { \
785 using __base_type = __constructor<__traits<_Types...>>; \
786 \
787 public: \
788 using __base_type::__base_type; \
789 using __base_type::operator=; \
790 \
791 __move_constructor(const __move_constructor&) = default; \
792 move_constructor \
793 ~__move_constructor() = default; \
794 __move_constructor& operator=(const __move_constructor&) = default; \
795 __move_constructor& operator=(__move_constructor&&) = default; \
796 }
797
798_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
799 _Trait::_TriviallyAvailable,
800 __move_constructor(__move_constructor&& __that) = default;);
801
802_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
803 _Trait::_Available,
804 __move_constructor(__move_constructor&& __that) noexcept(
805 __all<is_nothrow_move_constructible_v<_Types>...>::value)
806 : __move_constructor(__valueless_t{}) {
807 this->__generic_construct(*this, _VSTD::move(__that));
808 });
809
810_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
811 _Trait::_Unavailable,
812 __move_constructor(__move_constructor&&) = delete;);
813
814#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
815
816template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
817class _LIBCPP_TYPE_VIS_ONLY __copy_constructor;
818
819#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \
820 copy_constructor) \
821 template <class... _Types> \
822 class _LIBCPP_TYPE_VIS_ONLY __copy_constructor<__traits<_Types...>, \
823 copy_constructible_trait> \
824 : public __move_constructor<__traits<_Types...>> { \
825 using __base_type = __move_constructor<__traits<_Types...>>; \
826 \
827 public: \
828 using __base_type::__base_type; \
829 using __base_type::operator=; \
830 \
831 copy_constructor \
832 __copy_constructor(__copy_constructor&&) = default; \
833 ~__copy_constructor() = default; \
834 __copy_constructor& operator=(const __copy_constructor&) = default; \
835 __copy_constructor& operator=(__copy_constructor&&) = default; \
836 }
837
838_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
839 _Trait::_TriviallyAvailable,
840 __copy_constructor(const __copy_constructor& __that) = default;);
841
842_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
843 _Trait::_Available,
844 __copy_constructor(const __copy_constructor& __that)
845 : __copy_constructor(__valueless_t{}) {
846 this->__generic_construct(*this, __that);
847 });
848
849_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
850 _Trait::_Unavailable,
851 __copy_constructor(const __copy_constructor&) = delete;);
852
853#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
854
855template <class _Traits>
856class _LIBCPP_TYPE_VIS_ONLY __assignment : public __copy_constructor<_Traits> {
857 using __base_type = __copy_constructor<_Traits>;
858
859public:
860 using __base_type::__base_type;
861 using __base_type::operator=;
862
863 template <size_t _Ip, class... _Args>
864 inline _LIBCPP_INLINE_VISIBILITY
865 void __emplace(_Args&&... __args) {
866 this->__destroy();
867 this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
868 _VSTD::forward<_Args>(__args)...);
869 this->__index = _Ip;
870 }
871
872protected:
873 template <bool _CopyAssign, size_t _Ip, class _Tp, class _Arg>
874 inline _LIBCPP_INLINE_VISIBILITY
875 void __assign_alt(__alt<_Ip, _Tp>& __a,
876 _Arg&& __arg,
877 bool_constant<_CopyAssign> __tag) {
878 if (this->index() == _Ip) {
879 __a.__value = _VSTD::forward<_Arg>(__arg);
880 } else {
881 struct {
882 void operator()(true_type) const {
883 __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
884 }
885 void operator()(false_type) const {
886 __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
887 }
888 __assignment* __this;
889 _Arg&& __arg;
890 } __impl{this, _VSTD::forward<_Arg>(__arg)};
891 __impl(__tag);
892 }
893 }
894
895 template <class _That>
896 inline _LIBCPP_INLINE_VISIBILITY
897 void __generic_assign(_That&& __that) {
898 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
899 // do nothing.
900 } else if (__that.valueless_by_exception()) {
901 this->__destroy();
902 } else {
903 __visitation::__base::__visit_alt_at(
904 __that.index(),
905 [this](auto& __this_alt, auto&& __that_alt) {
906 this->__assign_alt(
907 __this_alt,
908 _VSTD::forward<decltype(__that_alt)>(__that_alt).__value,
909 is_lvalue_reference<_That>{});
910 },
911 *this, _VSTD::forward<_That>(__that));
912 }
913 }
914};
915
916template <class _Traits, _Trait = _Traits::__move_assignable_trait>
917class _LIBCPP_TYPE_VIS_ONLY __move_assignment;
918
919#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \
920 move_assignment) \
921 template <class... _Types> \
922 class _LIBCPP_TYPE_VIS_ONLY __move_assignment<__traits<_Types...>, \
923 move_assignable_trait> \
924 : public __assignment<__traits<_Types...>> { \
925 using __base_type = __assignment<__traits<_Types...>>; \
926 \
927 public: \
928 using __base_type::__base_type; \
929 using __base_type::operator=; \
930 \
931 __move_assignment(const __move_assignment&) = default; \
932 __move_assignment(__move_assignment&&) = default; \
933 ~__move_assignment() = default; \
934 __move_assignment& operator=(const __move_assignment&) = default; \
935 move_assignment \
936 }
937
938_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
939 _Trait::_TriviallyAvailable,
940 __move_assignment& operator=(__move_assignment&& __that) = default;);
941
942_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
943 _Trait::_Available,
944 __move_assignment& operator=(__move_assignment&& __that) noexcept(
945 __all<(is_nothrow_move_constructible_v<_Types> &&
946 is_nothrow_move_assignable_v<_Types>)...>::value) {
947 this->__generic_assign(_VSTD::move(__that));
948 return *this;
949 });
950
951_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
952 _Trait::_Unavailable,
953 __move_assignment& operator=(__move_assignment&&) = delete;);
954
955#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
956
957template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
958class _LIBCPP_TYPE_VIS_ONLY __copy_assignment;
959
960#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \
961 copy_assignment) \
962 template <class... _Types> \
963 class _LIBCPP_TYPE_VIS_ONLY __copy_assignment<__traits<_Types...>, \
964 copy_assignable_trait> \
965 : public __move_assignment<__traits<_Types...>> { \
966 using __base_type = __move_assignment<__traits<_Types...>>; \
967 \
968 public: \
969 using __base_type::__base_type; \
970 using __base_type::operator=; \
971 \
972 __copy_assignment(const __copy_assignment&) = default; \
973 __copy_assignment(__copy_assignment&&) = default; \
974 ~__copy_assignment() = default; \
975 copy_assignment \
976 __copy_assignment& operator=(__copy_assignment&&) = default; \
977 }
978
979_LIBCPP_VARIANT_COPY_ASSIGNMENT(
980 _Trait::_TriviallyAvailable,
981 __copy_assignment& operator=(const __copy_assignment& __that) = default;);
982
983_LIBCPP_VARIANT_COPY_ASSIGNMENT(
984 _Trait::_Available,
985 __copy_assignment& operator=(const __copy_assignment& __that) {
986 this->__generic_assign(__that);
987 return *this;
988 });
989
990_LIBCPP_VARIANT_COPY_ASSIGNMENT(
991 _Trait::_Unavailable,
992 __copy_assignment& operator=(const __copy_assignment&) = delete;);
993
994#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
995
996template <class... _Types>
997class _LIBCPP_TYPE_VIS_ONLY __impl
998 : public __copy_assignment<__traits<_Types...>> {
999 using __base_type = __copy_assignment<__traits<_Types...>>;
1000
1001public:
1002 using __base_type::__base_type;
1003 using __base_type::operator=;
1004
1005 template <size_t _Ip, class _Arg>
1006 inline _LIBCPP_INLINE_VISIBILITY
1007 void __assign(_Arg&& __arg) {
1008 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
1009 _VSTD::forward<_Arg>(__arg),
1010 false_type{});
1011 }
1012
1013 inline _LIBCPP_INLINE_VISIBILITY
1014 void __swap(__impl& __that) {
1015 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1016 // do nothing.
1017 } else if (this->index() == __that.index()) {
1018 __visitation::__base::__visit_alt_at(
1019 this->index(),
1020 [](auto& __this_alt, auto& __that_alt) {
1021 using _VSTD::swap;
1022 swap(__this_alt.__value, __that_alt.__value);
1023 },
1024 *this,
1025 __that);
1026 } else {
1027 __impl* __lhs = this;
1028 __impl* __rhs = _VSTD::addressof(__that);
1029 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1030 _VSTD::swap(__lhs, __rhs);
1031 }
1032 __impl __tmp(_VSTD::move(*__rhs));
1033#ifndef _LIBCPP_NO_EXCEPTIONS
1034 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1035 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1036 // into `__rhs` and provide the strong exception safety guarentee.
1037 try {
1038 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1039 } catch (...) {
1040 if (__tmp.__move_nothrow()) {
1041 this->__generic_construct(*__rhs, _VSTD::move(__tmp));
1042 }
1043 throw;
1044 }
1045#else
1046 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1047#endif
1048 this->__generic_construct(*__lhs, _VSTD::move(__tmp));
1049 }
1050 }
1051
1052private:
1053 inline _LIBCPP_INLINE_VISIBILITY
1054 bool __move_nothrow() const {
1055 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1056 return this->valueless_by_exception() || __results[this->index()];
1057 }
1058};
1059
1060template <class... _Types>
1061struct __overload;
1062
1063template <>
1064struct __overload<> { void operator()() const; };
1065
1066template <class _Tp, class... _Types>
1067struct __overload<_Tp, _Types...> : __overload<_Types...> {
1068 using __overload<_Types...>::operator();
1069 __identity<_Tp> operator()(_Tp) const;
1070};
1071
1072template <class _Tp, class... _Types>
1073using __best_match_t = typename result_of_t<__overload<_Types...>(_Tp&&)>::type;
1074
1075} // __variant_detail
1076
1077template <class... _Types>
1078class _LIBCPP_TYPE_VIS_ONLY variant
1079 : private __sfinae_ctor_base<
1080 __all<is_copy_constructible_v<_Types>...>::value,
1081 __all<is_move_constructible_v<_Types>...>::value>,
1082 private __sfinae_assign_base<
1083 __all<(is_copy_constructible_v<_Types> &&
1084 is_move_constructible_v<_Types> &&
1085 is_copy_assignable_v<_Types>)...>::value,
1086 __all<(is_move_constructible_v<_Types> &&
1087 is_move_assignable_v<_Types>)...>::value> {
1088 static_assert(0 < sizeof...(_Types),
1089 "variant must consist of at least one alternative.");
1090
1091 static_assert(__all<!is_array_v<_Types>...>::value,
1092 "variant can not have an array type as an alternative.");
1093
1094 static_assert(__all<!is_reference_v<_Types>...>::value,
1095 "variant can not have a reference type as an alternative.");
1096
1097 static_assert(__all<!is_void_v<_Types>...>::value,
1098 "variant can not have a void type as an alternative.");
1099
1100 using __first_type = variant_alternative_t<0, variant>;
1101
1102public:
1103 template <bool _Dummy = true,
1104 enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1105 _Dummy>::value,
1106 int> = 0>
1107 inline _LIBCPP_INLINE_VISIBILITY
1108 constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1109 : __impl(in_place_index<0>) {}
1110
1111 variant(const variant&) = default;
1112 variant(variant&&) = default;
1113
1114 template <
1115 class _Arg,
1116 enable_if_t<!is_same_v<decay_t<_Arg>, variant>, int> = 0,
1117 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1118 size_t _Ip =
1119 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1120 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1121 inline _LIBCPP_INLINE_VISIBILITY
1122 constexpr variant(_Arg&& __arg) noexcept(
1123 is_nothrow_constructible_v<_Tp, _Arg>)
1124 : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
1125
1126 template <size_t _Ip, class... _Args,
1127 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1128 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1129 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1130 inline _LIBCPP_INLINE_VISIBILITY
1131 explicit constexpr variant(
1132 in_place_index_t<_Ip>,
1133 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1134 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1135
1136 template <
1137 size_t _Ip,
1138 class _Up,
1139 class... _Args,
1140 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1141 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1142 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1143 int> = 0>
1144 inline _LIBCPP_INLINE_VISIBILITY
1145 explicit constexpr variant(
1146 in_place_index_t<_Ip>,
1147 initializer_list<_Up> __il,
1148 _Args&&... __args) noexcept(
1149 is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1150 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1151
1152 template <
1153 class _Tp,
1154 class... _Args,
1155 size_t _Ip =
1156 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1157 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1158 inline _LIBCPP_INLINE_VISIBILITY
1159 explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1160 is_nothrow_constructible_v<_Tp, _Args...>)
1161 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1162
1163 template <
1164 class _Tp,
1165 class _Up,
1166 class... _Args,
1167 size_t _Ip =
1168 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1169 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1170 int> = 0>
1171 inline _LIBCPP_INLINE_VISIBILITY
1172 explicit constexpr variant(
1173 in_place_type_t<_Tp>,
1174 initializer_list<_Up> __il,
1175 _Args&&... __args) noexcept(
1176 is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1177 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1178
1179 ~variant() = default;
1180
1181 variant& operator=(const variant&) = default;
1182 variant& operator=(variant&&) = default;
1183
1184 template <
1185 class _Arg,
1186 enable_if_t<!is_same_v<decay_t<_Arg>, variant>, int> = 0,
1187 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1188 size_t _Ip =
1189 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1190 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
1191 int> = 0>
1192 inline _LIBCPP_INLINE_VISIBILITY
1193 variant& operator=(_Arg&& __arg) noexcept(
1194 is_nothrow_assignable_v<_Tp&, _Arg> &&
1195 is_nothrow_constructible_v<_Tp, _Arg>) {
1196 __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
1197 return *this;
1198 }
1199
1200 template <
1201 size_t _Ip,
1202 class... _Args,
1203 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1204 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1205 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1206 inline _LIBCPP_INLINE_VISIBILITY
1207 void emplace(_Args&&... __args) {
1208 __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
1209 }
1210
1211 template <
1212 size_t _Ip,
1213 class _Up,
1214 class... _Args,
1215 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1216 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1217 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1218 int> = 0>
1219 inline _LIBCPP_INLINE_VISIBILITY
1220 void emplace(initializer_list<_Up> __il, _Args&&... __args) {
1221 __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
1222 }
1223
1224 template <
1225 class _Tp,
1226 class... _Args,
1227 size_t _Ip =
1228 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1229 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1230 inline _LIBCPP_INLINE_VISIBILITY
1231 void emplace(_Args&&... __args) {
1232 __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
1233 }
1234
1235 template <
1236 class _Tp,
1237 class _Up,
1238 class... _Args,
1239 size_t _Ip =
1240 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1241 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1242 int> = 0>
1243 inline _LIBCPP_INLINE_VISIBILITY
1244 void emplace(initializer_list<_Up> __il, _Args&&... __args) {
1245 __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
1246 }
1247
1248 inline _LIBCPP_INLINE_VISIBILITY
1249 constexpr bool valueless_by_exception() const noexcept {
1250 return __impl.valueless_by_exception();
1251 }
1252
1253 inline _LIBCPP_INLINE_VISIBILITY
1254 constexpr size_t index() const noexcept { return __impl.index(); }
1255
1256 template <
1257 bool _Dummy = true,
1258 enable_if_t<
1259 __all<(
1260 __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1261 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1262 int> = 0>
1263 inline _LIBCPP_INLINE_VISIBILITY
1264 void swap(variant& __that) noexcept(
1265 __all<(is_nothrow_move_constructible_v<_Types> &&
1266 is_nothrow_swappable_v<_Types>)...>::value) {
1267 __impl.__swap(__that.__impl);
1268 }
1269
1270private:
1271 __variant_detail::__impl<_Types...> __impl;
1272
1273 friend struct __variant_detail::__access::__variant;
1274 friend struct __variant_detail::__visitation::__variant;
1275};
1276
1277template <size_t _Ip, class... _Types>
1278inline _LIBCPP_INLINE_VISIBILITY
1279constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1280 return __v.index() == _Ip;
1281}
1282
1283template <class _Tp, class... _Types>
1284inline _LIBCPP_INLINE_VISIBILITY
1285constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1286 return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1287}
1288
1289template <size_t _Ip, class _Vp>
1290inline _LIBCPP_INLINE_VISIBILITY
1291static constexpr auto&& __generic_get(_Vp&& __v) {
1292 using __variant_detail::__access::__variant;
1293 if (!__holds_alternative<_Ip>(__v)) {
1294 throw bad_variant_access{};
1295 }
1296 return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
1297}
1298
1299template <size_t _Ip, class... _Types>
1300inline _LIBCPP_INLINE_VISIBILITY
1301constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
1302 variant<_Types...>& __v) {
1303 static_assert(_Ip < sizeof...(_Types));
1304 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1305 return __generic_get<_Ip>(__v);
1306}
1307
1308template <size_t _Ip, class... _Types>
1309inline _LIBCPP_INLINE_VISIBILITY
1310constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
1311 variant<_Types...>&& __v) {
1312 static_assert(_Ip < sizeof...(_Types));
1313 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1314 return __generic_get<_Ip>(_VSTD::move(__v));
1315}
1316
1317template <size_t _Ip, class... _Types>
1318inline _LIBCPP_INLINE_VISIBILITY
1319constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
1320 const variant<_Types...>& __v) {
1321 static_assert(_Ip < sizeof...(_Types));
1322 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1323 return __generic_get<_Ip>(__v);
1324}
1325
1326template <size_t _Ip, class... _Types>
1327inline _LIBCPP_INLINE_VISIBILITY
1328constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
1329 const variant<_Types...>&& __v) {
1330 static_assert(_Ip < sizeof...(_Types));
1331 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1332 return __generic_get<_Ip>(_VSTD::move(__v));
1333}
1334
1335template <class _Tp, class... _Types>
1336inline _LIBCPP_INLINE_VISIBILITY
1337constexpr _Tp& get(variant<_Types...>& __v) {
1338 static_assert(!is_void_v<_Tp>);
1339 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1340}
1341
1342template <class _Tp, class... _Types>
1343inline _LIBCPP_INLINE_VISIBILITY
1344constexpr _Tp&& get(variant<_Types...>&& __v) {
1345 static_assert(!is_void_v<_Tp>);
1346 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1347 _VSTD::move(__v));
1348}
1349
1350template <class _Tp, class... _Types>
1351inline _LIBCPP_INLINE_VISIBILITY
1352constexpr const _Tp& get(const variant<_Types...>& __v) {
1353 static_assert(!is_void_v<_Tp>);
1354 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1355}
1356
1357template <class _Tp, class... _Types>
1358inline _LIBCPP_INLINE_VISIBILITY
1359constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1360 static_assert(!is_void_v<_Tp>);
1361 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1362 _VSTD::move(__v));
1363}
1364
1365template <size_t _Ip, class _Vp>
1366inline _LIBCPP_INLINE_VISIBILITY
1367constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1368 using __variant_detail::__access::__variant;
1369 return __v && __holds_alternative<_Ip>(*__v)
1370 ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
1371 : nullptr;
1372}
1373
1374template <size_t _Ip, class... _Types>
1375inline _LIBCPP_INLINE_VISIBILITY
1376constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1377get_if(variant<_Types...>* __v) noexcept {
1378 static_assert(_Ip < sizeof...(_Types));
1379 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1380 return __generic_get_if<_Ip>(__v);
1381}
1382
1383template <size_t _Ip, class... _Types>
1384inline _LIBCPP_INLINE_VISIBILITY
1385constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1386get_if(const variant<_Types...>* __v) noexcept {
1387 static_assert(_Ip < sizeof...(_Types));
1388 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1389 return __generic_get_if<_Ip>(__v);
1390}
1391
1392template <class _Tp, class... _Types>
1393inline _LIBCPP_INLINE_VISIBILITY
1394constexpr add_pointer_t<_Tp>
1395get_if(variant<_Types...>* __v) noexcept {
1396 static_assert(!is_void_v<_Tp>);
1397 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1398}
1399
1400template <class _Tp, class... _Types>
1401inline _LIBCPP_INLINE_VISIBILITY
1402constexpr add_pointer_t<const _Tp>
1403get_if(const variant<_Types...>* __v) noexcept {
1404 static_assert(!is_void_v<_Tp>);
1405 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1406}
1407
1408template <class... _Types>
1409inline _LIBCPP_INLINE_VISIBILITY
1410constexpr bool operator==(const variant<_Types...>& __lhs,
1411 const variant<_Types...>& __rhs) {
1412 using __variant_detail::__visitation::__variant;
1413 if (__lhs.index() != __rhs.index()) return false;
1414 if (__lhs.valueless_by_exception()) return true;
1415 return __variant::__visit_value_at(__lhs.index(), equal_to<>{}, __lhs, __rhs);
1416}
1417
1418template <class... _Types>
1419inline _LIBCPP_INLINE_VISIBILITY
1420constexpr bool operator!=(const variant<_Types...>& __lhs,
1421 const variant<_Types...>& __rhs) {
1422 using __variant_detail::__visitation::__variant;
1423 if (__lhs.index() != __rhs.index()) return true;
1424 if (__lhs.valueless_by_exception()) return false;
1425 return __variant::__visit_value_at(
1426 __lhs.index(), not_equal_to<>{}, __lhs, __rhs);
1427}
1428
1429template <class... _Types>
1430inline _LIBCPP_INLINE_VISIBILITY
1431constexpr bool operator<(const variant<_Types...>& __lhs,
1432 const variant<_Types...>& __rhs) {
1433 using __variant_detail::__visitation::__variant;
1434 if (__rhs.valueless_by_exception()) return false;
1435 if (__lhs.valueless_by_exception()) return true;
1436 if (__lhs.index() < __rhs.index()) return true;
1437 if (__lhs.index() > __rhs.index()) return false;
1438 return __variant::__visit_value_at(__lhs.index(), less<>{}, __lhs, __rhs);
1439}
1440
1441template <class... _Types>
1442inline _LIBCPP_INLINE_VISIBILITY
1443constexpr bool operator>(const variant<_Types...>& __lhs,
1444 const variant<_Types...>& __rhs) {
1445 using __variant_detail::__visitation::__variant;
1446 if (__lhs.valueless_by_exception()) return false;
1447 if (__rhs.valueless_by_exception()) return true;
1448 if (__lhs.index() > __rhs.index()) return true;
1449 if (__lhs.index() < __rhs.index()) return false;
1450 return __variant::__visit_value_at(__lhs.index(), greater<>{}, __lhs, __rhs);
1451}
1452
1453template <class... _Types>
1454inline _LIBCPP_INLINE_VISIBILITY
1455constexpr bool operator<=(const variant<_Types...>& __lhs,
1456 const variant<_Types...>& __rhs) {
1457 using __variant_detail::__visitation::__variant;
1458 if (__lhs.valueless_by_exception()) return true;
1459 if (__rhs.valueless_by_exception()) return false;
1460 if (__lhs.index() < __rhs.index()) return true;
1461 if (__lhs.index() > __rhs.index()) return false;
1462 return __variant::__visit_value_at(
1463 __lhs.index(), less_equal<>{}, __lhs, __rhs);
1464}
1465
1466template <class... _Types>
1467inline _LIBCPP_INLINE_VISIBILITY
1468constexpr bool operator>=(const variant<_Types...>& __lhs,
1469 const variant<_Types...>& __rhs) {
1470 using __variant_detail::__visitation::__variant;
1471 if (__rhs.valueless_by_exception()) return true;
1472 if (__lhs.valueless_by_exception()) return false;
1473 if (__lhs.index() > __rhs.index()) return true;
1474 if (__lhs.index() < __rhs.index()) return false;
1475 return __variant::__visit_value_at(
1476 __lhs.index(), greater_equal<>{}, __lhs, __rhs);
1477}
1478
1479template <class _Visitor, class... _Vs>
1480inline _LIBCPP_INLINE_VISIBILITY
1481constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
1482 using __variant_detail::__visitation::__variant;
1483 bool __results[] = {__vs.valueless_by_exception()...};
1484 for (bool __result : __results) {
1485 if (__result) {
1486 throw bad_variant_access{};
1487 }
1488 }
1489 return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
1490 _VSTD::forward<_Vs>(__vs)...);
1491}
1492
1493struct _LIBCPP_TYPE_VIS_ONLY monostate {};
1494
1495inline _LIBCPP_INLINE_VISIBILITY
1496constexpr bool operator<(monostate, monostate) noexcept { return false; }
1497
1498inline _LIBCPP_INLINE_VISIBILITY
1499constexpr bool operator>(monostate, monostate) noexcept { return false; }
1500
1501inline _LIBCPP_INLINE_VISIBILITY
1502constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1503
1504inline _LIBCPP_INLINE_VISIBILITY
1505constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1506
1507inline _LIBCPP_INLINE_VISIBILITY
1508constexpr bool operator==(monostate, monostate) noexcept { return true; }
1509
1510inline _LIBCPP_INLINE_VISIBILITY
1511constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1512
1513template <class... _Types>
1514inline _LIBCPP_INLINE_VISIBILITY
1515auto swap(variant<_Types...>& __lhs,
1516 variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1517 -> decltype(__lhs.swap(__rhs)) {
1518 __lhs.swap(__rhs);
1519}
1520
1521template <class... _Types>
1522struct _LIBCPP_TYPE_VIS_ONLY hash<variant<_Types...>> {
1523 using argument_type = variant<_Types...>;
1524 using result_type = size_t;
1525
1526 inline _LIBCPP_INLINE_VISIBILITY
1527 result_type operator()(const argument_type& __v) const {
1528 using __variant_detail::__visitation::__variant;
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001529 size_t __res =
1530 __v.valueless_by_exception()
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001531 ? __v.index()
1532 : __variant::__visit_alt(
1533 [](const auto& __alt) {
1534 using __alt_type = decay_t<decltype(__alt)>;
1535 using __value_type = typename __alt_type::__value_type;
1536 return hash<__value_type>{}(__alt.__value);
1537 },
1538 __v);
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001539 return __hash_combine(__res, hash<size_t>{}(__v.index()));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001540 }
1541};
1542
1543template <>
1544struct _LIBCPP_TYPE_VIS_ONLY hash<monostate> {
1545 using argument_type = monostate;
1546 using result_type = size_t;
1547
1548 inline _LIBCPP_INLINE_VISIBILITY
1549 result_type operator()(const argument_type&) const { return 0; }
1550};
1551
1552#endif // _LIBCPP_STD_VER > 14
1553
1554_LIBCPP_END_NAMESPACE_STD
1555
1556#endif // _LIBCPP_VARIANT