blob: 13450ddf29ba4772e0e63f2539026c8249f3256d [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>
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000210#include <limits>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000211
212#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
213#pragma GCC system_header
214#endif
215
216namespace std { // explicitly not using versioning namespace
217
218class _LIBCPP_EXCEPTION_ABI bad_variant_access : public exception {
219public:
Saleem Abdulrasool5ee83382016-12-31 17:34:26 +0000220 virtual const char* what() const _NOEXCEPT;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000221};
222
223} // namespace std
224
225_LIBCPP_BEGIN_NAMESPACE_STD
226
227#if _LIBCPP_STD_VER > 14
228
Eric Fiselier10642ea2016-12-03 01:58:07 +0000229_LIBCPP_NORETURN
230inline _LIBCPP_INLINE_VISIBILITY
231void __throw_bad_variant_access() {
232#ifndef _LIBCPP_NO_EXCEPTIONS
233 throw bad_variant_access();
234#else
235 _VSTD::abort();
236#endif
237}
238
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000239template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000240class _LIBCPP_TEMPLATE_VIS variant;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000241
242template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000243struct _LIBCPP_TEMPLATE_VIS variant_size;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000244
245template <class _Tp>
246constexpr size_t variant_size_v = variant_size<_Tp>::value;
247
248template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000249struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000250
251template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000252struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000253
254template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000255struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000256 : variant_size<_Tp> {};
257
258template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000259struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000260 : integral_constant<size_t, sizeof...(_Types)> {};
261
262template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000263struct _LIBCPP_TEMPLATE_VIS variant_alternative;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000264
265template <size_t _Ip, class _Tp>
266using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
267
268template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000269struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000270 : add_const<variant_alternative_t<_Ip, _Tp>> {};
271
272template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000273struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000274 : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
275
276template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000277struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000278 : add_cv<variant_alternative_t<_Ip, _Tp>> {};
279
280template <size_t _Ip, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000281struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
Marshall Clow185577f2017-06-12 16:13:17 +0000282 static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000283 using type = __type_pack_element<_Ip, _Types...>;
284};
285
286constexpr size_t variant_npos = static_cast<size_t>(-1);
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000287
288constexpr int __choose_index_type(unsigned int __num_elem) {
289 if (__num_elem < std::numeric_limits<unsigned char>::max())
290 return 0;
291 if (__num_elem < std::numeric_limits<unsigned short>::max())
292 return 1;
293 return 2;
294}
295
296template <size_t _NumAlts>
297using __variant_index_t =
298#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
299 unsigned int;
300#else
301 std::tuple_element_t<
302 __choose_index_type(_NumAlts),
303 std::tuple<unsigned char, unsigned short, unsigned int>
304 >;
305#endif
306
307template <class _IndexType>
308constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000309
310namespace __find_detail {
311
312template <class _Tp, class... _Types>
313inline _LIBCPP_INLINE_VISIBILITY
314constexpr size_t __find_index() {
315 constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
316 size_t __result = __not_found;
317 for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
318 if (__matches[__i]) {
319 if (__result != __not_found) {
320 return __ambiguous;
321 }
322 __result = __i;
323 }
324 }
325 return __result;
326}
327
328template <size_t _Index>
329struct __find_unambiguous_index_sfinae_impl
330 : integral_constant<size_t, _Index> {};
331
332template <>
333struct __find_unambiguous_index_sfinae_impl<__not_found> {};
334
335template <>
336struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
337
338template <class _Tp, class... _Types>
339struct __find_unambiguous_index_sfinae
340 : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
341
342} // namespace __find_detail
343
344namespace __variant_detail {
345
346struct __valueless_t {};
347
348enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
349
350template <typename _Tp,
351 template <typename> class _IsTriviallyAvailable,
352 template <typename> class _IsAvailable>
353constexpr _Trait __trait =
354 _IsTriviallyAvailable<_Tp>::value
355 ? _Trait::_TriviallyAvailable
356 : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
357
358inline _LIBCPP_INLINE_VISIBILITY
359constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
360 _Trait __result = _Trait::_TriviallyAvailable;
361 for (_Trait __t : __traits) {
362 if (static_cast<int>(__t) > static_cast<int>(__result)) {
363 __result = __t;
364 }
365 }
366 return __result;
367}
368
369template <typename... _Types>
370struct __traits {
371 static constexpr _Trait __copy_constructible_trait =
372 __common_trait({__trait<_Types,
373 is_trivially_copy_constructible,
374 is_copy_constructible>...});
375
376 static constexpr _Trait __move_constructible_trait =
377 __common_trait({__trait<_Types,
378 is_trivially_move_constructible,
379 is_move_constructible>...});
380
381 static constexpr _Trait __copy_assignable_trait = __common_trait(
382 {__copy_constructible_trait,
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000383 __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
384
385 static constexpr _Trait __move_assignable_trait = __common_trait(
386 {__move_constructible_trait,
387 __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
388
389 static constexpr _Trait __destructible_trait = __common_trait(
390 {__trait<_Types, is_trivially_destructible, is_destructible>...});
391};
392
393namespace __access {
394
395struct __union {
396 template <class _Vp>
397 inline _LIBCPP_INLINE_VISIBILITY
398 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
399 return _VSTD::forward<_Vp>(__v).__head;
400 }
401
402 template <class _Vp, size_t _Ip>
403 inline _LIBCPP_INLINE_VISIBILITY
404 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
405 return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
406 }
407};
408
409struct __base {
410 template <size_t _Ip, class _Vp>
411 inline _LIBCPP_INLINE_VISIBILITY
412 static constexpr auto&& __get_alt(_Vp&& __v) {
413 return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
414 in_place_index<_Ip>);
415 }
416};
417
418struct __variant {
419 template <size_t _Ip, class _Vp>
420 inline _LIBCPP_INLINE_VISIBILITY
421 static constexpr auto&& __get_alt(_Vp&& __v) {
422 return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
423 }
424};
425
426} // namespace __access
427
428namespace __visitation {
429
430struct __base {
431 template <class _Visitor, class... _Vs>
432 inline _LIBCPP_INLINE_VISIBILITY
433 static constexpr decltype(auto)
434 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
435 constexpr auto __fdiagonal =
436 __make_fdiagonal<_Visitor&&,
437 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
438 return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
439 _VSTD::forward<_Vs>(__vs).__as_base()...);
440 }
441
442 template <class _Visitor, class... _Vs>
443 inline _LIBCPP_INLINE_VISIBILITY
444 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
445 _Vs&&... __vs) {
446 constexpr auto __fmatrix =
447 __make_fmatrix<_Visitor&&,
448 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
Michael Park07157892017-05-11 07:17:12 +0000449 return __at(__fmatrix, __vs.index()...)(
450 _VSTD::forward<_Visitor>(__visitor),
451 _VSTD::forward<_Vs>(__vs).__as_base()...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000452 }
453
454private:
455 template <class _Tp>
456 inline _LIBCPP_INLINE_VISIBILITY
Michael Park07157892017-05-11 07:17:12 +0000457 static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000458
Michael Park07157892017-05-11 07:17:12 +0000459 template <class _Tp, size_t _Np, typename... _Indices>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000460 inline _LIBCPP_INLINE_VISIBILITY
461 static constexpr auto&& __at(const array<_Tp, _Np>& __elems,
Michael Park07157892017-05-11 07:17:12 +0000462 size_t __index, _Indices... __indices) {
463 return __at(__elems[__index], __indices...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000464 }
465
466 template <class _Fp, class... _Fs>
467 static constexpr void __std_visit_visitor_return_type_check() {
468 static_assert(
469 __all<is_same_v<_Fp, _Fs>...>::value,
470 "`std::visit` requires the visitor to have a single return type.");
471 }
472
473 template <class... _Fs>
474 inline _LIBCPP_INLINE_VISIBILITY
475 static constexpr auto __make_farray(_Fs&&... __fs) {
476 __std_visit_visitor_return_type_check<decay_t<_Fs>...>();
477 using __result = array<common_type_t<decay_t<_Fs>...>, sizeof...(_Fs)>;
Eric Fiselier2adf0872016-12-02 23:17:33 +0000478 return __result{{_VSTD::forward<_Fs>(__fs)...}};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000479 }
480
Michael Park52dc9f02017-01-16 08:14:25 +0000481 template <std::size_t... _Is>
482 struct __dispatcher {
483 template <class _Fp, class... _Vs>
484 inline _LIBCPP_INLINE_VISIBILITY
485 static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000486 return __invoke_constexpr(
487 static_cast<_Fp>(__f),
488 __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
Michael Park52dc9f02017-01-16 08:14:25 +0000489 }
490 };
491
492 template <class _Fp, class... _Vs, size_t... _Is>
493 inline _LIBCPP_INLINE_VISIBILITY
494 static constexpr auto __make_dispatch(index_sequence<_Is...>) {
Eric Fiselier0ae996d2017-02-05 20:36:07 +0000495 return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000496 }
497
498 template <size_t _Ip, class _Fp, class... _Vs>
499 inline _LIBCPP_INLINE_VISIBILITY
500 static constexpr auto __make_fdiagonal_impl() {
501 return __make_dispatch<_Fp, _Vs...>(
502 index_sequence<(__identity<_Vs>{}, _Ip)...>{});
503 }
504
505 template <class _Fp, class... _Vs, size_t... _Is>
506 inline _LIBCPP_INLINE_VISIBILITY
507 static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
508 return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
509 }
510
511 template <class _Fp, class _Vp, class... _Vs>
512 inline _LIBCPP_INLINE_VISIBILITY
513 static constexpr auto __make_fdiagonal() {
514 constexpr size_t _Np = decay_t<_Vp>::__size();
515 static_assert(__all<(_Np == decay_t<_Vs>::__size())...>::value);
516 return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
517 }
518
519 template <class _Fp, class... _Vs, size_t... _Is>
520 inline _LIBCPP_INLINE_VISIBILITY
521 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
522 return __make_dispatch<_Fp, _Vs...>(__is);
523 }
524
525 template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
526 inline _LIBCPP_INLINE_VISIBILITY
527 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
528 index_sequence<_Js...>,
529 _Ls... __ls) {
530 return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
531 index_sequence<_Is..., _Js>{}, __ls...)...);
532 }
533
534 template <class _Fp, class... _Vs>
535 inline _LIBCPP_INLINE_VISIBILITY
536 static constexpr auto __make_fmatrix() {
537 return __make_fmatrix_impl<_Fp, _Vs...>(
538 index_sequence<>{}, make_index_sequence<decay_t<_Vs>::__size()>{}...);
539 }
540};
541
542struct __variant {
543 template <class _Visitor, class... _Vs>
544 inline _LIBCPP_INLINE_VISIBILITY
545 static constexpr decltype(auto)
546 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
547 return __base::__visit_alt_at(__index,
548 _VSTD::forward<_Visitor>(__visitor),
549 _VSTD::forward<_Vs>(__vs).__impl...);
550 }
551
552 template <class _Visitor, class... _Vs>
553 inline _LIBCPP_INLINE_VISIBILITY
554 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
555 _Vs&&... __vs) {
556 return __base::__visit_alt(_VSTD::forward<_Visitor>(__visitor),
557 _VSTD::forward<_Vs>(__vs).__impl...);
558 }
559
560 template <class _Visitor, class... _Vs>
561 inline _LIBCPP_INLINE_VISIBILITY
562 static constexpr decltype(auto)
563 __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
564 return __visit_alt_at(
565 __index,
566 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
567 _VSTD::forward<_Vs>(__vs)...);
568 }
569
570 template <class _Visitor, class... _Vs>
571 inline _LIBCPP_INLINE_VISIBILITY
572 static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
573 _Vs&&... __vs) {
574 return __visit_alt(
575 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
576 _VSTD::forward<_Vs>(__vs)...);
577 }
578
579private:
580 template <class _Visitor, class... _Values>
581 static constexpr void __std_visit_exhaustive_visitor_check() {
582 static_assert(is_callable_v<_Visitor(_Values...)>,
583 "`std::visit` requires the visitor to be exhaustive.");
584 }
585
586 template <class _Visitor>
587 struct __value_visitor {
588 template <class... _Alts>
589 inline _LIBCPP_INLINE_VISIBILITY
590 constexpr decltype(auto) operator()(_Alts&&... __alts) const {
591 __std_visit_exhaustive_visitor_check<
592 _Visitor,
Eric Fiselier060c1fe2017-02-09 19:01:22 +0000593 decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000594 return __invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
595 _VSTD::forward<_Alts>(__alts).__value...);
596 }
597 _Visitor&& __visitor;
598 };
599
600 template <class _Visitor>
601 inline _LIBCPP_INLINE_VISIBILITY
602 static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
603 return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
604 }
605};
606
607} // namespace __visitation
608
609template <size_t _Index, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000610struct _LIBCPP_TEMPLATE_VIS __alt {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000611 using __value_type = _Tp;
612
613 template <class... _Args>
614 inline _LIBCPP_INLINE_VISIBILITY
615 explicit constexpr __alt(in_place_t, _Args&&... __args)
616 : __value(_VSTD::forward<_Args>(__args)...) {}
617
618 __value_type __value;
619};
620
621template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000622union _LIBCPP_TEMPLATE_VIS __union;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000623
624template <_Trait _DestructibleTrait, size_t _Index>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000625union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000626
627#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \
628 template <size_t _Index, class _Tp, class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000629 union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000630 _Index, \
631 _Tp, \
632 _Types...> { \
633 public: \
634 inline _LIBCPP_INLINE_VISIBILITY \
635 explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
636 \
637 template <class... _Args> \
638 inline _LIBCPP_INLINE_VISIBILITY \
639 explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
640 : __head(in_place, _VSTD::forward<_Args>(__args)...) {} \
641 \
642 template <size_t _Ip, class... _Args> \
643 inline _LIBCPP_INLINE_VISIBILITY \
644 explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
645 : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
646 \
647 __union(const __union&) = default; \
648 __union(__union&&) = default; \
649 \
650 destructor \
651 \
652 __union& operator=(const __union&) = default; \
653 __union& operator=(__union&&) = default; \
654 \
655 private: \
656 char __dummy; \
657 __alt<_Index, _Tp> __head; \
658 __union<destructible_trait, _Index + 1, _Types...> __tail; \
659 \
660 friend struct __access::__union; \
661 }
662
663_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
664_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
665_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
666
667#undef _LIBCPP_VARIANT_UNION
668
669template <_Trait _DestructibleTrait, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000670class _LIBCPP_TEMPLATE_VIS __base {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000671public:
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000672 using __index_t = __variant_index_t<sizeof...(_Types)>;
673
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000674 inline _LIBCPP_INLINE_VISIBILITY
675 explicit constexpr __base(__valueless_t tag) noexcept
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000676 : __data(tag), __index(__variant_npos<__index_t>) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000677
678 template <size_t _Ip, class... _Args>
679 inline _LIBCPP_INLINE_VISIBILITY
680 explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
Eric Fiselier2adf0872016-12-02 23:17:33 +0000681 :
682 __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
683 __index(_Ip) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000684
685 inline _LIBCPP_INLINE_VISIBILITY
686 constexpr bool valueless_by_exception() const noexcept {
687 return index() == variant_npos;
688 }
689
690 inline _LIBCPP_INLINE_VISIBILITY
691 constexpr size_t index() const noexcept {
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000692 return __index == __variant_npos<__index_t> ? variant_npos : __index;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000693 }
694
695protected:
696 inline _LIBCPP_INLINE_VISIBILITY
697 constexpr auto&& __as_base() & { return *this; }
698
699 inline _LIBCPP_INLINE_VISIBILITY
700 constexpr auto&& __as_base() && { return _VSTD::move(*this); }
701
702 inline _LIBCPP_INLINE_VISIBILITY
703 constexpr auto&& __as_base() const & { return *this; }
704
705 inline _LIBCPP_INLINE_VISIBILITY
706 constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
707
708 inline _LIBCPP_INLINE_VISIBILITY
709 static constexpr size_t __size() { return sizeof...(_Types); }
710
711 __union<_DestructibleTrait, 0, _Types...> __data;
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000712 __index_t __index;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000713
714 friend struct __access::__base;
715 friend struct __visitation::__base;
716};
717
718template <class _Traits, _Trait = _Traits::__destructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000719class _LIBCPP_TEMPLATE_VIS __destructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000720
721#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \
722 template <class... _Types> \
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000723 class _LIBCPP_TEMPLATE_VIS __destructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000724 destructible_trait> \
725 : public __base<destructible_trait, _Types...> { \
726 using __base_type = __base<destructible_trait, _Types...>; \
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000727 using __index_t = typename __base_type::__index_t; \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000728 \
729 public: \
730 using __base_type::__base_type; \
731 using __base_type::operator=; \
732 \
733 __destructor(const __destructor&) = default; \
734 __destructor(__destructor&&) = default; \
735 destructor \
736 __destructor& operator=(const __destructor&) = default; \
737 __destructor& operator=(__destructor&&) = default; \
738 \
739 protected: \
740 inline _LIBCPP_INLINE_VISIBILITY \
741 destroy \
742 }
743
744_LIBCPP_VARIANT_DESTRUCTOR(
745 _Trait::_TriviallyAvailable,
746 ~__destructor() = default;,
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000747 void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000748
749_LIBCPP_VARIANT_DESTRUCTOR(
750 _Trait::_Available,
751 ~__destructor() { __destroy(); },
752 void __destroy() noexcept {
753 if (!this->valueless_by_exception()) {
754 __visitation::__base::__visit_alt(
755 [](auto& __alt) noexcept {
756 using __alt_type = decay_t<decltype(__alt)>;
757 __alt.~__alt_type();
758 },
759 *this);
760 }
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000761 this->__index = __variant_npos<__index_t>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000762 });
763
764_LIBCPP_VARIANT_DESTRUCTOR(
765 _Trait::_Unavailable,
766 ~__destructor() = delete;,
767 void __destroy() noexcept = delete;);
768
769#undef _LIBCPP_VARIANT_DESTRUCTOR
770
771template <class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000772class _LIBCPP_TEMPLATE_VIS __constructor : public __destructor<_Traits> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000773 using __base_type = __destructor<_Traits>;
774
775public:
776 using __base_type::__base_type;
777 using __base_type::operator=;
778
779protected:
780 template <size_t _Ip, class _Tp, class... _Args>
781 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +0000782 static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
783 ::new ((void*)_VSTD::addressof(__a))
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000784 __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
Eric Fiselier62928442017-04-15 19:32:02 +0000785 return __a.__value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000786 }
787
788 template <class _Rhs>
789 inline _LIBCPP_INLINE_VISIBILITY
790 static void __generic_construct(__constructor& __lhs, _Rhs&& __rhs) {
791 __lhs.__destroy();
792 if (!__rhs.valueless_by_exception()) {
793 __visitation::__base::__visit_alt_at(
794 __rhs.index(),
795 [](auto& __lhs_alt, auto&& __rhs_alt) {
796 __construct_alt(
797 __lhs_alt,
798 _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
799 },
800 __lhs, _VSTD::forward<_Rhs>(__rhs));
801 __lhs.__index = __rhs.index();
802 }
803 }
804};
805
806template <class _Traits, _Trait = _Traits::__move_constructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000807class _LIBCPP_TEMPLATE_VIS __move_constructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000808
809#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \
810 move_constructor) \
811 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000812 class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000813 move_constructible_trait> \
814 : public __constructor<__traits<_Types...>> { \
815 using __base_type = __constructor<__traits<_Types...>>; \
816 \
817 public: \
818 using __base_type::__base_type; \
819 using __base_type::operator=; \
820 \
821 __move_constructor(const __move_constructor&) = default; \
822 move_constructor \
823 ~__move_constructor() = default; \
824 __move_constructor& operator=(const __move_constructor&) = default; \
825 __move_constructor& operator=(__move_constructor&&) = default; \
826 }
827
828_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
829 _Trait::_TriviallyAvailable,
830 __move_constructor(__move_constructor&& __that) = default;);
831
832_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
833 _Trait::_Available,
834 __move_constructor(__move_constructor&& __that) noexcept(
835 __all<is_nothrow_move_constructible_v<_Types>...>::value)
836 : __move_constructor(__valueless_t{}) {
837 this->__generic_construct(*this, _VSTD::move(__that));
838 });
839
840_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
841 _Trait::_Unavailable,
842 __move_constructor(__move_constructor&&) = delete;);
843
844#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
845
846template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000847class _LIBCPP_TEMPLATE_VIS __copy_constructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000848
849#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \
850 copy_constructor) \
851 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000852 class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000853 copy_constructible_trait> \
854 : public __move_constructor<__traits<_Types...>> { \
855 using __base_type = __move_constructor<__traits<_Types...>>; \
856 \
857 public: \
858 using __base_type::__base_type; \
859 using __base_type::operator=; \
860 \
861 copy_constructor \
862 __copy_constructor(__copy_constructor&&) = default; \
863 ~__copy_constructor() = default; \
864 __copy_constructor& operator=(const __copy_constructor&) = default; \
865 __copy_constructor& operator=(__copy_constructor&&) = default; \
866 }
867
868_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
869 _Trait::_TriviallyAvailable,
870 __copy_constructor(const __copy_constructor& __that) = default;);
871
872_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
873 _Trait::_Available,
874 __copy_constructor(const __copy_constructor& __that)
875 : __copy_constructor(__valueless_t{}) {
876 this->__generic_construct(*this, __that);
877 });
878
879_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
880 _Trait::_Unavailable,
881 __copy_constructor(const __copy_constructor&) = delete;);
882
883#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
884
885template <class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000886class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000887 using __base_type = __copy_constructor<_Traits>;
888
889public:
890 using __base_type::__base_type;
891 using __base_type::operator=;
892
893 template <size_t _Ip, class... _Args>
894 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +0000895 auto& __emplace(_Args&&... __args) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000896 this->__destroy();
Eric Fiselier62928442017-04-15 19:32:02 +0000897 auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000898 _VSTD::forward<_Args>(__args)...);
899 this->__index = _Ip;
Eric Fiselier62928442017-04-15 19:32:02 +0000900 return __res;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000901 }
902
903protected:
Michael Park62118352017-06-07 10:22:43 +0000904 template <size_t _Ip, class _Tp, class _Arg>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000905 inline _LIBCPP_INLINE_VISIBILITY
Michael Park62118352017-06-07 10:22:43 +0000906 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000907 if (this->index() == _Ip) {
908 __a.__value = _VSTD::forward<_Arg>(__arg);
909 } else {
910 struct {
911 void operator()(true_type) const {
Michael Park62118352017-06-07 10:22:43 +0000912 __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000913 }
914 void operator()(false_type) const {
Michael Park62118352017-06-07 10:22:43 +0000915 __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000916 }
917 __assignment* __this;
918 _Arg&& __arg;
919 } __impl{this, _VSTD::forward<_Arg>(__arg)};
Michael Park62118352017-06-07 10:22:43 +0000920 __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
921 !is_nothrow_move_constructible_v<_Tp>>{});
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000922 }
923 }
924
925 template <class _That>
926 inline _LIBCPP_INLINE_VISIBILITY
927 void __generic_assign(_That&& __that) {
928 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
929 // do nothing.
930 } else if (__that.valueless_by_exception()) {
931 this->__destroy();
932 } else {
933 __visitation::__base::__visit_alt_at(
934 __that.index(),
935 [this](auto& __this_alt, auto&& __that_alt) {
936 this->__assign_alt(
937 __this_alt,
Michael Park62118352017-06-07 10:22:43 +0000938 _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000939 },
940 *this, _VSTD::forward<_That>(__that));
941 }
942 }
943};
944
945template <class _Traits, _Trait = _Traits::__move_assignable_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000946class _LIBCPP_TEMPLATE_VIS __move_assignment;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000947
948#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \
949 move_assignment) \
950 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000951 class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000952 move_assignable_trait> \
953 : public __assignment<__traits<_Types...>> { \
954 using __base_type = __assignment<__traits<_Types...>>; \
955 \
956 public: \
957 using __base_type::__base_type; \
958 using __base_type::operator=; \
959 \
960 __move_assignment(const __move_assignment&) = default; \
961 __move_assignment(__move_assignment&&) = default; \
962 ~__move_assignment() = default; \
963 __move_assignment& operator=(const __move_assignment&) = default; \
964 move_assignment \
965 }
966
967_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
968 _Trait::_TriviallyAvailable,
969 __move_assignment& operator=(__move_assignment&& __that) = default;);
970
971_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
972 _Trait::_Available,
973 __move_assignment& operator=(__move_assignment&& __that) noexcept(
974 __all<(is_nothrow_move_constructible_v<_Types> &&
975 is_nothrow_move_assignable_v<_Types>)...>::value) {
976 this->__generic_assign(_VSTD::move(__that));
977 return *this;
978 });
979
980_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
981 _Trait::_Unavailable,
982 __move_assignment& operator=(__move_assignment&&) = delete;);
983
984#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
985
986template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000987class _LIBCPP_TEMPLATE_VIS __copy_assignment;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000988
989#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \
990 copy_assignment) \
991 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000992 class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000993 copy_assignable_trait> \
994 : public __move_assignment<__traits<_Types...>> { \
995 using __base_type = __move_assignment<__traits<_Types...>>; \
996 \
997 public: \
998 using __base_type::__base_type; \
999 using __base_type::operator=; \
1000 \
1001 __copy_assignment(const __copy_assignment&) = default; \
1002 __copy_assignment(__copy_assignment&&) = default; \
1003 ~__copy_assignment() = default; \
1004 copy_assignment \
1005 __copy_assignment& operator=(__copy_assignment&&) = default; \
1006 }
1007
1008_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1009 _Trait::_TriviallyAvailable,
1010 __copy_assignment& operator=(const __copy_assignment& __that) = default;);
1011
1012_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1013 _Trait::_Available,
1014 __copy_assignment& operator=(const __copy_assignment& __that) {
1015 this->__generic_assign(__that);
1016 return *this;
1017 });
1018
1019_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1020 _Trait::_Unavailable,
1021 __copy_assignment& operator=(const __copy_assignment&) = delete;);
1022
1023#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1024
1025template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001026class _LIBCPP_TEMPLATE_VIS __impl
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001027 : public __copy_assignment<__traits<_Types...>> {
1028 using __base_type = __copy_assignment<__traits<_Types...>>;
1029
1030public:
1031 using __base_type::__base_type;
1032 using __base_type::operator=;
1033
1034 template <size_t _Ip, class _Arg>
1035 inline _LIBCPP_INLINE_VISIBILITY
1036 void __assign(_Arg&& __arg) {
1037 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
Michael Park62118352017-06-07 10:22:43 +00001038 _VSTD::forward<_Arg>(__arg));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001039 }
1040
1041 inline _LIBCPP_INLINE_VISIBILITY
1042 void __swap(__impl& __that) {
1043 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1044 // do nothing.
1045 } else if (this->index() == __that.index()) {
1046 __visitation::__base::__visit_alt_at(
1047 this->index(),
1048 [](auto& __this_alt, auto& __that_alt) {
1049 using _VSTD::swap;
1050 swap(__this_alt.__value, __that_alt.__value);
1051 },
1052 *this,
1053 __that);
1054 } else {
1055 __impl* __lhs = this;
1056 __impl* __rhs = _VSTD::addressof(__that);
1057 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1058 _VSTD::swap(__lhs, __rhs);
1059 }
1060 __impl __tmp(_VSTD::move(*__rhs));
1061#ifndef _LIBCPP_NO_EXCEPTIONS
1062 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1063 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1064 // into `__rhs` and provide the strong exception safety guarentee.
1065 try {
1066 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1067 } catch (...) {
1068 if (__tmp.__move_nothrow()) {
1069 this->__generic_construct(*__rhs, _VSTD::move(__tmp));
1070 }
1071 throw;
1072 }
1073#else
1074 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1075#endif
1076 this->__generic_construct(*__lhs, _VSTD::move(__tmp));
1077 }
1078 }
1079
1080private:
1081 inline _LIBCPP_INLINE_VISIBILITY
1082 bool __move_nothrow() const {
1083 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1084 return this->valueless_by_exception() || __results[this->index()];
1085 }
1086};
1087
1088template <class... _Types>
1089struct __overload;
1090
1091template <>
1092struct __overload<> { void operator()() const; };
1093
1094template <class _Tp, class... _Types>
1095struct __overload<_Tp, _Types...> : __overload<_Types...> {
1096 using __overload<_Types...>::operator();
1097 __identity<_Tp> operator()(_Tp) const;
1098};
1099
1100template <class _Tp, class... _Types>
1101using __best_match_t = typename result_of_t<__overload<_Types...>(_Tp&&)>::type;
1102
1103} // __variant_detail
1104
1105template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001106class _LIBCPP_TEMPLATE_VIS variant
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001107 : private __sfinae_ctor_base<
1108 __all<is_copy_constructible_v<_Types>...>::value,
1109 __all<is_move_constructible_v<_Types>...>::value>,
1110 private __sfinae_assign_base<
1111 __all<(is_copy_constructible_v<_Types> &&
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001112 is_copy_assignable_v<_Types>)...>::value,
1113 __all<(is_move_constructible_v<_Types> &&
1114 is_move_assignable_v<_Types>)...>::value> {
1115 static_assert(0 < sizeof...(_Types),
1116 "variant must consist of at least one alternative.");
1117
1118 static_assert(__all<!is_array_v<_Types>...>::value,
1119 "variant can not have an array type as an alternative.");
1120
1121 static_assert(__all<!is_reference_v<_Types>...>::value,
1122 "variant can not have a reference type as an alternative.");
1123
1124 static_assert(__all<!is_void_v<_Types>...>::value,
1125 "variant can not have a void type as an alternative.");
1126
1127 using __first_type = variant_alternative_t<0, variant>;
1128
1129public:
1130 template <bool _Dummy = true,
1131 enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1132 _Dummy>::value,
1133 int> = 0>
1134 inline _LIBCPP_INLINE_VISIBILITY
1135 constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1136 : __impl(in_place_index<0>) {}
1137
1138 variant(const variant&) = default;
1139 variant(variant&&) = default;
1140
1141 template <
1142 class _Arg,
1143 enable_if_t<!is_same_v<decay_t<_Arg>, variant>, int> = 0,
Michael Park0d30fc32017-06-19 08:25:57 +00001144 enable_if_t<!__is_inplace_type<decay_t<_Arg>>::value, int> = 0,
1145 enable_if_t<!__is_inplace_index<decay_t<_Arg>>::value, int> = 0,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001146 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1147 size_t _Ip =
1148 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1149 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1150 inline _LIBCPP_INLINE_VISIBILITY
1151 constexpr variant(_Arg&& __arg) noexcept(
1152 is_nothrow_constructible_v<_Tp, _Arg>)
1153 : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
1154
1155 template <size_t _Ip, class... _Args,
Eric Fiseliered86f1f2017-05-09 00:00:00 +00001156 class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001157 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1158 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1159 inline _LIBCPP_INLINE_VISIBILITY
1160 explicit constexpr variant(
1161 in_place_index_t<_Ip>,
1162 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1163 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1164
1165 template <
1166 size_t _Ip,
1167 class _Up,
1168 class... _Args,
1169 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1170 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
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_index_t<_Ip>,
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 template <
1182 class _Tp,
1183 class... _Args,
1184 size_t _Ip =
1185 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1186 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1187 inline _LIBCPP_INLINE_VISIBILITY
1188 explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1189 is_nothrow_constructible_v<_Tp, _Args...>)
1190 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1191
1192 template <
1193 class _Tp,
1194 class _Up,
1195 class... _Args,
1196 size_t _Ip =
1197 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1198 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1199 int> = 0>
1200 inline _LIBCPP_INLINE_VISIBILITY
1201 explicit constexpr variant(
1202 in_place_type_t<_Tp>,
1203 initializer_list<_Up> __il,
1204 _Args&&... __args) noexcept(
1205 is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1206 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1207
1208 ~variant() = default;
1209
1210 variant& operator=(const variant&) = default;
1211 variant& operator=(variant&&) = default;
1212
1213 template <
1214 class _Arg,
1215 enable_if_t<!is_same_v<decay_t<_Arg>, variant>, int> = 0,
1216 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1217 size_t _Ip =
1218 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1219 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
1220 int> = 0>
1221 inline _LIBCPP_INLINE_VISIBILITY
1222 variant& operator=(_Arg&& __arg) noexcept(
1223 is_nothrow_assignable_v<_Tp&, _Arg> &&
1224 is_nothrow_constructible_v<_Tp, _Arg>) {
1225 __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
1226 return *this;
1227 }
1228
1229 template <
1230 size_t _Ip,
1231 class... _Args,
1232 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1233 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1234 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1235 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001236 _Tp& emplace(_Args&&... __args) {
1237 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001238 }
1239
1240 template <
1241 size_t _Ip,
1242 class _Up,
1243 class... _Args,
1244 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1245 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1246 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1247 int> = 0>
1248 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001249 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1250 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001251 }
1252
1253 template <
1254 class _Tp,
1255 class... _Args,
1256 size_t _Ip =
1257 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1258 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1259 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001260 _Tp& emplace(_Args&&... __args) {
1261 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001262 }
1263
1264 template <
1265 class _Tp,
1266 class _Up,
1267 class... _Args,
1268 size_t _Ip =
1269 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1270 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1271 int> = 0>
1272 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001273 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1274 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001275 }
1276
1277 inline _LIBCPP_INLINE_VISIBILITY
1278 constexpr bool valueless_by_exception() const noexcept {
1279 return __impl.valueless_by_exception();
1280 }
1281
1282 inline _LIBCPP_INLINE_VISIBILITY
1283 constexpr size_t index() const noexcept { return __impl.index(); }
1284
1285 template <
1286 bool _Dummy = true,
1287 enable_if_t<
1288 __all<(
1289 __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1290 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1291 int> = 0>
1292 inline _LIBCPP_INLINE_VISIBILITY
1293 void swap(variant& __that) noexcept(
1294 __all<(is_nothrow_move_constructible_v<_Types> &&
1295 is_nothrow_swappable_v<_Types>)...>::value) {
1296 __impl.__swap(__that.__impl);
1297 }
1298
1299private:
1300 __variant_detail::__impl<_Types...> __impl;
1301
1302 friend struct __variant_detail::__access::__variant;
1303 friend struct __variant_detail::__visitation::__variant;
1304};
1305
1306template <size_t _Ip, class... _Types>
1307inline _LIBCPP_INLINE_VISIBILITY
1308constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1309 return __v.index() == _Ip;
1310}
1311
1312template <class _Tp, class... _Types>
1313inline _LIBCPP_INLINE_VISIBILITY
1314constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1315 return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1316}
1317
1318template <size_t _Ip, class _Vp>
1319inline _LIBCPP_INLINE_VISIBILITY
1320static constexpr auto&& __generic_get(_Vp&& __v) {
1321 using __variant_detail::__access::__variant;
1322 if (!__holds_alternative<_Ip>(__v)) {
Eric Fiselier10642ea2016-12-03 01:58:07 +00001323 __throw_bad_variant_access();
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001324 }
1325 return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
1326}
1327
1328template <size_t _Ip, class... _Types>
1329inline _LIBCPP_INLINE_VISIBILITY
1330constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
1331 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>(__v);
1335}
1336
1337template <size_t _Ip, class... _Types>
1338inline _LIBCPP_INLINE_VISIBILITY
1339constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
1340 variant<_Types...>&& __v) {
1341 static_assert(_Ip < sizeof...(_Types));
1342 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1343 return __generic_get<_Ip>(_VSTD::move(__v));
1344}
1345
1346template <size_t _Ip, class... _Types>
1347inline _LIBCPP_INLINE_VISIBILITY
1348constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
1349 const variant<_Types...>& __v) {
1350 static_assert(_Ip < sizeof...(_Types));
1351 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1352 return __generic_get<_Ip>(__v);
1353}
1354
1355template <size_t _Ip, class... _Types>
1356inline _LIBCPP_INLINE_VISIBILITY
1357constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
1358 const variant<_Types...>&& __v) {
1359 static_assert(_Ip < sizeof...(_Types));
1360 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1361 return __generic_get<_Ip>(_VSTD::move(__v));
1362}
1363
1364template <class _Tp, class... _Types>
1365inline _LIBCPP_INLINE_VISIBILITY
1366constexpr _Tp& get(variant<_Types...>& __v) {
1367 static_assert(!is_void_v<_Tp>);
1368 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1369}
1370
1371template <class _Tp, class... _Types>
1372inline _LIBCPP_INLINE_VISIBILITY
1373constexpr _Tp&& get(variant<_Types...>&& __v) {
1374 static_assert(!is_void_v<_Tp>);
1375 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1376 _VSTD::move(__v));
1377}
1378
1379template <class _Tp, class... _Types>
1380inline _LIBCPP_INLINE_VISIBILITY
1381constexpr const _Tp& get(const variant<_Types...>& __v) {
1382 static_assert(!is_void_v<_Tp>);
1383 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1384}
1385
1386template <class _Tp, class... _Types>
1387inline _LIBCPP_INLINE_VISIBILITY
1388constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1389 static_assert(!is_void_v<_Tp>);
1390 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1391 _VSTD::move(__v));
1392}
1393
1394template <size_t _Ip, class _Vp>
1395inline _LIBCPP_INLINE_VISIBILITY
1396constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1397 using __variant_detail::__access::__variant;
1398 return __v && __holds_alternative<_Ip>(*__v)
1399 ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
1400 : nullptr;
1401}
1402
1403template <size_t _Ip, class... _Types>
1404inline _LIBCPP_INLINE_VISIBILITY
1405constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1406get_if(variant<_Types...>* __v) noexcept {
1407 static_assert(_Ip < sizeof...(_Types));
1408 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1409 return __generic_get_if<_Ip>(__v);
1410}
1411
1412template <size_t _Ip, class... _Types>
1413inline _LIBCPP_INLINE_VISIBILITY
1414constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1415get_if(const variant<_Types...>* __v) noexcept {
1416 static_assert(_Ip < sizeof...(_Types));
1417 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1418 return __generic_get_if<_Ip>(__v);
1419}
1420
1421template <class _Tp, class... _Types>
1422inline _LIBCPP_INLINE_VISIBILITY
1423constexpr add_pointer_t<_Tp>
1424get_if(variant<_Types...>* __v) noexcept {
1425 static_assert(!is_void_v<_Tp>);
1426 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1427}
1428
1429template <class _Tp, class... _Types>
1430inline _LIBCPP_INLINE_VISIBILITY
1431constexpr add_pointer_t<const _Tp>
1432get_if(const variant<_Types...>* __v) noexcept {
1433 static_assert(!is_void_v<_Tp>);
1434 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1435}
1436
1437template <class... _Types>
1438inline _LIBCPP_INLINE_VISIBILITY
1439constexpr bool operator==(const variant<_Types...>& __lhs,
1440 const variant<_Types...>& __rhs) {
1441 using __variant_detail::__visitation::__variant;
1442 if (__lhs.index() != __rhs.index()) return false;
1443 if (__lhs.valueless_by_exception()) return true;
1444 return __variant::__visit_value_at(__lhs.index(), equal_to<>{}, __lhs, __rhs);
1445}
1446
1447template <class... _Types>
1448inline _LIBCPP_INLINE_VISIBILITY
1449constexpr bool operator!=(const variant<_Types...>& __lhs,
1450 const variant<_Types...>& __rhs) {
1451 using __variant_detail::__visitation::__variant;
1452 if (__lhs.index() != __rhs.index()) return true;
1453 if (__lhs.valueless_by_exception()) return false;
1454 return __variant::__visit_value_at(
1455 __lhs.index(), not_equal_to<>{}, __lhs, __rhs);
1456}
1457
1458template <class... _Types>
1459inline _LIBCPP_INLINE_VISIBILITY
1460constexpr bool operator<(const variant<_Types...>& __lhs,
1461 const variant<_Types...>& __rhs) {
1462 using __variant_detail::__visitation::__variant;
1463 if (__rhs.valueless_by_exception()) return false;
1464 if (__lhs.valueless_by_exception()) return true;
1465 if (__lhs.index() < __rhs.index()) return true;
1466 if (__lhs.index() > __rhs.index()) return false;
1467 return __variant::__visit_value_at(__lhs.index(), less<>{}, __lhs, __rhs);
1468}
1469
1470template <class... _Types>
1471inline _LIBCPP_INLINE_VISIBILITY
1472constexpr bool operator>(const variant<_Types...>& __lhs,
1473 const variant<_Types...>& __rhs) {
1474 using __variant_detail::__visitation::__variant;
1475 if (__lhs.valueless_by_exception()) return false;
1476 if (__rhs.valueless_by_exception()) return true;
1477 if (__lhs.index() > __rhs.index()) return true;
1478 if (__lhs.index() < __rhs.index()) return false;
1479 return __variant::__visit_value_at(__lhs.index(), greater<>{}, __lhs, __rhs);
1480}
1481
1482template <class... _Types>
1483inline _LIBCPP_INLINE_VISIBILITY
1484constexpr bool operator<=(const variant<_Types...>& __lhs,
1485 const variant<_Types...>& __rhs) {
1486 using __variant_detail::__visitation::__variant;
1487 if (__lhs.valueless_by_exception()) return true;
1488 if (__rhs.valueless_by_exception()) return false;
1489 if (__lhs.index() < __rhs.index()) return true;
1490 if (__lhs.index() > __rhs.index()) return false;
1491 return __variant::__visit_value_at(
1492 __lhs.index(), less_equal<>{}, __lhs, __rhs);
1493}
1494
1495template <class... _Types>
1496inline _LIBCPP_INLINE_VISIBILITY
1497constexpr bool operator>=(const variant<_Types...>& __lhs,
1498 const variant<_Types...>& __rhs) {
1499 using __variant_detail::__visitation::__variant;
1500 if (__rhs.valueless_by_exception()) return true;
1501 if (__lhs.valueless_by_exception()) return false;
1502 if (__lhs.index() > __rhs.index()) return true;
1503 if (__lhs.index() < __rhs.index()) return false;
1504 return __variant::__visit_value_at(
1505 __lhs.index(), greater_equal<>{}, __lhs, __rhs);
1506}
1507
1508template <class _Visitor, class... _Vs>
1509inline _LIBCPP_INLINE_VISIBILITY
1510constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
1511 using __variant_detail::__visitation::__variant;
1512 bool __results[] = {__vs.valueless_by_exception()...};
1513 for (bool __result : __results) {
1514 if (__result) {
Eric Fiselier10642ea2016-12-03 01:58:07 +00001515 __throw_bad_variant_access();
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001516 }
1517 }
1518 return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
1519 _VSTD::forward<_Vs>(__vs)...);
1520}
1521
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001522struct _LIBCPP_TEMPLATE_VIS monostate {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001523
1524inline _LIBCPP_INLINE_VISIBILITY
1525constexpr bool operator<(monostate, monostate) noexcept { return false; }
1526
1527inline _LIBCPP_INLINE_VISIBILITY
1528constexpr bool operator>(monostate, monostate) noexcept { return false; }
1529
1530inline _LIBCPP_INLINE_VISIBILITY
1531constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1532
1533inline _LIBCPP_INLINE_VISIBILITY
1534constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1535
1536inline _LIBCPP_INLINE_VISIBILITY
1537constexpr bool operator==(monostate, monostate) noexcept { return true; }
1538
1539inline _LIBCPP_INLINE_VISIBILITY
1540constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1541
1542template <class... _Types>
1543inline _LIBCPP_INLINE_VISIBILITY
1544auto swap(variant<_Types...>& __lhs,
1545 variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1546 -> decltype(__lhs.swap(__rhs)) {
1547 __lhs.swap(__rhs);
1548}
1549
1550template <class... _Types>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001551struct _LIBCPP_TEMPLATE_VIS hash<
1552 __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001553 using argument_type = variant<_Types...>;
1554 using result_type = size_t;
1555
1556 inline _LIBCPP_INLINE_VISIBILITY
1557 result_type operator()(const argument_type& __v) const {
1558 using __variant_detail::__visitation::__variant;
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001559 size_t __res =
1560 __v.valueless_by_exception()
Eric Fiselier6b1683c2016-12-04 21:37:37 +00001561 ? 299792458 // Random value chosen by the universe upon creation
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001562 : __variant::__visit_alt(
1563 [](const auto& __alt) {
1564 using __alt_type = decay_t<decltype(__alt)>;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001565 using __value_type = remove_const_t<
1566 typename __alt_type::__value_type>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001567 return hash<__value_type>{}(__alt.__value);
1568 },
1569 __v);
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001570 return __hash_combine(__res, hash<size_t>{}(__v.index()));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001571 }
1572};
1573
1574template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001575struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001576 using argument_type = monostate;
1577 using result_type = size_t;
1578
1579 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001580 result_type operator()(const argument_type&) const _NOEXCEPT {
Eric Fiselier6b1683c2016-12-04 21:37:37 +00001581 return 66740831; // return a fundamentally attractive random value.
1582 }
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001583};
1584
1585#endif // _LIBCPP_STD_VER > 14
1586
1587_LIBCPP_END_NAMESPACE_STD
1588
1589#endif // _LIBCPP_VARIANT