blob: 8505f3262a186a692ff103f5a6adf49bb1b023d4 [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>
Eric Fiselier62928442017-04-15 19:32:02 +000056 T& emplace(Args&&...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +000057
58 template <class T, class U, class... Args>
Eric Fiselier62928442017-04-15 19:32:02 +000059 T& emplace(initializer_list<U>, Args&&...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +000060
61 template <size_t I, class... Args>
Eric Fiselier62928442017-04-15 19:32:02 +000062 variant_alternative_t<I, variant>& emplace(Args&&...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +000063
64 template <size_t I, class U, class... Args>
Eric Fiselier62928442017-04-15 19:32:02 +000065 variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +000066
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:
Saleem Abdulrasool5ee83382016-12-31 17:34:26 +0000219 virtual const char* what() const _NOEXCEPT;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000220};
221
222} // namespace std
223
224_LIBCPP_BEGIN_NAMESPACE_STD
225
226#if _LIBCPP_STD_VER > 14
227
Eric Fiselier10642ea2016-12-03 01:58:07 +0000228_LIBCPP_NORETURN
229inline _LIBCPP_INLINE_VISIBILITY
230void __throw_bad_variant_access() {
231#ifndef _LIBCPP_NO_EXCEPTIONS
232 throw bad_variant_access();
233#else
234 _VSTD::abort();
235#endif
236}
237
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000238template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000239class _LIBCPP_TEMPLATE_VIS variant;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000240
241template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000242struct _LIBCPP_TEMPLATE_VIS variant_size;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000243
244template <class _Tp>
245constexpr size_t variant_size_v = variant_size<_Tp>::value;
246
247template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000248struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000249
250template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000251struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000252
253template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000254struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000255 : variant_size<_Tp> {};
256
257template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000258struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000259 : integral_constant<size_t, sizeof...(_Types)> {};
260
261template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000262struct _LIBCPP_TEMPLATE_VIS variant_alternative;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000263
264template <size_t _Ip, class _Tp>
265using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
266
267template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000268struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000269 : add_const<variant_alternative_t<_Ip, _Tp>> {};
270
271template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000272struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000273 : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
274
275template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000276struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000277 : add_cv<variant_alternative_t<_Ip, _Tp>> {};
278
279template <size_t _Ip, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000280struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000281 static_assert(_Ip < sizeof...(_Types));
282 using type = __type_pack_element<_Ip, _Types...>;
283};
284
285constexpr size_t variant_npos = static_cast<size_t>(-1);
286constexpr unsigned int __variant_npos = static_cast<unsigned int>(-1);
287
288namespace __find_detail {
289
290template <class _Tp, class... _Types>
291inline _LIBCPP_INLINE_VISIBILITY
292constexpr size_t __find_index() {
293 constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
294 size_t __result = __not_found;
295 for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
296 if (__matches[__i]) {
297 if (__result != __not_found) {
298 return __ambiguous;
299 }
300 __result = __i;
301 }
302 }
303 return __result;
304}
305
306template <size_t _Index>
307struct __find_unambiguous_index_sfinae_impl
308 : integral_constant<size_t, _Index> {};
309
310template <>
311struct __find_unambiguous_index_sfinae_impl<__not_found> {};
312
313template <>
314struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
315
316template <class _Tp, class... _Types>
317struct __find_unambiguous_index_sfinae
318 : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
319
320} // namespace __find_detail
321
322namespace __variant_detail {
323
324struct __valueless_t {};
325
326enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
327
328template <typename _Tp,
329 template <typename> class _IsTriviallyAvailable,
330 template <typename> class _IsAvailable>
331constexpr _Trait __trait =
332 _IsTriviallyAvailable<_Tp>::value
333 ? _Trait::_TriviallyAvailable
334 : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
335
336inline _LIBCPP_INLINE_VISIBILITY
337constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
338 _Trait __result = _Trait::_TriviallyAvailable;
339 for (_Trait __t : __traits) {
340 if (static_cast<int>(__t) > static_cast<int>(__result)) {
341 __result = __t;
342 }
343 }
344 return __result;
345}
346
347template <typename... _Types>
348struct __traits {
349 static constexpr _Trait __copy_constructible_trait =
350 __common_trait({__trait<_Types,
351 is_trivially_copy_constructible,
352 is_copy_constructible>...});
353
354 static constexpr _Trait __move_constructible_trait =
355 __common_trait({__trait<_Types,
356 is_trivially_move_constructible,
357 is_move_constructible>...});
358
359 static constexpr _Trait __copy_assignable_trait = __common_trait(
360 {__copy_constructible_trait,
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000361 __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
362
363 static constexpr _Trait __move_assignable_trait = __common_trait(
364 {__move_constructible_trait,
365 __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
366
367 static constexpr _Trait __destructible_trait = __common_trait(
368 {__trait<_Types, is_trivially_destructible, is_destructible>...});
369};
370
371namespace __access {
372
373struct __union {
374 template <class _Vp>
375 inline _LIBCPP_INLINE_VISIBILITY
376 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
377 return _VSTD::forward<_Vp>(__v).__head;
378 }
379
380 template <class _Vp, size_t _Ip>
381 inline _LIBCPP_INLINE_VISIBILITY
382 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
383 return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
384 }
385};
386
387struct __base {
388 template <size_t _Ip, class _Vp>
389 inline _LIBCPP_INLINE_VISIBILITY
390 static constexpr auto&& __get_alt(_Vp&& __v) {
391 return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
392 in_place_index<_Ip>);
393 }
394};
395
396struct __variant {
397 template <size_t _Ip, class _Vp>
398 inline _LIBCPP_INLINE_VISIBILITY
399 static constexpr auto&& __get_alt(_Vp&& __v) {
400 return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
401 }
402};
403
404} // namespace __access
405
406namespace __visitation {
407
408struct __base {
409 template <class _Visitor, class... _Vs>
410 inline _LIBCPP_INLINE_VISIBILITY
411 static constexpr decltype(auto)
412 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
413 constexpr auto __fdiagonal =
414 __make_fdiagonal<_Visitor&&,
415 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
416 return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
417 _VSTD::forward<_Vs>(__vs).__as_base()...);
418 }
419
420 template <class _Visitor, class... _Vs>
421 inline _LIBCPP_INLINE_VISIBILITY
422 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
423 _Vs&&... __vs) {
424 constexpr auto __fmatrix =
425 __make_fmatrix<_Visitor&&,
426 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
Michael Park07157892017-05-11 07:17:12 +0000427 return __at(__fmatrix, __vs.index()...)(
428 _VSTD::forward<_Visitor>(__visitor),
429 _VSTD::forward<_Vs>(__vs).__as_base()...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000430 }
431
432private:
433 template <class _Tp>
434 inline _LIBCPP_INLINE_VISIBILITY
Michael Park07157892017-05-11 07:17:12 +0000435 static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000436
Michael Park07157892017-05-11 07:17:12 +0000437 template <class _Tp, size_t _Np, typename... _Indices>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000438 inline _LIBCPP_INLINE_VISIBILITY
439 static constexpr auto&& __at(const array<_Tp, _Np>& __elems,
Michael Park07157892017-05-11 07:17:12 +0000440 size_t __index, _Indices... __indices) {
441 return __at(__elems[__index], __indices...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000442 }
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
Michael Park52dc9f02017-01-16 08:14:25 +0000459 template <std::size_t... _Is>
460 struct __dispatcher {
461 template <class _Fp, class... _Vs>
462 inline _LIBCPP_INLINE_VISIBILITY
463 static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000464 return __invoke_constexpr(
465 static_cast<_Fp>(__f),
466 __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
Michael Park52dc9f02017-01-16 08:14:25 +0000467 }
468 };
469
470 template <class _Fp, class... _Vs, size_t... _Is>
471 inline _LIBCPP_INLINE_VISIBILITY
472 static constexpr auto __make_dispatch(index_sequence<_Is...>) {
Eric Fiselier0ae996d2017-02-05 20:36:07 +0000473 return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000474 }
475
476 template <size_t _Ip, class _Fp, class... _Vs>
477 inline _LIBCPP_INLINE_VISIBILITY
478 static constexpr auto __make_fdiagonal_impl() {
479 return __make_dispatch<_Fp, _Vs...>(
480 index_sequence<(__identity<_Vs>{}, _Ip)...>{});
481 }
482
483 template <class _Fp, class... _Vs, size_t... _Is>
484 inline _LIBCPP_INLINE_VISIBILITY
485 static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
486 return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
487 }
488
489 template <class _Fp, class _Vp, class... _Vs>
490 inline _LIBCPP_INLINE_VISIBILITY
491 static constexpr auto __make_fdiagonal() {
492 constexpr size_t _Np = decay_t<_Vp>::__size();
493 static_assert(__all<(_Np == decay_t<_Vs>::__size())...>::value);
494 return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
495 }
496
497 template <class _Fp, class... _Vs, size_t... _Is>
498 inline _LIBCPP_INLINE_VISIBILITY
499 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
500 return __make_dispatch<_Fp, _Vs...>(__is);
501 }
502
503 template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
504 inline _LIBCPP_INLINE_VISIBILITY
505 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
506 index_sequence<_Js...>,
507 _Ls... __ls) {
508 return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
509 index_sequence<_Is..., _Js>{}, __ls...)...);
510 }
511
512 template <class _Fp, class... _Vs>
513 inline _LIBCPP_INLINE_VISIBILITY
514 static constexpr auto __make_fmatrix() {
515 return __make_fmatrix_impl<_Fp, _Vs...>(
516 index_sequence<>{}, make_index_sequence<decay_t<_Vs>::__size()>{}...);
517 }
518};
519
520struct __variant {
521 template <class _Visitor, class... _Vs>
522 inline _LIBCPP_INLINE_VISIBILITY
523 static constexpr decltype(auto)
524 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
525 return __base::__visit_alt_at(__index,
526 _VSTD::forward<_Visitor>(__visitor),
527 _VSTD::forward<_Vs>(__vs).__impl...);
528 }
529
530 template <class _Visitor, class... _Vs>
531 inline _LIBCPP_INLINE_VISIBILITY
532 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
533 _Vs&&... __vs) {
534 return __base::__visit_alt(_VSTD::forward<_Visitor>(__visitor),
535 _VSTD::forward<_Vs>(__vs).__impl...);
536 }
537
538 template <class _Visitor, class... _Vs>
539 inline _LIBCPP_INLINE_VISIBILITY
540 static constexpr decltype(auto)
541 __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
542 return __visit_alt_at(
543 __index,
544 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
545 _VSTD::forward<_Vs>(__vs)...);
546 }
547
548 template <class _Visitor, class... _Vs>
549 inline _LIBCPP_INLINE_VISIBILITY
550 static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
551 _Vs&&... __vs) {
552 return __visit_alt(
553 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
554 _VSTD::forward<_Vs>(__vs)...);
555 }
556
557private:
558 template <class _Visitor, class... _Values>
559 static constexpr void __std_visit_exhaustive_visitor_check() {
560 static_assert(is_callable_v<_Visitor(_Values...)>,
561 "`std::visit` requires the visitor to be exhaustive.");
562 }
563
564 template <class _Visitor>
565 struct __value_visitor {
566 template <class... _Alts>
567 inline _LIBCPP_INLINE_VISIBILITY
568 constexpr decltype(auto) operator()(_Alts&&... __alts) const {
569 __std_visit_exhaustive_visitor_check<
570 _Visitor,
Eric Fiselier060c1fe2017-02-09 19:01:22 +0000571 decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000572 return __invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
573 _VSTD::forward<_Alts>(__alts).__value...);
574 }
575 _Visitor&& __visitor;
576 };
577
578 template <class _Visitor>
579 inline _LIBCPP_INLINE_VISIBILITY
580 static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
581 return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
582 }
583};
584
585} // namespace __visitation
586
587template <size_t _Index, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000588struct _LIBCPP_TEMPLATE_VIS __alt {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000589 using __value_type = _Tp;
590
591 template <class... _Args>
592 inline _LIBCPP_INLINE_VISIBILITY
593 explicit constexpr __alt(in_place_t, _Args&&... __args)
594 : __value(_VSTD::forward<_Args>(__args)...) {}
595
596 __value_type __value;
597};
598
599template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000600union _LIBCPP_TEMPLATE_VIS __union;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000601
602template <_Trait _DestructibleTrait, size_t _Index>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000603union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000604
605#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \
606 template <size_t _Index, class _Tp, class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000607 union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000608 _Index, \
609 _Tp, \
610 _Types...> { \
611 public: \
612 inline _LIBCPP_INLINE_VISIBILITY \
613 explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
614 \
615 template <class... _Args> \
616 inline _LIBCPP_INLINE_VISIBILITY \
617 explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
618 : __head(in_place, _VSTD::forward<_Args>(__args)...) {} \
619 \
620 template <size_t _Ip, class... _Args> \
621 inline _LIBCPP_INLINE_VISIBILITY \
622 explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
623 : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
624 \
625 __union(const __union&) = default; \
626 __union(__union&&) = default; \
627 \
628 destructor \
629 \
630 __union& operator=(const __union&) = default; \
631 __union& operator=(__union&&) = default; \
632 \
633 private: \
634 char __dummy; \
635 __alt<_Index, _Tp> __head; \
636 __union<destructible_trait, _Index + 1, _Types...> __tail; \
637 \
638 friend struct __access::__union; \
639 }
640
641_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
642_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
643_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
644
645#undef _LIBCPP_VARIANT_UNION
646
647template <_Trait _DestructibleTrait, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000648class _LIBCPP_TEMPLATE_VIS __base {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000649public:
650 inline _LIBCPP_INLINE_VISIBILITY
651 explicit constexpr __base(__valueless_t tag) noexcept
Eric Fiselier2adf0872016-12-02 23:17:33 +0000652 : __data(tag), __index(__variant_npos) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000653
654 template <size_t _Ip, class... _Args>
655 inline _LIBCPP_INLINE_VISIBILITY
656 explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
Eric Fiselier2adf0872016-12-02 23:17:33 +0000657 :
658 __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
659 __index(_Ip) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000660
661 inline _LIBCPP_INLINE_VISIBILITY
662 constexpr bool valueless_by_exception() const noexcept {
663 return index() == variant_npos;
664 }
665
666 inline _LIBCPP_INLINE_VISIBILITY
667 constexpr size_t index() const noexcept {
668 return __index == __variant_npos ? variant_npos : __index;
669 }
670
671protected:
672 inline _LIBCPP_INLINE_VISIBILITY
673 constexpr auto&& __as_base() & { return *this; }
674
675 inline _LIBCPP_INLINE_VISIBILITY
676 constexpr auto&& __as_base() && { return _VSTD::move(*this); }
677
678 inline _LIBCPP_INLINE_VISIBILITY
679 constexpr auto&& __as_base() const & { return *this; }
680
681 inline _LIBCPP_INLINE_VISIBILITY
682 constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
683
684 inline _LIBCPP_INLINE_VISIBILITY
685 static constexpr size_t __size() { return sizeof...(_Types); }
686
687 __union<_DestructibleTrait, 0, _Types...> __data;
688 unsigned int __index;
689
690 friend struct __access::__base;
691 friend struct __visitation::__base;
692};
693
694template <class _Traits, _Trait = _Traits::__destructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000695class _LIBCPP_TEMPLATE_VIS __destructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000696
697#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \
698 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000699 class _LIBCPP_TEMPLATE_VIS __destructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000700 destructible_trait> \
701 : public __base<destructible_trait, _Types...> { \
702 using __base_type = __base<destructible_trait, _Types...>; \
703 \
704 public: \
705 using __base_type::__base_type; \
706 using __base_type::operator=; \
707 \
708 __destructor(const __destructor&) = default; \
709 __destructor(__destructor&&) = default; \
710 destructor \
711 __destructor& operator=(const __destructor&) = default; \
712 __destructor& operator=(__destructor&&) = default; \
713 \
714 protected: \
715 inline _LIBCPP_INLINE_VISIBILITY \
716 destroy \
717 }
718
719_LIBCPP_VARIANT_DESTRUCTOR(
720 _Trait::_TriviallyAvailable,
721 ~__destructor() = default;,
722 void __destroy() noexcept { this->__index = __variant_npos; });
723
724_LIBCPP_VARIANT_DESTRUCTOR(
725 _Trait::_Available,
726 ~__destructor() { __destroy(); },
727 void __destroy() noexcept {
728 if (!this->valueless_by_exception()) {
729 __visitation::__base::__visit_alt(
730 [](auto& __alt) noexcept {
731 using __alt_type = decay_t<decltype(__alt)>;
732 __alt.~__alt_type();
733 },
734 *this);
735 }
736 this->__index = __variant_npos;
737 });
738
739_LIBCPP_VARIANT_DESTRUCTOR(
740 _Trait::_Unavailable,
741 ~__destructor() = delete;,
742 void __destroy() noexcept = delete;);
743
744#undef _LIBCPP_VARIANT_DESTRUCTOR
745
746template <class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000747class _LIBCPP_TEMPLATE_VIS __constructor : public __destructor<_Traits> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000748 using __base_type = __destructor<_Traits>;
749
750public:
751 using __base_type::__base_type;
752 using __base_type::operator=;
753
754protected:
755 template <size_t _Ip, class _Tp, class... _Args>
756 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +0000757 static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
758 ::new ((void*)_VSTD::addressof(__a))
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000759 __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
Eric Fiselier62928442017-04-15 19:32:02 +0000760 return __a.__value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000761 }
762
763 template <class _Rhs>
764 inline _LIBCPP_INLINE_VISIBILITY
765 static void __generic_construct(__constructor& __lhs, _Rhs&& __rhs) {
766 __lhs.__destroy();
767 if (!__rhs.valueless_by_exception()) {
768 __visitation::__base::__visit_alt_at(
769 __rhs.index(),
770 [](auto& __lhs_alt, auto&& __rhs_alt) {
771 __construct_alt(
772 __lhs_alt,
773 _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
774 },
775 __lhs, _VSTD::forward<_Rhs>(__rhs));
776 __lhs.__index = __rhs.index();
777 }
778 }
779};
780
781template <class _Traits, _Trait = _Traits::__move_constructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000782class _LIBCPP_TEMPLATE_VIS __move_constructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000783
784#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \
785 move_constructor) \
786 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000787 class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000788 move_constructible_trait> \
789 : public __constructor<__traits<_Types...>> { \
790 using __base_type = __constructor<__traits<_Types...>>; \
791 \
792 public: \
793 using __base_type::__base_type; \
794 using __base_type::operator=; \
795 \
796 __move_constructor(const __move_constructor&) = default; \
797 move_constructor \
798 ~__move_constructor() = default; \
799 __move_constructor& operator=(const __move_constructor&) = default; \
800 __move_constructor& operator=(__move_constructor&&) = default; \
801 }
802
803_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
804 _Trait::_TriviallyAvailable,
805 __move_constructor(__move_constructor&& __that) = default;);
806
807_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
808 _Trait::_Available,
809 __move_constructor(__move_constructor&& __that) noexcept(
810 __all<is_nothrow_move_constructible_v<_Types>...>::value)
811 : __move_constructor(__valueless_t{}) {
812 this->__generic_construct(*this, _VSTD::move(__that));
813 });
814
815_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
816 _Trait::_Unavailable,
817 __move_constructor(__move_constructor&&) = delete;);
818
819#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
820
821template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000822class _LIBCPP_TEMPLATE_VIS __copy_constructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000823
824#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \
825 copy_constructor) \
826 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000827 class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000828 copy_constructible_trait> \
829 : public __move_constructor<__traits<_Types...>> { \
830 using __base_type = __move_constructor<__traits<_Types...>>; \
831 \
832 public: \
833 using __base_type::__base_type; \
834 using __base_type::operator=; \
835 \
836 copy_constructor \
837 __copy_constructor(__copy_constructor&&) = default; \
838 ~__copy_constructor() = default; \
839 __copy_constructor& operator=(const __copy_constructor&) = default; \
840 __copy_constructor& operator=(__copy_constructor&&) = default; \
841 }
842
843_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
844 _Trait::_TriviallyAvailable,
845 __copy_constructor(const __copy_constructor& __that) = default;);
846
847_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
848 _Trait::_Available,
849 __copy_constructor(const __copy_constructor& __that)
850 : __copy_constructor(__valueless_t{}) {
851 this->__generic_construct(*this, __that);
852 });
853
854_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
855 _Trait::_Unavailable,
856 __copy_constructor(const __copy_constructor&) = delete;);
857
858#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
859
860template <class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000861class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000862 using __base_type = __copy_constructor<_Traits>;
863
864public:
865 using __base_type::__base_type;
866 using __base_type::operator=;
867
868 template <size_t _Ip, class... _Args>
869 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +0000870 auto& __emplace(_Args&&... __args) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000871 this->__destroy();
Eric Fiselier62928442017-04-15 19:32:02 +0000872 auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000873 _VSTD::forward<_Args>(__args)...);
874 this->__index = _Ip;
Eric Fiselier62928442017-04-15 19:32:02 +0000875 return __res;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000876 }
877
878protected:
Michael Park62118352017-06-07 10:22:43 +0000879 template <size_t _Ip, class _Tp, class _Arg>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000880 inline _LIBCPP_INLINE_VISIBILITY
Michael Park62118352017-06-07 10:22:43 +0000881 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000882 if (this->index() == _Ip) {
883 __a.__value = _VSTD::forward<_Arg>(__arg);
884 } else {
885 struct {
886 void operator()(true_type) const {
Michael Park62118352017-06-07 10:22:43 +0000887 __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000888 }
889 void operator()(false_type) const {
Michael Park62118352017-06-07 10:22:43 +0000890 __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000891 }
892 __assignment* __this;
893 _Arg&& __arg;
894 } __impl{this, _VSTD::forward<_Arg>(__arg)};
Michael Park62118352017-06-07 10:22:43 +0000895 __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
896 !is_nothrow_move_constructible_v<_Tp>>{});
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000897 }
898 }
899
900 template <class _That>
901 inline _LIBCPP_INLINE_VISIBILITY
902 void __generic_assign(_That&& __that) {
903 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
904 // do nothing.
905 } else if (__that.valueless_by_exception()) {
906 this->__destroy();
907 } else {
908 __visitation::__base::__visit_alt_at(
909 __that.index(),
910 [this](auto& __this_alt, auto&& __that_alt) {
911 this->__assign_alt(
912 __this_alt,
Michael Park62118352017-06-07 10:22:43 +0000913 _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000914 },
915 *this, _VSTD::forward<_That>(__that));
916 }
917 }
918};
919
920template <class _Traits, _Trait = _Traits::__move_assignable_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000921class _LIBCPP_TEMPLATE_VIS __move_assignment;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000922
923#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \
924 move_assignment) \
925 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000926 class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000927 move_assignable_trait> \
928 : public __assignment<__traits<_Types...>> { \
929 using __base_type = __assignment<__traits<_Types...>>; \
930 \
931 public: \
932 using __base_type::__base_type; \
933 using __base_type::operator=; \
934 \
935 __move_assignment(const __move_assignment&) = default; \
936 __move_assignment(__move_assignment&&) = default; \
937 ~__move_assignment() = default; \
938 __move_assignment& operator=(const __move_assignment&) = default; \
939 move_assignment \
940 }
941
942_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
943 _Trait::_TriviallyAvailable,
944 __move_assignment& operator=(__move_assignment&& __that) = default;);
945
946_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
947 _Trait::_Available,
948 __move_assignment& operator=(__move_assignment&& __that) noexcept(
949 __all<(is_nothrow_move_constructible_v<_Types> &&
950 is_nothrow_move_assignable_v<_Types>)...>::value) {
951 this->__generic_assign(_VSTD::move(__that));
952 return *this;
953 });
954
955_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
956 _Trait::_Unavailable,
957 __move_assignment& operator=(__move_assignment&&) = delete;);
958
959#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
960
961template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000962class _LIBCPP_TEMPLATE_VIS __copy_assignment;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000963
964#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \
965 copy_assignment) \
966 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000967 class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000968 copy_assignable_trait> \
969 : public __move_assignment<__traits<_Types...>> { \
970 using __base_type = __move_assignment<__traits<_Types...>>; \
971 \
972 public: \
973 using __base_type::__base_type; \
974 using __base_type::operator=; \
975 \
976 __copy_assignment(const __copy_assignment&) = default; \
977 __copy_assignment(__copy_assignment&&) = default; \
978 ~__copy_assignment() = default; \
979 copy_assignment \
980 __copy_assignment& operator=(__copy_assignment&&) = default; \
981 }
982
983_LIBCPP_VARIANT_COPY_ASSIGNMENT(
984 _Trait::_TriviallyAvailable,
985 __copy_assignment& operator=(const __copy_assignment& __that) = default;);
986
987_LIBCPP_VARIANT_COPY_ASSIGNMENT(
988 _Trait::_Available,
989 __copy_assignment& operator=(const __copy_assignment& __that) {
990 this->__generic_assign(__that);
991 return *this;
992 });
993
994_LIBCPP_VARIANT_COPY_ASSIGNMENT(
995 _Trait::_Unavailable,
996 __copy_assignment& operator=(const __copy_assignment&) = delete;);
997
998#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
999
1000template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001001class _LIBCPP_TEMPLATE_VIS __impl
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001002 : public __copy_assignment<__traits<_Types...>> {
1003 using __base_type = __copy_assignment<__traits<_Types...>>;
1004
1005public:
1006 using __base_type::__base_type;
1007 using __base_type::operator=;
1008
1009 template <size_t _Ip, class _Arg>
1010 inline _LIBCPP_INLINE_VISIBILITY
1011 void __assign(_Arg&& __arg) {
1012 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
Michael Park62118352017-06-07 10:22:43 +00001013 _VSTD::forward<_Arg>(__arg));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001014 }
1015
1016 inline _LIBCPP_INLINE_VISIBILITY
1017 void __swap(__impl& __that) {
1018 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1019 // do nothing.
1020 } else if (this->index() == __that.index()) {
1021 __visitation::__base::__visit_alt_at(
1022 this->index(),
1023 [](auto& __this_alt, auto& __that_alt) {
1024 using _VSTD::swap;
1025 swap(__this_alt.__value, __that_alt.__value);
1026 },
1027 *this,
1028 __that);
1029 } else {
1030 __impl* __lhs = this;
1031 __impl* __rhs = _VSTD::addressof(__that);
1032 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1033 _VSTD::swap(__lhs, __rhs);
1034 }
1035 __impl __tmp(_VSTD::move(*__rhs));
1036#ifndef _LIBCPP_NO_EXCEPTIONS
1037 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1038 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1039 // into `__rhs` and provide the strong exception safety guarentee.
1040 try {
1041 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1042 } catch (...) {
1043 if (__tmp.__move_nothrow()) {
1044 this->__generic_construct(*__rhs, _VSTD::move(__tmp));
1045 }
1046 throw;
1047 }
1048#else
1049 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1050#endif
1051 this->__generic_construct(*__lhs, _VSTD::move(__tmp));
1052 }
1053 }
1054
1055private:
1056 inline _LIBCPP_INLINE_VISIBILITY
1057 bool __move_nothrow() const {
1058 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1059 return this->valueless_by_exception() || __results[this->index()];
1060 }
1061};
1062
1063template <class... _Types>
1064struct __overload;
1065
1066template <>
1067struct __overload<> { void operator()() const; };
1068
1069template <class _Tp, class... _Types>
1070struct __overload<_Tp, _Types...> : __overload<_Types...> {
1071 using __overload<_Types...>::operator();
1072 __identity<_Tp> operator()(_Tp) const;
1073};
1074
1075template <class _Tp, class... _Types>
1076using __best_match_t = typename result_of_t<__overload<_Types...>(_Tp&&)>::type;
1077
1078} // __variant_detail
1079
1080template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001081class _LIBCPP_TEMPLATE_VIS variant
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001082 : private __sfinae_ctor_base<
1083 __all<is_copy_constructible_v<_Types>...>::value,
1084 __all<is_move_constructible_v<_Types>...>::value>,
1085 private __sfinae_assign_base<
1086 __all<(is_copy_constructible_v<_Types> &&
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001087 is_copy_assignable_v<_Types>)...>::value,
1088 __all<(is_move_constructible_v<_Types> &&
1089 is_move_assignable_v<_Types>)...>::value> {
1090 static_assert(0 < sizeof...(_Types),
1091 "variant must consist of at least one alternative.");
1092
1093 static_assert(__all<!is_array_v<_Types>...>::value,
1094 "variant can not have an array type as an alternative.");
1095
1096 static_assert(__all<!is_reference_v<_Types>...>::value,
1097 "variant can not have a reference type as an alternative.");
1098
1099 static_assert(__all<!is_void_v<_Types>...>::value,
1100 "variant can not have a void type as an alternative.");
1101
1102 using __first_type = variant_alternative_t<0, variant>;
1103
1104public:
1105 template <bool _Dummy = true,
1106 enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1107 _Dummy>::value,
1108 int> = 0>
1109 inline _LIBCPP_INLINE_VISIBILITY
1110 constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1111 : __impl(in_place_index<0>) {}
1112
1113 variant(const variant&) = default;
1114 variant(variant&&) = default;
1115
1116 template <
1117 class _Arg,
1118 enable_if_t<!is_same_v<decay_t<_Arg>, variant>, int> = 0,
1119 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1120 size_t _Ip =
1121 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1122 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1123 inline _LIBCPP_INLINE_VISIBILITY
1124 constexpr variant(_Arg&& __arg) noexcept(
1125 is_nothrow_constructible_v<_Tp, _Arg>)
1126 : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
1127
1128 template <size_t _Ip, class... _Args,
Eric Fiseliered86f1f2017-05-09 00:00:00 +00001129 class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001130 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1131 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1132 inline _LIBCPP_INLINE_VISIBILITY
1133 explicit constexpr variant(
1134 in_place_index_t<_Ip>,
1135 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1136 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1137
1138 template <
1139 size_t _Ip,
1140 class _Up,
1141 class... _Args,
1142 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1143 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1144 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1145 int> = 0>
1146 inline _LIBCPP_INLINE_VISIBILITY
1147 explicit constexpr variant(
1148 in_place_index_t<_Ip>,
1149 initializer_list<_Up> __il,
1150 _Args&&... __args) noexcept(
1151 is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1152 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1153
1154 template <
1155 class _Tp,
1156 class... _Args,
1157 size_t _Ip =
1158 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1159 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1160 inline _LIBCPP_INLINE_VISIBILITY
1161 explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1162 is_nothrow_constructible_v<_Tp, _Args...>)
1163 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1164
1165 template <
1166 class _Tp,
1167 class _Up,
1168 class... _Args,
1169 size_t _Ip =
1170 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1171 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1172 int> = 0>
1173 inline _LIBCPP_INLINE_VISIBILITY
1174 explicit constexpr variant(
1175 in_place_type_t<_Tp>,
1176 initializer_list<_Up> __il,
1177 _Args&&... __args) noexcept(
1178 is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1179 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1180
1181 ~variant() = default;
1182
1183 variant& operator=(const variant&) = default;
1184 variant& operator=(variant&&) = default;
1185
1186 template <
1187 class _Arg,
1188 enable_if_t<!is_same_v<decay_t<_Arg>, variant>, int> = 0,
1189 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1190 size_t _Ip =
1191 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1192 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
1193 int> = 0>
1194 inline _LIBCPP_INLINE_VISIBILITY
1195 variant& operator=(_Arg&& __arg) noexcept(
1196 is_nothrow_assignable_v<_Tp&, _Arg> &&
1197 is_nothrow_constructible_v<_Tp, _Arg>) {
1198 __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
1199 return *this;
1200 }
1201
1202 template <
1203 size_t _Ip,
1204 class... _Args,
1205 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1206 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1207 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1208 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001209 _Tp& emplace(_Args&&... __args) {
1210 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001211 }
1212
1213 template <
1214 size_t _Ip,
1215 class _Up,
1216 class... _Args,
1217 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1218 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1219 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1220 int> = 0>
1221 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001222 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1223 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001224 }
1225
1226 template <
1227 class _Tp,
1228 class... _Args,
1229 size_t _Ip =
1230 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1231 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1232 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001233 _Tp& emplace(_Args&&... __args) {
1234 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001235 }
1236
1237 template <
1238 class _Tp,
1239 class _Up,
1240 class... _Args,
1241 size_t _Ip =
1242 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1243 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1244 int> = 0>
1245 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001246 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1247 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001248 }
1249
1250 inline _LIBCPP_INLINE_VISIBILITY
1251 constexpr bool valueless_by_exception() const noexcept {
1252 return __impl.valueless_by_exception();
1253 }
1254
1255 inline _LIBCPP_INLINE_VISIBILITY
1256 constexpr size_t index() const noexcept { return __impl.index(); }
1257
1258 template <
1259 bool _Dummy = true,
1260 enable_if_t<
1261 __all<(
1262 __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1263 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1264 int> = 0>
1265 inline _LIBCPP_INLINE_VISIBILITY
1266 void swap(variant& __that) noexcept(
1267 __all<(is_nothrow_move_constructible_v<_Types> &&
1268 is_nothrow_swappable_v<_Types>)...>::value) {
1269 __impl.__swap(__that.__impl);
1270 }
1271
1272private:
1273 __variant_detail::__impl<_Types...> __impl;
1274
1275 friend struct __variant_detail::__access::__variant;
1276 friend struct __variant_detail::__visitation::__variant;
1277};
1278
1279template <size_t _Ip, class... _Types>
1280inline _LIBCPP_INLINE_VISIBILITY
1281constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1282 return __v.index() == _Ip;
1283}
1284
1285template <class _Tp, class... _Types>
1286inline _LIBCPP_INLINE_VISIBILITY
1287constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1288 return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1289}
1290
1291template <size_t _Ip, class _Vp>
1292inline _LIBCPP_INLINE_VISIBILITY
1293static constexpr auto&& __generic_get(_Vp&& __v) {
1294 using __variant_detail::__access::__variant;
1295 if (!__holds_alternative<_Ip>(__v)) {
Eric Fiselier10642ea2016-12-03 01:58:07 +00001296 __throw_bad_variant_access();
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001297 }
1298 return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
1299}
1300
1301template <size_t _Ip, class... _Types>
1302inline _LIBCPP_INLINE_VISIBILITY
1303constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
1304 variant<_Types...>& __v) {
1305 static_assert(_Ip < sizeof...(_Types));
1306 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1307 return __generic_get<_Ip>(__v);
1308}
1309
1310template <size_t _Ip, class... _Types>
1311inline _LIBCPP_INLINE_VISIBILITY
1312constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
1313 variant<_Types...>&& __v) {
1314 static_assert(_Ip < sizeof...(_Types));
1315 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1316 return __generic_get<_Ip>(_VSTD::move(__v));
1317}
1318
1319template <size_t _Ip, class... _Types>
1320inline _LIBCPP_INLINE_VISIBILITY
1321constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
1322 const variant<_Types...>& __v) {
1323 static_assert(_Ip < sizeof...(_Types));
1324 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1325 return __generic_get<_Ip>(__v);
1326}
1327
1328template <size_t _Ip, class... _Types>
1329inline _LIBCPP_INLINE_VISIBILITY
1330constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
1331 const variant<_Types...>&& __v) {
1332 static_assert(_Ip < sizeof...(_Types));
1333 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1334 return __generic_get<_Ip>(_VSTD::move(__v));
1335}
1336
1337template <class _Tp, class... _Types>
1338inline _LIBCPP_INLINE_VISIBILITY
1339constexpr _Tp& get(variant<_Types...>& __v) {
1340 static_assert(!is_void_v<_Tp>);
1341 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1342}
1343
1344template <class _Tp, class... _Types>
1345inline _LIBCPP_INLINE_VISIBILITY
1346constexpr _Tp&& get(variant<_Types...>&& __v) {
1347 static_assert(!is_void_v<_Tp>);
1348 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1349 _VSTD::move(__v));
1350}
1351
1352template <class _Tp, class... _Types>
1353inline _LIBCPP_INLINE_VISIBILITY
1354constexpr const _Tp& get(const variant<_Types...>& __v) {
1355 static_assert(!is_void_v<_Tp>);
1356 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1357}
1358
1359template <class _Tp, class... _Types>
1360inline _LIBCPP_INLINE_VISIBILITY
1361constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1362 static_assert(!is_void_v<_Tp>);
1363 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1364 _VSTD::move(__v));
1365}
1366
1367template <size_t _Ip, class _Vp>
1368inline _LIBCPP_INLINE_VISIBILITY
1369constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1370 using __variant_detail::__access::__variant;
1371 return __v && __holds_alternative<_Ip>(*__v)
1372 ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
1373 : nullptr;
1374}
1375
1376template <size_t _Ip, class... _Types>
1377inline _LIBCPP_INLINE_VISIBILITY
1378constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1379get_if(variant<_Types...>* __v) noexcept {
1380 static_assert(_Ip < sizeof...(_Types));
1381 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1382 return __generic_get_if<_Ip>(__v);
1383}
1384
1385template <size_t _Ip, class... _Types>
1386inline _LIBCPP_INLINE_VISIBILITY
1387constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1388get_if(const variant<_Types...>* __v) noexcept {
1389 static_assert(_Ip < sizeof...(_Types));
1390 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1391 return __generic_get_if<_Ip>(__v);
1392}
1393
1394template <class _Tp, class... _Types>
1395inline _LIBCPP_INLINE_VISIBILITY
1396constexpr add_pointer_t<_Tp>
1397get_if(variant<_Types...>* __v) noexcept {
1398 static_assert(!is_void_v<_Tp>);
1399 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1400}
1401
1402template <class _Tp, class... _Types>
1403inline _LIBCPP_INLINE_VISIBILITY
1404constexpr add_pointer_t<const _Tp>
1405get_if(const variant<_Types...>* __v) noexcept {
1406 static_assert(!is_void_v<_Tp>);
1407 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1408}
1409
1410template <class... _Types>
1411inline _LIBCPP_INLINE_VISIBILITY
1412constexpr bool operator==(const variant<_Types...>& __lhs,
1413 const variant<_Types...>& __rhs) {
1414 using __variant_detail::__visitation::__variant;
1415 if (__lhs.index() != __rhs.index()) return false;
1416 if (__lhs.valueless_by_exception()) return true;
1417 return __variant::__visit_value_at(__lhs.index(), equal_to<>{}, __lhs, __rhs);
1418}
1419
1420template <class... _Types>
1421inline _LIBCPP_INLINE_VISIBILITY
1422constexpr bool operator!=(const variant<_Types...>& __lhs,
1423 const variant<_Types...>& __rhs) {
1424 using __variant_detail::__visitation::__variant;
1425 if (__lhs.index() != __rhs.index()) return true;
1426 if (__lhs.valueless_by_exception()) return false;
1427 return __variant::__visit_value_at(
1428 __lhs.index(), not_equal_to<>{}, __lhs, __rhs);
1429}
1430
1431template <class... _Types>
1432inline _LIBCPP_INLINE_VISIBILITY
1433constexpr bool operator<(const variant<_Types...>& __lhs,
1434 const variant<_Types...>& __rhs) {
1435 using __variant_detail::__visitation::__variant;
1436 if (__rhs.valueless_by_exception()) return false;
1437 if (__lhs.valueless_by_exception()) return true;
1438 if (__lhs.index() < __rhs.index()) return true;
1439 if (__lhs.index() > __rhs.index()) return false;
1440 return __variant::__visit_value_at(__lhs.index(), less<>{}, __lhs, __rhs);
1441}
1442
1443template <class... _Types>
1444inline _LIBCPP_INLINE_VISIBILITY
1445constexpr bool operator>(const variant<_Types...>& __lhs,
1446 const variant<_Types...>& __rhs) {
1447 using __variant_detail::__visitation::__variant;
1448 if (__lhs.valueless_by_exception()) return false;
1449 if (__rhs.valueless_by_exception()) return true;
1450 if (__lhs.index() > __rhs.index()) return true;
1451 if (__lhs.index() < __rhs.index()) return false;
1452 return __variant::__visit_value_at(__lhs.index(), greater<>{}, __lhs, __rhs);
1453}
1454
1455template <class... _Types>
1456inline _LIBCPP_INLINE_VISIBILITY
1457constexpr bool operator<=(const variant<_Types...>& __lhs,
1458 const variant<_Types...>& __rhs) {
1459 using __variant_detail::__visitation::__variant;
1460 if (__lhs.valueless_by_exception()) return true;
1461 if (__rhs.valueless_by_exception()) return false;
1462 if (__lhs.index() < __rhs.index()) return true;
1463 if (__lhs.index() > __rhs.index()) return false;
1464 return __variant::__visit_value_at(
1465 __lhs.index(), less_equal<>{}, __lhs, __rhs);
1466}
1467
1468template <class... _Types>
1469inline _LIBCPP_INLINE_VISIBILITY
1470constexpr bool operator>=(const variant<_Types...>& __lhs,
1471 const variant<_Types...>& __rhs) {
1472 using __variant_detail::__visitation::__variant;
1473 if (__rhs.valueless_by_exception()) return true;
1474 if (__lhs.valueless_by_exception()) return false;
1475 if (__lhs.index() > __rhs.index()) return true;
1476 if (__lhs.index() < __rhs.index()) return false;
1477 return __variant::__visit_value_at(
1478 __lhs.index(), greater_equal<>{}, __lhs, __rhs);
1479}
1480
1481template <class _Visitor, class... _Vs>
1482inline _LIBCPP_INLINE_VISIBILITY
1483constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
1484 using __variant_detail::__visitation::__variant;
1485 bool __results[] = {__vs.valueless_by_exception()...};
1486 for (bool __result : __results) {
1487 if (__result) {
Eric Fiselier10642ea2016-12-03 01:58:07 +00001488 __throw_bad_variant_access();
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001489 }
1490 }
1491 return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
1492 _VSTD::forward<_Vs>(__vs)...);
1493}
1494
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001495struct _LIBCPP_TEMPLATE_VIS monostate {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001496
1497inline _LIBCPP_INLINE_VISIBILITY
1498constexpr bool operator<(monostate, monostate) noexcept { return false; }
1499
1500inline _LIBCPP_INLINE_VISIBILITY
1501constexpr bool operator>(monostate, monostate) noexcept { return false; }
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 true; }
1511
1512inline _LIBCPP_INLINE_VISIBILITY
1513constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1514
1515template <class... _Types>
1516inline _LIBCPP_INLINE_VISIBILITY
1517auto swap(variant<_Types...>& __lhs,
1518 variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1519 -> decltype(__lhs.swap(__rhs)) {
1520 __lhs.swap(__rhs);
1521}
1522
1523template <class... _Types>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001524struct _LIBCPP_TEMPLATE_VIS hash<
1525 __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001526 using argument_type = variant<_Types...>;
1527 using result_type = size_t;
1528
1529 inline _LIBCPP_INLINE_VISIBILITY
1530 result_type operator()(const argument_type& __v) const {
1531 using __variant_detail::__visitation::__variant;
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001532 size_t __res =
1533 __v.valueless_by_exception()
Eric Fiselier6b1683c2016-12-04 21:37:37 +00001534 ? 299792458 // Random value chosen by the universe upon creation
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001535 : __variant::__visit_alt(
1536 [](const auto& __alt) {
1537 using __alt_type = decay_t<decltype(__alt)>;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001538 using __value_type = remove_const_t<
1539 typename __alt_type::__value_type>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001540 return hash<__value_type>{}(__alt.__value);
1541 },
1542 __v);
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001543 return __hash_combine(__res, hash<size_t>{}(__v.index()));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001544 }
1545};
1546
1547template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001548struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001549 using argument_type = monostate;
1550 using result_type = size_t;
1551
1552 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001553 result_type operator()(const argument_type&) const _NOEXCEPT {
Eric Fiselier6b1683c2016-12-04 21:37:37 +00001554 return 66740831; // return a fundamentally attractive random value.
1555 }
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001556};
1557
1558#endif // _LIBCPP_STD_VER > 14
1559
1560_LIBCPP_END_NAMESPACE_STD
1561
1562#endif // _LIBCPP_VARIANT