blob: 6f78e2de1573b7b15a50885fc75bebddbde0d085 [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>
Marshall Clowf1bf62f2018-01-02 17:17:01 +000079 inline constexpr size_t variant_size_v = variant_size<T>::value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +000080
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
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000100 inline constexpr size_t variant_npos = -1;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000101
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
Eric Fiselier8625d332017-11-19 04:57:22 +0000216_LIBCPP_PUSH_MACROS
217#include <__undef_macros>
218
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000219namespace std { // explicitly not using versioning namespace
220
221class _LIBCPP_EXCEPTION_ABI bad_variant_access : public exception {
222public:
Saleem Abdulrasool5ee83382016-12-31 17:34:26 +0000223 virtual const char* what() const _NOEXCEPT;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000224};
225
226} // namespace std
227
228_LIBCPP_BEGIN_NAMESPACE_STD
229
230#if _LIBCPP_STD_VER > 14
231
Eric Fiselier10642ea2016-12-03 01:58:07 +0000232_LIBCPP_NORETURN
233inline _LIBCPP_INLINE_VISIBILITY
234void __throw_bad_variant_access() {
235#ifndef _LIBCPP_NO_EXCEPTIONS
236 throw bad_variant_access();
237#else
238 _VSTD::abort();
239#endif
240}
241
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000242template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000243class _LIBCPP_TEMPLATE_VIS variant;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000244
245template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000246struct _LIBCPP_TEMPLATE_VIS variant_size;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000247
248template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000249_LIBCPP_INLINE_VAR constexpr size_t variant_size_v = variant_size<_Tp>::value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000250
251template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000252struct _LIBCPP_TEMPLATE_VIS variant_size<const _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<volatile _Tp> : variant_size<_Tp> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000256
257template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000258struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000259 : variant_size<_Tp> {};
260
261template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000262struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000263 : integral_constant<size_t, sizeof...(_Types)> {};
264
265template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000266struct _LIBCPP_TEMPLATE_VIS variant_alternative;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000267
268template <size_t _Ip, class _Tp>
269using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
270
271template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000272struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000273 : add_const<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, volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000277 : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
278
279template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000280struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000281 : add_cv<variant_alternative_t<_Ip, _Tp>> {};
282
283template <size_t _Ip, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000284struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
Marshall Clow185577f2017-06-12 16:13:17 +0000285 static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000286 using type = __type_pack_element<_Ip, _Types...>;
287};
288
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000289_LIBCPP_INLINE_VAR constexpr size_t variant_npos = static_cast<size_t>(-1);
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000290
291constexpr int __choose_index_type(unsigned int __num_elem) {
292 if (__num_elem < std::numeric_limits<unsigned char>::max())
293 return 0;
294 if (__num_elem < std::numeric_limits<unsigned short>::max())
295 return 1;
296 return 2;
297}
298
299template <size_t _NumAlts>
300using __variant_index_t =
301#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
302 unsigned int;
303#else
304 std::tuple_element_t<
305 __choose_index_type(_NumAlts),
306 std::tuple<unsigned char, unsigned short, unsigned int>
307 >;
308#endif
309
310template <class _IndexType>
311constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000312
313namespace __find_detail {
314
315template <class _Tp, class... _Types>
316inline _LIBCPP_INLINE_VISIBILITY
317constexpr size_t __find_index() {
318 constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
319 size_t __result = __not_found;
320 for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
321 if (__matches[__i]) {
322 if (__result != __not_found) {
323 return __ambiguous;
324 }
325 __result = __i;
326 }
327 }
328 return __result;
329}
330
331template <size_t _Index>
332struct __find_unambiguous_index_sfinae_impl
333 : integral_constant<size_t, _Index> {};
334
335template <>
336struct __find_unambiguous_index_sfinae_impl<__not_found> {};
337
338template <>
339struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
340
341template <class _Tp, class... _Types>
342struct __find_unambiguous_index_sfinae
343 : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
344
345} // namespace __find_detail
346
347namespace __variant_detail {
348
349struct __valueless_t {};
350
351enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
352
353template <typename _Tp,
354 template <typename> class _IsTriviallyAvailable,
355 template <typename> class _IsAvailable>
356constexpr _Trait __trait =
357 _IsTriviallyAvailable<_Tp>::value
358 ? _Trait::_TriviallyAvailable
359 : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
360
361inline _LIBCPP_INLINE_VISIBILITY
362constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
363 _Trait __result = _Trait::_TriviallyAvailable;
364 for (_Trait __t : __traits) {
365 if (static_cast<int>(__t) > static_cast<int>(__result)) {
366 __result = __t;
367 }
368 }
369 return __result;
370}
371
372template <typename... _Types>
373struct __traits {
374 static constexpr _Trait __copy_constructible_trait =
375 __common_trait({__trait<_Types,
376 is_trivially_copy_constructible,
377 is_copy_constructible>...});
378
379 static constexpr _Trait __move_constructible_trait =
380 __common_trait({__trait<_Types,
381 is_trivially_move_constructible,
382 is_move_constructible>...});
383
384 static constexpr _Trait __copy_assignable_trait = __common_trait(
385 {__copy_constructible_trait,
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000386 __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
387
388 static constexpr _Trait __move_assignable_trait = __common_trait(
389 {__move_constructible_trait,
390 __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
391
392 static constexpr _Trait __destructible_trait = __common_trait(
393 {__trait<_Types, is_trivially_destructible, is_destructible>...});
394};
395
396namespace __access {
397
398struct __union {
399 template <class _Vp>
400 inline _LIBCPP_INLINE_VISIBILITY
401 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
402 return _VSTD::forward<_Vp>(__v).__head;
403 }
404
405 template <class _Vp, size_t _Ip>
406 inline _LIBCPP_INLINE_VISIBILITY
407 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
408 return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
409 }
410};
411
412struct __base {
413 template <size_t _Ip, class _Vp>
414 inline _LIBCPP_INLINE_VISIBILITY
415 static constexpr auto&& __get_alt(_Vp&& __v) {
416 return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
417 in_place_index<_Ip>);
418 }
419};
420
421struct __variant {
422 template <size_t _Ip, class _Vp>
423 inline _LIBCPP_INLINE_VISIBILITY
424 static constexpr auto&& __get_alt(_Vp&& __v) {
425 return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
426 }
427};
428
429} // namespace __access
430
431namespace __visitation {
432
433struct __base {
434 template <class _Visitor, class... _Vs>
435 inline _LIBCPP_INLINE_VISIBILITY
436 static constexpr decltype(auto)
437 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
438 constexpr auto __fdiagonal =
439 __make_fdiagonal<_Visitor&&,
440 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
441 return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
442 _VSTD::forward<_Vs>(__vs).__as_base()...);
443 }
444
445 template <class _Visitor, class... _Vs>
446 inline _LIBCPP_INLINE_VISIBILITY
447 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
448 _Vs&&... __vs) {
449 constexpr auto __fmatrix =
450 __make_fmatrix<_Visitor&&,
451 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
Michael Park07157892017-05-11 07:17:12 +0000452 return __at(__fmatrix, __vs.index()...)(
453 _VSTD::forward<_Visitor>(__visitor),
454 _VSTD::forward<_Vs>(__vs).__as_base()...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000455 }
456
457private:
458 template <class _Tp>
459 inline _LIBCPP_INLINE_VISIBILITY
Michael Park07157892017-05-11 07:17:12 +0000460 static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000461
Michael Park07157892017-05-11 07:17:12 +0000462 template <class _Tp, size_t _Np, typename... _Indices>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000463 inline _LIBCPP_INLINE_VISIBILITY
464 static constexpr auto&& __at(const array<_Tp, _Np>& __elems,
Michael Park07157892017-05-11 07:17:12 +0000465 size_t __index, _Indices... __indices) {
466 return __at(__elems[__index], __indices...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000467 }
468
469 template <class _Fp, class... _Fs>
470 static constexpr void __std_visit_visitor_return_type_check() {
471 static_assert(
472 __all<is_same_v<_Fp, _Fs>...>::value,
473 "`std::visit` requires the visitor to have a single return type.");
474 }
475
476 template <class... _Fs>
477 inline _LIBCPP_INLINE_VISIBILITY
478 static constexpr auto __make_farray(_Fs&&... __fs) {
Marshall Clowe61ba952018-02-12 15:41:25 +0000479 __std_visit_visitor_return_type_check<__uncvref_t<_Fs>...>();
480 using __result = array<common_type_t<__uncvref_t<_Fs>...>, sizeof...(_Fs)>;
Eric Fiselier2adf0872016-12-02 23:17:33 +0000481 return __result{{_VSTD::forward<_Fs>(__fs)...}};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000482 }
483
Michael Park52dc9f02017-01-16 08:14:25 +0000484 template <std::size_t... _Is>
485 struct __dispatcher {
486 template <class _Fp, class... _Vs>
487 inline _LIBCPP_INLINE_VISIBILITY
488 static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000489 return __invoke_constexpr(
490 static_cast<_Fp>(__f),
491 __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
Michael Park52dc9f02017-01-16 08:14:25 +0000492 }
493 };
494
495 template <class _Fp, class... _Vs, size_t... _Is>
496 inline _LIBCPP_INLINE_VISIBILITY
497 static constexpr auto __make_dispatch(index_sequence<_Is...>) {
Eric Fiselier0ae996d2017-02-05 20:36:07 +0000498 return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000499 }
500
501 template <size_t _Ip, class _Fp, class... _Vs>
502 inline _LIBCPP_INLINE_VISIBILITY
503 static constexpr auto __make_fdiagonal_impl() {
504 return __make_dispatch<_Fp, _Vs...>(
505 index_sequence<(__identity<_Vs>{}, _Ip)...>{});
506 }
507
508 template <class _Fp, class... _Vs, size_t... _Is>
509 inline _LIBCPP_INLINE_VISIBILITY
510 static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
511 return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
512 }
513
514 template <class _Fp, class _Vp, class... _Vs>
515 inline _LIBCPP_INLINE_VISIBILITY
516 static constexpr auto __make_fdiagonal() {
Marshall Clowe61ba952018-02-12 15:41:25 +0000517 constexpr size_t _Np = __uncvref_t<_Vp>::__size();
518 static_assert(__all<(_Np == __uncvref_t<_Vs>::__size())...>::value);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000519 return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
520 }
521
522 template <class _Fp, class... _Vs, size_t... _Is>
523 inline _LIBCPP_INLINE_VISIBILITY
524 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
525 return __make_dispatch<_Fp, _Vs...>(__is);
526 }
527
528 template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
529 inline _LIBCPP_INLINE_VISIBILITY
530 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
531 index_sequence<_Js...>,
532 _Ls... __ls) {
533 return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
534 index_sequence<_Is..., _Js>{}, __ls...)...);
535 }
536
537 template <class _Fp, class... _Vs>
538 inline _LIBCPP_INLINE_VISIBILITY
539 static constexpr auto __make_fmatrix() {
540 return __make_fmatrix_impl<_Fp, _Vs...>(
Marshall Clowe61ba952018-02-12 15:41:25 +0000541 index_sequence<>{}, make_index_sequence<__uncvref_t<_Vs>::__size()>{}...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000542 }
543};
544
545struct __variant {
546 template <class _Visitor, class... _Vs>
547 inline _LIBCPP_INLINE_VISIBILITY
548 static constexpr decltype(auto)
549 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
550 return __base::__visit_alt_at(__index,
551 _VSTD::forward<_Visitor>(__visitor),
552 _VSTD::forward<_Vs>(__vs).__impl...);
553 }
554
555 template <class _Visitor, class... _Vs>
556 inline _LIBCPP_INLINE_VISIBILITY
557 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
558 _Vs&&... __vs) {
559 return __base::__visit_alt(_VSTD::forward<_Visitor>(__visitor),
560 _VSTD::forward<_Vs>(__vs).__impl...);
561 }
562
563 template <class _Visitor, class... _Vs>
564 inline _LIBCPP_INLINE_VISIBILITY
565 static constexpr decltype(auto)
566 __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
567 return __visit_alt_at(
568 __index,
569 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
570 _VSTD::forward<_Vs>(__vs)...);
571 }
572
573 template <class _Visitor, class... _Vs>
574 inline _LIBCPP_INLINE_VISIBILITY
575 static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
576 _Vs&&... __vs) {
577 return __visit_alt(
578 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
579 _VSTD::forward<_Vs>(__vs)...);
580 }
581
582private:
583 template <class _Visitor, class... _Values>
584 static constexpr void __std_visit_exhaustive_visitor_check() {
Zhihao Yuane2bb8752017-12-12 18:42:04 +0000585 static_assert(is_invocable_v<_Visitor, _Values...>,
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000586 "`std::visit` requires the visitor to be exhaustive.");
587 }
588
589 template <class _Visitor>
590 struct __value_visitor {
591 template <class... _Alts>
592 inline _LIBCPP_INLINE_VISIBILITY
593 constexpr decltype(auto) operator()(_Alts&&... __alts) const {
594 __std_visit_exhaustive_visitor_check<
595 _Visitor,
Eric Fiselier060c1fe2017-02-09 19:01:22 +0000596 decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000597 return __invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
598 _VSTD::forward<_Alts>(__alts).__value...);
599 }
600 _Visitor&& __visitor;
601 };
602
603 template <class _Visitor>
604 inline _LIBCPP_INLINE_VISIBILITY
605 static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
606 return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
607 }
608};
609
610} // namespace __visitation
611
612template <size_t _Index, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000613struct _LIBCPP_TEMPLATE_VIS __alt {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000614 using __value_type = _Tp;
615
616 template <class... _Args>
617 inline _LIBCPP_INLINE_VISIBILITY
618 explicit constexpr __alt(in_place_t, _Args&&... __args)
619 : __value(_VSTD::forward<_Args>(__args)...) {}
620
621 __value_type __value;
622};
623
624template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000625union _LIBCPP_TEMPLATE_VIS __union;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000626
627template <_Trait _DestructibleTrait, size_t _Index>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000628union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000629
630#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \
631 template <size_t _Index, class _Tp, class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000632 union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000633 _Index, \
634 _Tp, \
635 _Types...> { \
636 public: \
637 inline _LIBCPP_INLINE_VISIBILITY \
638 explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
639 \
640 template <class... _Args> \
641 inline _LIBCPP_INLINE_VISIBILITY \
642 explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
643 : __head(in_place, _VSTD::forward<_Args>(__args)...) {} \
644 \
645 template <size_t _Ip, class... _Args> \
646 inline _LIBCPP_INLINE_VISIBILITY \
647 explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
648 : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
649 \
650 __union(const __union&) = default; \
651 __union(__union&&) = default; \
652 \
653 destructor \
654 \
655 __union& operator=(const __union&) = default; \
656 __union& operator=(__union&&) = default; \
657 \
658 private: \
659 char __dummy; \
660 __alt<_Index, _Tp> __head; \
661 __union<destructible_trait, _Index + 1, _Types...> __tail; \
662 \
663 friend struct __access::__union; \
664 }
665
666_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
667_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
668_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
669
670#undef _LIBCPP_VARIANT_UNION
671
672template <_Trait _DestructibleTrait, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000673class _LIBCPP_TEMPLATE_VIS __base {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000674public:
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000675 using __index_t = __variant_index_t<sizeof...(_Types)>;
676
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000677 inline _LIBCPP_INLINE_VISIBILITY
678 explicit constexpr __base(__valueless_t tag) noexcept
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000679 : __data(tag), __index(__variant_npos<__index_t>) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000680
681 template <size_t _Ip, class... _Args>
682 inline _LIBCPP_INLINE_VISIBILITY
683 explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
Eric Fiselier2adf0872016-12-02 23:17:33 +0000684 :
685 __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
686 __index(_Ip) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000687
688 inline _LIBCPP_INLINE_VISIBILITY
689 constexpr bool valueless_by_exception() const noexcept {
690 return index() == variant_npos;
691 }
692
693 inline _LIBCPP_INLINE_VISIBILITY
694 constexpr size_t index() const noexcept {
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000695 return __index == __variant_npos<__index_t> ? variant_npos : __index;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000696 }
697
698protected:
699 inline _LIBCPP_INLINE_VISIBILITY
700 constexpr auto&& __as_base() & { return *this; }
701
702 inline _LIBCPP_INLINE_VISIBILITY
703 constexpr auto&& __as_base() && { return _VSTD::move(*this); }
704
705 inline _LIBCPP_INLINE_VISIBILITY
706 constexpr auto&& __as_base() const & { return *this; }
707
708 inline _LIBCPP_INLINE_VISIBILITY
709 constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
710
711 inline _LIBCPP_INLINE_VISIBILITY
712 static constexpr size_t __size() { return sizeof...(_Types); }
713
714 __union<_DestructibleTrait, 0, _Types...> __data;
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000715 __index_t __index;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000716
717 friend struct __access::__base;
718 friend struct __visitation::__base;
719};
720
721template <class _Traits, _Trait = _Traits::__destructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000722class _LIBCPP_TEMPLATE_VIS __destructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000723
724#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \
725 template <class... _Types> \
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000726 class _LIBCPP_TEMPLATE_VIS __destructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000727 destructible_trait> \
728 : public __base<destructible_trait, _Types...> { \
729 using __base_type = __base<destructible_trait, _Types...>; \
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000730 using __index_t = typename __base_type::__index_t; \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000731 \
732 public: \
733 using __base_type::__base_type; \
734 using __base_type::operator=; \
735 \
736 __destructor(const __destructor&) = default; \
737 __destructor(__destructor&&) = default; \
738 destructor \
739 __destructor& operator=(const __destructor&) = default; \
740 __destructor& operator=(__destructor&&) = default; \
741 \
742 protected: \
743 inline _LIBCPP_INLINE_VISIBILITY \
744 destroy \
745 }
746
747_LIBCPP_VARIANT_DESTRUCTOR(
748 _Trait::_TriviallyAvailable,
749 ~__destructor() = default;,
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000750 void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000751
752_LIBCPP_VARIANT_DESTRUCTOR(
753 _Trait::_Available,
754 ~__destructor() { __destroy(); },
755 void __destroy() noexcept {
756 if (!this->valueless_by_exception()) {
757 __visitation::__base::__visit_alt(
758 [](auto& __alt) noexcept {
Marshall Clowe61ba952018-02-12 15:41:25 +0000759 using __alt_type = __uncvref_t<decltype(__alt)>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000760 __alt.~__alt_type();
761 },
762 *this);
763 }
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000764 this->__index = __variant_npos<__index_t>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000765 });
766
767_LIBCPP_VARIANT_DESTRUCTOR(
768 _Trait::_Unavailable,
769 ~__destructor() = delete;,
770 void __destroy() noexcept = delete;);
771
772#undef _LIBCPP_VARIANT_DESTRUCTOR
773
774template <class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000775class _LIBCPP_TEMPLATE_VIS __constructor : public __destructor<_Traits> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000776 using __base_type = __destructor<_Traits>;
777
778public:
779 using __base_type::__base_type;
780 using __base_type::operator=;
781
782protected:
783 template <size_t _Ip, class _Tp, class... _Args>
784 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +0000785 static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
786 ::new ((void*)_VSTD::addressof(__a))
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000787 __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
Eric Fiselier62928442017-04-15 19:32:02 +0000788 return __a.__value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000789 }
790
791 template <class _Rhs>
792 inline _LIBCPP_INLINE_VISIBILITY
793 static void __generic_construct(__constructor& __lhs, _Rhs&& __rhs) {
794 __lhs.__destroy();
795 if (!__rhs.valueless_by_exception()) {
796 __visitation::__base::__visit_alt_at(
797 __rhs.index(),
798 [](auto& __lhs_alt, auto&& __rhs_alt) {
799 __construct_alt(
800 __lhs_alt,
801 _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
802 },
803 __lhs, _VSTD::forward<_Rhs>(__rhs));
804 __lhs.__index = __rhs.index();
805 }
806 }
807};
808
809template <class _Traits, _Trait = _Traits::__move_constructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000810class _LIBCPP_TEMPLATE_VIS __move_constructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000811
812#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \
813 move_constructor) \
814 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000815 class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000816 move_constructible_trait> \
817 : public __constructor<__traits<_Types...>> { \
818 using __base_type = __constructor<__traits<_Types...>>; \
819 \
820 public: \
821 using __base_type::__base_type; \
822 using __base_type::operator=; \
823 \
824 __move_constructor(const __move_constructor&) = default; \
825 move_constructor \
826 ~__move_constructor() = default; \
827 __move_constructor& operator=(const __move_constructor&) = default; \
828 __move_constructor& operator=(__move_constructor&&) = default; \
829 }
830
831_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
832 _Trait::_TriviallyAvailable,
833 __move_constructor(__move_constructor&& __that) = default;);
834
835_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
836 _Trait::_Available,
837 __move_constructor(__move_constructor&& __that) noexcept(
838 __all<is_nothrow_move_constructible_v<_Types>...>::value)
839 : __move_constructor(__valueless_t{}) {
840 this->__generic_construct(*this, _VSTD::move(__that));
841 });
842
843_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
844 _Trait::_Unavailable,
845 __move_constructor(__move_constructor&&) = delete;);
846
847#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
848
849template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000850class _LIBCPP_TEMPLATE_VIS __copy_constructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000851
852#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \
853 copy_constructor) \
854 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000855 class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000856 copy_constructible_trait> \
857 : public __move_constructor<__traits<_Types...>> { \
858 using __base_type = __move_constructor<__traits<_Types...>>; \
859 \
860 public: \
861 using __base_type::__base_type; \
862 using __base_type::operator=; \
863 \
864 copy_constructor \
865 __copy_constructor(__copy_constructor&&) = default; \
866 ~__copy_constructor() = default; \
867 __copy_constructor& operator=(const __copy_constructor&) = default; \
868 __copy_constructor& operator=(__copy_constructor&&) = default; \
869 }
870
871_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
872 _Trait::_TriviallyAvailable,
873 __copy_constructor(const __copy_constructor& __that) = default;);
874
875_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
876 _Trait::_Available,
877 __copy_constructor(const __copy_constructor& __that)
878 : __copy_constructor(__valueless_t{}) {
879 this->__generic_construct(*this, __that);
880 });
881
882_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
883 _Trait::_Unavailable,
884 __copy_constructor(const __copy_constructor&) = delete;);
885
886#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
887
888template <class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000889class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000890 using __base_type = __copy_constructor<_Traits>;
891
892public:
893 using __base_type::__base_type;
894 using __base_type::operator=;
895
896 template <size_t _Ip, class... _Args>
897 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +0000898 auto& __emplace(_Args&&... __args) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000899 this->__destroy();
Eric Fiselier62928442017-04-15 19:32:02 +0000900 auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000901 _VSTD::forward<_Args>(__args)...);
902 this->__index = _Ip;
Eric Fiselier62928442017-04-15 19:32:02 +0000903 return __res;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000904 }
905
906protected:
Michael Park62118352017-06-07 10:22:43 +0000907 template <size_t _Ip, class _Tp, class _Arg>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000908 inline _LIBCPP_INLINE_VISIBILITY
Michael Park62118352017-06-07 10:22:43 +0000909 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000910 if (this->index() == _Ip) {
911 __a.__value = _VSTD::forward<_Arg>(__arg);
912 } else {
913 struct {
914 void operator()(true_type) const {
Michael Park62118352017-06-07 10:22:43 +0000915 __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000916 }
917 void operator()(false_type) const {
Michael Park62118352017-06-07 10:22:43 +0000918 __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000919 }
920 __assignment* __this;
921 _Arg&& __arg;
922 } __impl{this, _VSTD::forward<_Arg>(__arg)};
Michael Park62118352017-06-07 10:22:43 +0000923 __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
924 !is_nothrow_move_constructible_v<_Tp>>{});
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000925 }
926 }
927
928 template <class _That>
929 inline _LIBCPP_INLINE_VISIBILITY
930 void __generic_assign(_That&& __that) {
931 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
932 // do nothing.
933 } else if (__that.valueless_by_exception()) {
934 this->__destroy();
935 } else {
936 __visitation::__base::__visit_alt_at(
937 __that.index(),
938 [this](auto& __this_alt, auto&& __that_alt) {
939 this->__assign_alt(
940 __this_alt,
Michael Park62118352017-06-07 10:22:43 +0000941 _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000942 },
943 *this, _VSTD::forward<_That>(__that));
944 }
945 }
946};
947
948template <class _Traits, _Trait = _Traits::__move_assignable_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000949class _LIBCPP_TEMPLATE_VIS __move_assignment;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000950
951#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \
952 move_assignment) \
953 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000954 class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000955 move_assignable_trait> \
956 : public __assignment<__traits<_Types...>> { \
957 using __base_type = __assignment<__traits<_Types...>>; \
958 \
959 public: \
960 using __base_type::__base_type; \
961 using __base_type::operator=; \
962 \
963 __move_assignment(const __move_assignment&) = default; \
964 __move_assignment(__move_assignment&&) = default; \
965 ~__move_assignment() = default; \
966 __move_assignment& operator=(const __move_assignment&) = default; \
967 move_assignment \
968 }
969
970_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
971 _Trait::_TriviallyAvailable,
972 __move_assignment& operator=(__move_assignment&& __that) = default;);
973
974_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
975 _Trait::_Available,
976 __move_assignment& operator=(__move_assignment&& __that) noexcept(
977 __all<(is_nothrow_move_constructible_v<_Types> &&
978 is_nothrow_move_assignable_v<_Types>)...>::value) {
979 this->__generic_assign(_VSTD::move(__that));
980 return *this;
981 });
982
983_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
984 _Trait::_Unavailable,
985 __move_assignment& operator=(__move_assignment&&) = delete;);
986
987#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
988
989template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000990class _LIBCPP_TEMPLATE_VIS __copy_assignment;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000991
992#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \
993 copy_assignment) \
994 template <class... _Types> \
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000995 class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000996 copy_assignable_trait> \
997 : public __move_assignment<__traits<_Types...>> { \
998 using __base_type = __move_assignment<__traits<_Types...>>; \
999 \
1000 public: \
1001 using __base_type::__base_type; \
1002 using __base_type::operator=; \
1003 \
1004 __copy_assignment(const __copy_assignment&) = default; \
1005 __copy_assignment(__copy_assignment&&) = default; \
1006 ~__copy_assignment() = default; \
1007 copy_assignment \
1008 __copy_assignment& operator=(__copy_assignment&&) = default; \
1009 }
1010
1011_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1012 _Trait::_TriviallyAvailable,
1013 __copy_assignment& operator=(const __copy_assignment& __that) = default;);
1014
1015_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1016 _Trait::_Available,
1017 __copy_assignment& operator=(const __copy_assignment& __that) {
1018 this->__generic_assign(__that);
1019 return *this;
1020 });
1021
1022_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1023 _Trait::_Unavailable,
1024 __copy_assignment& operator=(const __copy_assignment&) = delete;);
1025
1026#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1027
1028template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001029class _LIBCPP_TEMPLATE_VIS __impl
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001030 : public __copy_assignment<__traits<_Types...>> {
1031 using __base_type = __copy_assignment<__traits<_Types...>>;
1032
1033public:
1034 using __base_type::__base_type;
1035 using __base_type::operator=;
1036
1037 template <size_t _Ip, class _Arg>
1038 inline _LIBCPP_INLINE_VISIBILITY
1039 void __assign(_Arg&& __arg) {
1040 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
Michael Park62118352017-06-07 10:22:43 +00001041 _VSTD::forward<_Arg>(__arg));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001042 }
1043
1044 inline _LIBCPP_INLINE_VISIBILITY
1045 void __swap(__impl& __that) {
1046 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1047 // do nothing.
1048 } else if (this->index() == __that.index()) {
1049 __visitation::__base::__visit_alt_at(
1050 this->index(),
1051 [](auto& __this_alt, auto& __that_alt) {
1052 using _VSTD::swap;
1053 swap(__this_alt.__value, __that_alt.__value);
1054 },
1055 *this,
1056 __that);
1057 } else {
1058 __impl* __lhs = this;
1059 __impl* __rhs = _VSTD::addressof(__that);
1060 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1061 _VSTD::swap(__lhs, __rhs);
1062 }
1063 __impl __tmp(_VSTD::move(*__rhs));
1064#ifndef _LIBCPP_NO_EXCEPTIONS
1065 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1066 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1067 // into `__rhs` and provide the strong exception safety guarentee.
1068 try {
1069 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1070 } catch (...) {
1071 if (__tmp.__move_nothrow()) {
1072 this->__generic_construct(*__rhs, _VSTD::move(__tmp));
1073 }
1074 throw;
1075 }
1076#else
1077 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1078#endif
1079 this->__generic_construct(*__lhs, _VSTD::move(__tmp));
1080 }
1081 }
1082
1083private:
1084 inline _LIBCPP_INLINE_VISIBILITY
1085 bool __move_nothrow() const {
1086 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1087 return this->valueless_by_exception() || __results[this->index()];
1088 }
1089};
1090
1091template <class... _Types>
1092struct __overload;
1093
1094template <>
1095struct __overload<> { void operator()() const; };
1096
1097template <class _Tp, class... _Types>
1098struct __overload<_Tp, _Types...> : __overload<_Types...> {
1099 using __overload<_Types...>::operator();
1100 __identity<_Tp> operator()(_Tp) const;
1101};
1102
1103template <class _Tp, class... _Types>
1104using __best_match_t = typename result_of_t<__overload<_Types...>(_Tp&&)>::type;
1105
1106} // __variant_detail
1107
1108template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001109class _LIBCPP_TEMPLATE_VIS variant
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001110 : private __sfinae_ctor_base<
1111 __all<is_copy_constructible_v<_Types>...>::value,
1112 __all<is_move_constructible_v<_Types>...>::value>,
1113 private __sfinae_assign_base<
1114 __all<(is_copy_constructible_v<_Types> &&
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001115 is_copy_assignable_v<_Types>)...>::value,
1116 __all<(is_move_constructible_v<_Types> &&
1117 is_move_assignable_v<_Types>)...>::value> {
1118 static_assert(0 < sizeof...(_Types),
1119 "variant must consist of at least one alternative.");
1120
1121 static_assert(__all<!is_array_v<_Types>...>::value,
1122 "variant can not have an array type as an alternative.");
1123
1124 static_assert(__all<!is_reference_v<_Types>...>::value,
1125 "variant can not have a reference type as an alternative.");
1126
1127 static_assert(__all<!is_void_v<_Types>...>::value,
1128 "variant can not have a void type as an alternative.");
1129
1130 using __first_type = variant_alternative_t<0, variant>;
1131
1132public:
1133 template <bool _Dummy = true,
1134 enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1135 _Dummy>::value,
1136 int> = 0>
1137 inline _LIBCPP_INLINE_VISIBILITY
1138 constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1139 : __impl(in_place_index<0>) {}
1140
1141 variant(const variant&) = default;
1142 variant(variant&&) = default;
1143
1144 template <
1145 class _Arg,
Marshall Clowd5bbc242018-02-06 20:56:55 +00001146 enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
1147 enable_if_t<!__is_inplace_type<__uncvref_t<_Arg>>::value, int> = 0,
1148 enable_if_t<!__is_inplace_index<__uncvref_t<_Arg>>::value, int> = 0,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001149 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1150 size_t _Ip =
1151 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1152 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1153 inline _LIBCPP_INLINE_VISIBILITY
1154 constexpr variant(_Arg&& __arg) noexcept(
1155 is_nothrow_constructible_v<_Tp, _Arg>)
1156 : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
1157
1158 template <size_t _Ip, class... _Args,
Eric Fiselierb9cb6e02018-03-22 22:32:55 +00001159 enable_if_t<(_Ip < sizeof...(_Types)), size_t> _Ip2 = _Ip,
1160 class _Tp = variant_alternative_t<_Ip2, variant<_Types...>>,
1161 enable_if_t<is_constructible<_Tp, _Args...>::value, int> = 0>
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001162 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb9cb6e02018-03-22 22:32:55 +00001163 explicit constexpr variant(in_place_index_t<_Ip>,
1164 _Args&&... __args)
1165 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001166 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1167
Eric Fiselierb9cb6e02018-03-22 22:32:55 +00001168 template <size_t _Ip, class _Up, class... _Args,
1169 enable_if_t<(_Ip < sizeof...(_Types)), size_t> _Ip2 = _Ip,
1170 class _Tp = variant_alternative_t<_Ip2, variant<_Types...>>,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001171 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1172 int> = 0>
1173 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb9cb6e02018-03-22 22:32:55 +00001174 explicit constexpr variant(in_place_index_t<_Ip>, initializer_list<_Up> __il,
1175 _Args&&... __args)
1176 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001177 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1178
1179 template <
1180 class _Tp,
1181 class... _Args,
1182 size_t _Ip =
1183 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1184 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1185 inline _LIBCPP_INLINE_VISIBILITY
1186 explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1187 is_nothrow_constructible_v<_Tp, _Args...>)
1188 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1189
1190 template <
1191 class _Tp,
1192 class _Up,
1193 class... _Args,
1194 size_t _Ip =
1195 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1196 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1197 int> = 0>
1198 inline _LIBCPP_INLINE_VISIBILITY
1199 explicit constexpr variant(
1200 in_place_type_t<_Tp>,
1201 initializer_list<_Up> __il,
1202 _Args&&... __args) noexcept(
1203 is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1204 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1205
1206 ~variant() = default;
1207
1208 variant& operator=(const variant&) = default;
1209 variant& operator=(variant&&) = default;
1210
1211 template <
1212 class _Arg,
Marshall Clowd5bbc242018-02-06 20:56:55 +00001213 enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001214 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1215 size_t _Ip =
1216 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1217 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
1218 int> = 0>
1219 inline _LIBCPP_INLINE_VISIBILITY
1220 variant& operator=(_Arg&& __arg) noexcept(
1221 is_nothrow_assignable_v<_Tp&, _Arg> &&
1222 is_nothrow_constructible_v<_Tp, _Arg>) {
1223 __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
1224 return *this;
1225 }
1226
1227 template <
1228 size_t _Ip,
1229 class... _Args,
1230 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1231 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1232 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1233 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001234 _Tp& emplace(_Args&&... __args) {
1235 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001236 }
1237
1238 template <
1239 size_t _Ip,
1240 class _Up,
1241 class... _Args,
1242 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1243 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1244 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1245 int> = 0>
1246 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001247 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1248 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001249 }
1250
1251 template <
1252 class _Tp,
1253 class... _Args,
1254 size_t _Ip =
1255 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1256 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1257 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001258 _Tp& emplace(_Args&&... __args) {
1259 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001260 }
1261
1262 template <
1263 class _Tp,
1264 class _Up,
1265 class... _Args,
1266 size_t _Ip =
1267 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1268 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1269 int> = 0>
1270 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001271 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1272 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001273 }
1274
1275 inline _LIBCPP_INLINE_VISIBILITY
1276 constexpr bool valueless_by_exception() const noexcept {
1277 return __impl.valueless_by_exception();
1278 }
1279
1280 inline _LIBCPP_INLINE_VISIBILITY
1281 constexpr size_t index() const noexcept { return __impl.index(); }
1282
1283 template <
1284 bool _Dummy = true,
1285 enable_if_t<
1286 __all<(
1287 __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1288 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1289 int> = 0>
1290 inline _LIBCPP_INLINE_VISIBILITY
1291 void swap(variant& __that) noexcept(
1292 __all<(is_nothrow_move_constructible_v<_Types> &&
1293 is_nothrow_swappable_v<_Types>)...>::value) {
1294 __impl.__swap(__that.__impl);
1295 }
1296
1297private:
1298 __variant_detail::__impl<_Types...> __impl;
1299
1300 friend struct __variant_detail::__access::__variant;
1301 friend struct __variant_detail::__visitation::__variant;
1302};
1303
1304template <size_t _Ip, class... _Types>
1305inline _LIBCPP_INLINE_VISIBILITY
1306constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1307 return __v.index() == _Ip;
1308}
1309
1310template <class _Tp, class... _Types>
1311inline _LIBCPP_INLINE_VISIBILITY
1312constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1313 return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1314}
1315
1316template <size_t _Ip, class _Vp>
1317inline _LIBCPP_INLINE_VISIBILITY
1318static constexpr auto&& __generic_get(_Vp&& __v) {
1319 using __variant_detail::__access::__variant;
1320 if (!__holds_alternative<_Ip>(__v)) {
Eric Fiselier10642ea2016-12-03 01:58:07 +00001321 __throw_bad_variant_access();
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001322 }
1323 return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
1324}
1325
1326template <size_t _Ip, class... _Types>
1327inline _LIBCPP_INLINE_VISIBILITY
1328constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
1329 variant<_Types...>& __v) {
1330 static_assert(_Ip < sizeof...(_Types));
1331 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1332 return __generic_get<_Ip>(__v);
1333}
1334
1335template <size_t _Ip, class... _Types>
1336inline _LIBCPP_INLINE_VISIBILITY
1337constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
1338 variant<_Types...>&& __v) {
1339 static_assert(_Ip < sizeof...(_Types));
1340 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1341 return __generic_get<_Ip>(_VSTD::move(__v));
1342}
1343
1344template <size_t _Ip, class... _Types>
1345inline _LIBCPP_INLINE_VISIBILITY
1346constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
1347 const variant<_Types...>& __v) {
1348 static_assert(_Ip < sizeof...(_Types));
1349 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1350 return __generic_get<_Ip>(__v);
1351}
1352
1353template <size_t _Ip, class... _Types>
1354inline _LIBCPP_INLINE_VISIBILITY
1355constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
1356 const variant<_Types...>&& __v) {
1357 static_assert(_Ip < sizeof...(_Types));
1358 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1359 return __generic_get<_Ip>(_VSTD::move(__v));
1360}
1361
1362template <class _Tp, class... _Types>
1363inline _LIBCPP_INLINE_VISIBILITY
1364constexpr _Tp& get(variant<_Types...>& __v) {
1365 static_assert(!is_void_v<_Tp>);
1366 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1367}
1368
1369template <class _Tp, class... _Types>
1370inline _LIBCPP_INLINE_VISIBILITY
1371constexpr _Tp&& get(variant<_Types...>&& __v) {
1372 static_assert(!is_void_v<_Tp>);
1373 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1374 _VSTD::move(__v));
1375}
1376
1377template <class _Tp, class... _Types>
1378inline _LIBCPP_INLINE_VISIBILITY
1379constexpr const _Tp& get(const variant<_Types...>& __v) {
1380 static_assert(!is_void_v<_Tp>);
1381 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1382}
1383
1384template <class _Tp, class... _Types>
1385inline _LIBCPP_INLINE_VISIBILITY
1386constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1387 static_assert(!is_void_v<_Tp>);
1388 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1389 _VSTD::move(__v));
1390}
1391
1392template <size_t _Ip, class _Vp>
1393inline _LIBCPP_INLINE_VISIBILITY
1394constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1395 using __variant_detail::__access::__variant;
1396 return __v && __holds_alternative<_Ip>(*__v)
1397 ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
1398 : nullptr;
1399}
1400
1401template <size_t _Ip, class... _Types>
1402inline _LIBCPP_INLINE_VISIBILITY
1403constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1404get_if(variant<_Types...>* __v) noexcept {
1405 static_assert(_Ip < sizeof...(_Types));
1406 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1407 return __generic_get_if<_Ip>(__v);
1408}
1409
1410template <size_t _Ip, class... _Types>
1411inline _LIBCPP_INLINE_VISIBILITY
1412constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1413get_if(const variant<_Types...>* __v) noexcept {
1414 static_assert(_Ip < sizeof...(_Types));
1415 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1416 return __generic_get_if<_Ip>(__v);
1417}
1418
1419template <class _Tp, class... _Types>
1420inline _LIBCPP_INLINE_VISIBILITY
1421constexpr add_pointer_t<_Tp>
1422get_if(variant<_Types...>* __v) noexcept {
1423 static_assert(!is_void_v<_Tp>);
1424 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1425}
1426
1427template <class _Tp, class... _Types>
1428inline _LIBCPP_INLINE_VISIBILITY
1429constexpr add_pointer_t<const _Tp>
1430get_if(const variant<_Types...>* __v) noexcept {
1431 static_assert(!is_void_v<_Tp>);
1432 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1433}
1434
1435template <class... _Types>
1436inline _LIBCPP_INLINE_VISIBILITY
1437constexpr bool operator==(const variant<_Types...>& __lhs,
1438 const variant<_Types...>& __rhs) {
1439 using __variant_detail::__visitation::__variant;
1440 if (__lhs.index() != __rhs.index()) return false;
1441 if (__lhs.valueless_by_exception()) return true;
1442 return __variant::__visit_value_at(__lhs.index(), equal_to<>{}, __lhs, __rhs);
1443}
1444
1445template <class... _Types>
1446inline _LIBCPP_INLINE_VISIBILITY
1447constexpr bool operator!=(const variant<_Types...>& __lhs,
1448 const variant<_Types...>& __rhs) {
1449 using __variant_detail::__visitation::__variant;
1450 if (__lhs.index() != __rhs.index()) return true;
1451 if (__lhs.valueless_by_exception()) return false;
1452 return __variant::__visit_value_at(
1453 __lhs.index(), not_equal_to<>{}, __lhs, __rhs);
1454}
1455
1456template <class... _Types>
1457inline _LIBCPP_INLINE_VISIBILITY
1458constexpr bool operator<(const variant<_Types...>& __lhs,
1459 const variant<_Types...>& __rhs) {
1460 using __variant_detail::__visitation::__variant;
1461 if (__rhs.valueless_by_exception()) return false;
1462 if (__lhs.valueless_by_exception()) return true;
1463 if (__lhs.index() < __rhs.index()) return true;
1464 if (__lhs.index() > __rhs.index()) return false;
1465 return __variant::__visit_value_at(__lhs.index(), less<>{}, __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 (__lhs.valueless_by_exception()) return false;
1474 if (__rhs.valueless_by_exception()) return true;
1475 if (__lhs.index() > __rhs.index()) return true;
1476 if (__lhs.index() < __rhs.index()) return false;
1477 return __variant::__visit_value_at(__lhs.index(), greater<>{}, __lhs, __rhs);
1478}
1479
1480template <class... _Types>
1481inline _LIBCPP_INLINE_VISIBILITY
1482constexpr bool operator<=(const variant<_Types...>& __lhs,
1483 const variant<_Types...>& __rhs) {
1484 using __variant_detail::__visitation::__variant;
1485 if (__lhs.valueless_by_exception()) return true;
1486 if (__rhs.valueless_by_exception()) return false;
1487 if (__lhs.index() < __rhs.index()) return true;
1488 if (__lhs.index() > __rhs.index()) return false;
1489 return __variant::__visit_value_at(
1490 __lhs.index(), less_equal<>{}, __lhs, __rhs);
1491}
1492
1493template <class... _Types>
1494inline _LIBCPP_INLINE_VISIBILITY
1495constexpr bool operator>=(const variant<_Types...>& __lhs,
1496 const variant<_Types...>& __rhs) {
1497 using __variant_detail::__visitation::__variant;
1498 if (__rhs.valueless_by_exception()) return true;
1499 if (__lhs.valueless_by_exception()) return false;
1500 if (__lhs.index() > __rhs.index()) return true;
1501 if (__lhs.index() < __rhs.index()) return false;
1502 return __variant::__visit_value_at(
1503 __lhs.index(), greater_equal<>{}, __lhs, __rhs);
1504}
1505
1506template <class _Visitor, class... _Vs>
1507inline _LIBCPP_INLINE_VISIBILITY
1508constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
1509 using __variant_detail::__visitation::__variant;
1510 bool __results[] = {__vs.valueless_by_exception()...};
1511 for (bool __result : __results) {
1512 if (__result) {
Eric Fiselier10642ea2016-12-03 01:58:07 +00001513 __throw_bad_variant_access();
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001514 }
1515 }
1516 return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
1517 _VSTD::forward<_Vs>(__vs)...);
1518}
1519
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001520struct _LIBCPP_TEMPLATE_VIS monostate {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001521
1522inline _LIBCPP_INLINE_VISIBILITY
1523constexpr bool operator<(monostate, monostate) noexcept { return false; }
1524
1525inline _LIBCPP_INLINE_VISIBILITY
1526constexpr bool operator>(monostate, monostate) noexcept { return false; }
1527
1528inline _LIBCPP_INLINE_VISIBILITY
1529constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1530
1531inline _LIBCPP_INLINE_VISIBILITY
1532constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1533
1534inline _LIBCPP_INLINE_VISIBILITY
1535constexpr bool operator==(monostate, monostate) noexcept { return true; }
1536
1537inline _LIBCPP_INLINE_VISIBILITY
1538constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1539
1540template <class... _Types>
1541inline _LIBCPP_INLINE_VISIBILITY
1542auto swap(variant<_Types...>& __lhs,
1543 variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1544 -> decltype(__lhs.swap(__rhs)) {
1545 __lhs.swap(__rhs);
1546}
1547
1548template <class... _Types>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001549struct _LIBCPP_TEMPLATE_VIS hash<
1550 __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001551 using argument_type = variant<_Types...>;
1552 using result_type = size_t;
1553
1554 inline _LIBCPP_INLINE_VISIBILITY
1555 result_type operator()(const argument_type& __v) const {
1556 using __variant_detail::__visitation::__variant;
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001557 size_t __res =
1558 __v.valueless_by_exception()
Eric Fiselier6b1683c2016-12-04 21:37:37 +00001559 ? 299792458 // Random value chosen by the universe upon creation
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001560 : __variant::__visit_alt(
1561 [](const auto& __alt) {
Marshall Clowe61ba952018-02-12 15:41:25 +00001562 using __alt_type = __uncvref_t<decltype(__alt)>;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001563 using __value_type = remove_const_t<
1564 typename __alt_type::__value_type>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001565 return hash<__value_type>{}(__alt.__value);
1566 },
1567 __v);
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001568 return __hash_combine(__res, hash<size_t>{}(__v.index()));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001569 }
1570};
1571
1572template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001573struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001574 using argument_type = monostate;
1575 using result_type = size_t;
1576
1577 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001578 result_type operator()(const argument_type&) const _NOEXCEPT {
Eric Fiselier6b1683c2016-12-04 21:37:37 +00001579 return 66740831; // return a fundamentally attractive random value.
1580 }
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001581};
1582
1583#endif // _LIBCPP_STD_VER > 14
1584
1585_LIBCPP_END_NAMESPACE_STD
1586
Eric Fiselier8625d332017-11-19 04:57:22 +00001587_LIBCPP_POP_MACROS
1588
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001589#endif // _LIBCPP_VARIANT