blob: aa1c17e1e5cc32b0909698c32ac3553a2e58ed1f [file] [log] [blame]
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001// -*- C++ -*-
2//===------------------------------ variant -------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Eric Fiselier94cdacc2016-12-02 23:00:05 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_VARIANT
11#define _LIBCPP_VARIANT
12
13/*
14 variant synopsis
15
16namespace std {
17
18 // 20.7.2, class template variant
19 template <class... Types>
20 class variant {
21 public:
22
23 // 20.7.2.1, constructors
24 constexpr variant() noexcept(see below);
Louis Dionnecee59012019-01-10 20:06:11 +000025 variant(const variant&); // constexpr in C++20
26 variant(variant&&) noexcept(see below); // constexpr in C++20
Eric Fiselier94cdacc2016-12-02 23:00:05 +000027
28 template <class T> constexpr variant(T&&) noexcept(see below);
29
30 template <class T, class... Args>
31 constexpr explicit variant(in_place_type_t<T>, Args&&...);
32
33 template <class T, class U, class... Args>
34 constexpr explicit variant(
35 in_place_type_t<T>, initializer_list<U>, Args&&...);
36
37 template <size_t I, class... Args>
38 constexpr explicit variant(in_place_index_t<I>, Args&&...);
39
40 template <size_t I, class U, class... Args>
41 constexpr explicit variant(
42 in_place_index_t<I>, initializer_list<U>, Args&&...);
43
44 // 20.7.2.2, destructor
45 ~variant();
46
47 // 20.7.2.3, assignment
Louis Dionnecee59012019-01-10 20:06:11 +000048 variant& operator=(const variant&); // constexpr in C++20
49 variant& operator=(variant&&) noexcept(see below); // constexpr in C++20
Eric Fiselier94cdacc2016-12-02 23:00:05 +000050
51 template <class T> variant& operator=(T&&) noexcept(see below);
52
53 // 20.7.2.4, modifiers
54 template <class T, class... Args>
Eric Fiselier62928442017-04-15 19:32:02 +000055 T& emplace(Args&&...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +000056
57 template <class T, class U, class... Args>
Eric Fiselier62928442017-04-15 19:32:02 +000058 T& emplace(initializer_list<U>, Args&&...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +000059
60 template <size_t I, class... Args>
Eric Fiselier62928442017-04-15 19:32:02 +000061 variant_alternative_t<I, variant>& emplace(Args&&...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +000062
63 template <size_t I, class U, class... Args>
Eric Fiselier62928442017-04-15 19:32:02 +000064 variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +000065
66 // 20.7.2.5, value status
67 constexpr bool valueless_by_exception() const noexcept;
68 constexpr size_t index() const noexcept;
69
70 // 20.7.2.6, swap
71 void swap(variant&) noexcept(see below);
72 };
73
74 // 20.7.3, variant helper classes
75 template <class T> struct variant_size; // undefined
76
77 template <class T>
Marshall Clowf1bf62f2018-01-02 17:17:01 +000078 inline constexpr size_t variant_size_v = variant_size<T>::value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +000079
80 template <class T> struct variant_size<const T>;
81 template <class T> struct variant_size<volatile T>;
82 template <class T> struct variant_size<const volatile T>;
83
84 template <class... Types>
85 struct variant_size<variant<Types...>>;
86
87 template <size_t I, class T> struct variant_alternative; // undefined
88
89 template <size_t I, class T>
90 using variant_alternative_t = typename variant_alternative<I, T>::type;
91
92 template <size_t I, class T> struct variant_alternative<I, const T>;
93 template <size_t I, class T> struct variant_alternative<I, volatile T>;
94 template <size_t I, class T> struct variant_alternative<I, const volatile T>;
95
96 template <size_t I, class... Types>
97 struct variant_alternative<I, variant<Types...>>;
98
Marshall Clowf1bf62f2018-01-02 17:17:01 +000099 inline constexpr size_t variant_npos = -1;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000100
101 // 20.7.4, value access
102 template <class T, class... Types>
103 constexpr bool holds_alternative(const variant<Types...>&) noexcept;
104
105 template <size_t I, class... Types>
106 constexpr variant_alternative_t<I, variant<Types...>>&
107 get(variant<Types...>&);
108
109 template <size_t I, class... Types>
110 constexpr variant_alternative_t<I, variant<Types...>>&&
111 get(variant<Types...>&&);
112
113 template <size_t I, class... Types>
114 constexpr variant_alternative_t<I, variant<Types...>> const&
115 get(const variant<Types...>&);
116
117 template <size_t I, class... Types>
118 constexpr variant_alternative_t<I, variant<Types...>> const&&
119 get(const variant<Types...>&&);
120
121 template <class T, class... Types>
122 constexpr T& get(variant<Types...>&);
123
124 template <class T, class... Types>
125 constexpr T&& get(variant<Types...>&&);
126
127 template <class T, class... Types>
128 constexpr const T& get(const variant<Types...>&);
129
130 template <class T, class... Types>
131 constexpr const T&& get(const variant<Types...>&&);
132
133 template <size_t I, class... Types>
134 constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
135 get_if(variant<Types...>*) noexcept;
136
137 template <size_t I, class... Types>
138 constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
139 get_if(const variant<Types...>*) noexcept;
140
141 template <class T, class... Types>
142 constexpr add_pointer_t<T>
143 get_if(variant<Types...>*) noexcept;
144
145 template <class T, class... Types>
146 constexpr add_pointer_t<const T>
147 get_if(const variant<Types...>*) noexcept;
148
149 // 20.7.5, relational operators
150 template <class... Types>
151 constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
152
153 template <class... Types>
154 constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
155
156 template <class... Types>
157 constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
158
159 template <class... Types>
160 constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
161
162 template <class... Types>
163 constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
164
165 template <class... Types>
166 constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
167
168 // 20.7.6, visitation
169 template <class Visitor, class... Variants>
170 constexpr see below visit(Visitor&&, Variants&&...);
171
172 // 20.7.7, class monostate
173 struct monostate;
174
175 // 20.7.8, monostate relational operators
176 constexpr bool operator<(monostate, monostate) noexcept;
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
183 // 20.7.9, specialized algorithms
184 template <class... Types>
185 void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
186
187 // 20.7.10, class bad_variant_access
188 class bad_variant_access;
189
190 // 20.7.11, hash support
191 template <class T> struct hash;
192 template <class... Types> struct hash<variant<Types...>>;
193 template <> struct hash<monostate>;
194
195} // namespace std
196
197*/
198
199#include <__config>
200#include <__tuple>
201#include <array>
202#include <exception>
203#include <functional>
204#include <initializer_list>
205#include <new>
206#include <tuple>
207#include <type_traits>
208#include <utility>
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000209#include <limits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000210#include <version>
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
Louis Dionnecef92e62018-11-19 15:37:04 +0000221class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000222public:
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
Louis Dionne7202cd62020-07-22 15:24:16 -0400230// TODO: GCC 5 lies about its support for C++17 (it says it supports it but it
231// really doesn't). That breaks variant, which uses some C++17 features.
232// Remove this once we drop support for GCC 5.
Louis Dionne129391d2020-07-22 15:59:09 -0400233#if _LIBCPP_STD_VER > 14 && !(defined(_LIBCPP_COMPILER_GCC) && _GNUC_VER_NEW < 6000)
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000234
Eric Fiselier10642ea2016-12-03 01:58:07 +0000235_LIBCPP_NORETURN
236inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +0000237_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier10642ea2016-12-03 01:58:07 +0000238void __throw_bad_variant_access() {
239#ifndef _LIBCPP_NO_EXCEPTIONS
240 throw bad_variant_access();
241#else
242 _VSTD::abort();
243#endif
244}
245
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000246template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000247class _LIBCPP_TEMPLATE_VIS variant;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000248
249template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000250struct _LIBCPP_TEMPLATE_VIS variant_size;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000251
252template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000253_LIBCPP_INLINE_VAR constexpr size_t variant_size_v = variant_size<_Tp>::value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000254
255template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000256struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000257
258template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000259struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000260
261template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000262struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000263 : variant_size<_Tp> {};
264
265template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000266struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000267 : integral_constant<size_t, sizeof...(_Types)> {};
268
269template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000270struct _LIBCPP_TEMPLATE_VIS variant_alternative;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000271
272template <size_t _Ip, class _Tp>
273using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
274
275template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000276struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000277 : add_const<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, volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000281 : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
282
283template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000284struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000285 : add_cv<variant_alternative_t<_Ip, _Tp>> {};
286
287template <size_t _Ip, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000288struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
Marshall Clow185577f2017-06-12 16:13:17 +0000289 static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000290 using type = __type_pack_element<_Ip, _Types...>;
291};
292
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000293_LIBCPP_INLINE_VAR constexpr size_t variant_npos = static_cast<size_t>(-1);
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000294
295constexpr int __choose_index_type(unsigned int __num_elem) {
296 if (__num_elem < std::numeric_limits<unsigned char>::max())
297 return 0;
298 if (__num_elem < std::numeric_limits<unsigned short>::max())
299 return 1;
300 return 2;
301}
302
303template <size_t _NumAlts>
304using __variant_index_t =
305#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
306 unsigned int;
307#else
308 std::tuple_element_t<
309 __choose_index_type(_NumAlts),
310 std::tuple<unsigned char, unsigned short, unsigned int>
311 >;
312#endif
313
314template <class _IndexType>
315constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000316
317namespace __find_detail {
318
319template <class _Tp, class... _Types>
320inline _LIBCPP_INLINE_VISIBILITY
321constexpr size_t __find_index() {
322 constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
323 size_t __result = __not_found;
324 for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
325 if (__matches[__i]) {
326 if (__result != __not_found) {
327 return __ambiguous;
328 }
329 __result = __i;
330 }
331 }
332 return __result;
333}
334
335template <size_t _Index>
336struct __find_unambiguous_index_sfinae_impl
337 : integral_constant<size_t, _Index> {};
338
339template <>
340struct __find_unambiguous_index_sfinae_impl<__not_found> {};
341
342template <>
343struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
344
345template <class _Tp, class... _Types>
346struct __find_unambiguous_index_sfinae
347 : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
348
349} // namespace __find_detail
350
351namespace __variant_detail {
352
353struct __valueless_t {};
354
355enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
356
Michael Park34321672020-08-11 15:52:49 -0700357template <class _Tp,
358 template <class> class _IsTriviallyAvailable,
359 template <class> class _IsAvailable>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000360constexpr _Trait __trait =
361 _IsTriviallyAvailable<_Tp>::value
362 ? _Trait::_TriviallyAvailable
363 : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
364
365inline _LIBCPP_INLINE_VISIBILITY
366constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
367 _Trait __result = _Trait::_TriviallyAvailable;
368 for (_Trait __t : __traits) {
369 if (static_cast<int>(__t) > static_cast<int>(__result)) {
370 __result = __t;
371 }
372 }
373 return __result;
374}
375
Michael Park34321672020-08-11 15:52:49 -0700376template <class... _Types>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000377struct __traits {
378 static constexpr _Trait __copy_constructible_trait =
379 __common_trait({__trait<_Types,
380 is_trivially_copy_constructible,
381 is_copy_constructible>...});
382
383 static constexpr _Trait __move_constructible_trait =
384 __common_trait({__trait<_Types,
385 is_trivially_move_constructible,
386 is_move_constructible>...});
387
388 static constexpr _Trait __copy_assignable_trait = __common_trait(
389 {__copy_constructible_trait,
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000390 __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
391
392 static constexpr _Trait __move_assignable_trait = __common_trait(
393 {__move_constructible_trait,
394 __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
395
396 static constexpr _Trait __destructible_trait = __common_trait(
397 {__trait<_Types, is_trivially_destructible, is_destructible>...});
398};
399
400namespace __access {
401
402struct __union {
403 template <class _Vp>
404 inline _LIBCPP_INLINE_VISIBILITY
405 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
406 return _VSTD::forward<_Vp>(__v).__head;
407 }
408
409 template <class _Vp, size_t _Ip>
410 inline _LIBCPP_INLINE_VISIBILITY
411 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
412 return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
413 }
414};
415
416struct __base {
417 template <size_t _Ip, class _Vp>
418 inline _LIBCPP_INLINE_VISIBILITY
419 static constexpr auto&& __get_alt(_Vp&& __v) {
420 return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
421 in_place_index<_Ip>);
422 }
423};
424
425struct __variant {
426 template <size_t _Ip, class _Vp>
427 inline _LIBCPP_INLINE_VISIBILITY
428 static constexpr auto&& __get_alt(_Vp&& __v) {
429 return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
430 }
431};
432
433} // namespace __access
434
435namespace __visitation {
436
Michael Park34321672020-08-11 15:52:49 -0700437#define _LIBCPP_VARIANT_CASES_4(_Case, _Base) \
438 _Case(_Base + 0) \
439 _Case(_Base + 1) \
440 _Case(_Base + 2) \
441 _Case(_Base + 3)
442
443#define _LIBCPP_VARIANT_CASES_16(_Case, _Base) \
444 _LIBCPP_VARIANT_CASES_4(_Case, _Base + 4 * 0) \
445 _LIBCPP_VARIANT_CASES_4(_Case, _Base + 4 * 1) \
446 _LIBCPP_VARIANT_CASES_4(_Case, _Base + 4 * 2) \
447 _LIBCPP_VARIANT_CASES_4(_Case, _Base + 4 * 3)
448
449#define _LIBCPP_VARIANT_CASES_64(_Case, _Base) \
450 _LIBCPP_VARIANT_CASES_16(_Case, _Base + 16 * 0) \
451 _LIBCPP_VARIANT_CASES_16(_Case, _Base + 16 * 1) \
452 _LIBCPP_VARIANT_CASES_16(_Case, _Base + 16 * 2) \
453 _LIBCPP_VARIANT_CASES_16(_Case, _Base + 16 * 3)
454
455#define _LIBCPP_VARIANT_CASES_256(_Case, _Base) \
456 _LIBCPP_VARIANT_CASES_64(_Case, _Base + 64 * 0) \
457 _LIBCPP_VARIANT_CASES_64(_Case, _Base + 64 * 1) \
458 _LIBCPP_VARIANT_CASES_64(_Case, _Base + 64 * 2) \
459 _LIBCPP_VARIANT_CASES_64(_Case, _Base + 64 * 3)
460
461#define _LIBCPP_VARIANT_CASES(_NumCases, _Case) \
462 _LIBCPP_CONCAT(_LIBCPP_VARIANT_CASES_, _NumCases)(_Case, 0)
463
464#define _LIBCPP_VARIANT_SWITCH_MAX 256
465
466template <class _Iter, class _Fp, size_t... _Is>
467inline _LIBCPP_INLINE_VISIBILITY
468static constexpr void __fill_cartesian_impl(
469 _Iter __iter, _Fp __f, index_sequence<_Is...>) {
470 *__iter = __f(integral_constant<size_t, _Is>{}...);
471}
472
473template <class _Iter, class _Fp, size_t... _Is, size_t... _Js, class... _Ls>
474inline _LIBCPP_INLINE_VISIBILITY
475static constexpr void __fill_cartesian_impl(
476 _Iter __iter, _Fp __f, index_sequence<_Is...>, index_sequence<_Js...>, _Ls... __ls) {
477 constexpr size_t _Mp = (1 * ... * _Ls::size());
478 (__fill_cartesian_impl(
479 __iter + _Js * _Mp, __f, index_sequence<_Is..., _Js>{}, __ls...), ...);
480}
481
482template <size_t... _Ns, class _Iter, class _Fp>
483inline _LIBCPP_INLINE_VISIBILITY
484static constexpr void __fill_cartesian(_Iter __iter, _Fp __f) {
485 __fill_cartesian_impl(
486 __iter, __f, index_sequence<>{}, make_index_sequence<_Ns>{}...);
487}
488
489template <size_t _Np, size_t... _Ns>
490struct __multi {
491 inline _LIBCPP_INLINE_VISIBILITY
492 static constexpr size_t __size = (_Np * ... * _Ns);
493
494 inline _LIBCPP_INLINE_VISIBILITY
495 static constexpr size_t
496 __index(const size_t (&__is)[sizeof...(_Ns) + 1]) noexcept {
497 constexpr size_t __ns[] = {_Ns..., 1};
498 size_t __result = 0;
499 for (size_t __i = 0; __i < sizeof...(_Ns) + 1; ++__i) {
500 if (__is[__i] == variant_npos) {
501 return variant_npos;
502 }
503 __result += __is[__i];
504 __result *= __ns[__i];
505 }
506 return __result;
507 }
508};
509
510template <size_t... _Ns>
511struct __indices {
512 inline _LIBCPP_INLINE_VISIBILITY
513 static constexpr auto __value = [] {
514 using _Tp = array<size_t, sizeof...(_Ns)>;
515 array<_Tp, (1 * ... * _Ns)> __result = {};
516 __fill_cartesian<_Ns...>(__result.begin(),
517 [](auto... __is) -> _Tp { return {__is...}; });
518 return __result;
519 }();
520};
521
522template <size_t... _Ns, class _Fp, class _Rp, class... _Args>
523inline _LIBCPP_INLINE_VISIBILITY
524static constexpr auto __make_vtable_impl(_Fp __f, _Rp (*)(_Args...)) {
525 array<_Rp (*)(_Args...), (1 * ... * _Ns) + 1> __result = {
526 [](_Args...) -> _Rp { __throw_bad_variant_access(); }
527 };
528 __fill_cartesian<_Ns...>(__result.begin() + 1, __f);
529 return __result;
530}
531
532template <size_t... _Ns, class _Fp>
533inline _LIBCPP_INLINE_VISIBILITY
534static constexpr auto __make_vtable(_Fp __f) {
535 using _Tp = decltype(__f(integral_constant<size_t, (_Ns, 0)>{}...));
536 return __make_vtable_impl<_Ns...>(__f, _Tp{});
537}
538
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000539struct __base {
Michael Park34321672020-08-11 15:52:49 -0700540 template <class _Vis, class _Vp, class _Wp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000541 inline _LIBCPP_INLINE_VISIBILITY
542 static constexpr decltype(auto)
Michael Park34321672020-08-11 15:52:49 -0700543 __visit_alt_at(size_t __index, _Vis&& __vis, _Vp&& __v, _Wp&& __w) {
544 constexpr size_t __size = __uncvref_t<_Vp>::__size();
545 static_assert(__size == __uncvref_t<_Wp>::__size());
546 constexpr auto __dispatch = [](auto __i) {
Michael Park74257322020-08-14 16:30:10 -0700547 return +[](_Vis&& __vis_, _Vp&& __v_, _Wp&& __w_) -> decltype(auto) {
Michael Park34321672020-08-11 15:52:49 -0700548 constexpr size_t _Ip = decltype(__i)::value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000549 return __invoke_constexpr(
Michael Park74257322020-08-14 16:30:10 -0700550 _VSTD::forward<_Vis>(__vis_),
551 __access::__base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v_)),
552 __access::__base::__get_alt<_Ip>(_VSTD::forward<_Wp>(__w_)));
Michael Park34321672020-08-11 15:52:49 -0700553 };
554 };
555#define _LIBCPP_VARIANT_CASE(_Ip) \
556 case _Ip: { \
557 if constexpr (_Ip < __size) { \
558 return __dispatch(integral_constant<size_t, _Ip>{})( \
559 _VSTD::forward<_Vis>(__vis), \
560 _VSTD::forward<_Vp>(__v), \
561 _VSTD::forward<_Wp>(__w)); \
562 } else { \
563 _LIBCPP_UNREACHABLE(); \
564 } \
565 }
566 if constexpr (__size <= _LIBCPP_VARIANT_SWITCH_MAX) {
567 switch (__index) {
568 _LIBCPP_VARIANT_CASES(_LIBCPP_VARIANT_SWITCH_MAX, _LIBCPP_VARIANT_CASE)
569 default: __throw_bad_variant_access();
570 }
571 } else {
572 constexpr auto __vtable = __make_vtable<__size>(__dispatch);
573 return __vtable[__index + 1](_VSTD::forward<_Vis>(__vis),
574 _VSTD::forward<_Vp>(__v),
575 _VSTD::forward<_Wp>(__w));
Michael Park52dc9f02017-01-16 08:14:25 +0000576 }
Michael Park34321672020-08-11 15:52:49 -0700577#undef _LIBCPP_VARIANT_CASE
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000578 }
579
Michael Park34321672020-08-11 15:52:49 -0700580 template <size_t... _Is, class _Vis, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000581 inline _LIBCPP_INLINE_VISIBILITY
Michael Park34321672020-08-11 15:52:49 -0700582 static constexpr decltype(auto) __visit_alt(_Vis&& __vis, _Vs&&... __vs) {
583 if constexpr (sizeof...(_Vs) == 0) {
584 return __invoke_constexpr(_VSTD::forward<_Vis>(__vis));
585 } else {
586 return __visit_alt_impl(index_sequence_for<_Vs...>{},
587 _VSTD::forward<_Vis>(__vis),
588 _VSTD::forward<_Vs>(__vs)...);
589 }
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000590 }
591
Michael Park34321672020-08-11 15:52:49 -0700592 template <size_t... _Is, class _Vis, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000593 inline _LIBCPP_INLINE_VISIBILITY
Michael Park34321672020-08-11 15:52:49 -0700594 static constexpr decltype(auto)
595 __visit_alt_impl(index_sequence<_Is...>, _Vis&& __vis, _Vs&&... __vs) {
596 using __multi = __multi<__uncvref_t<_Vs>::__size()...>;
597 constexpr auto __dispatch = [](auto... __is) {
Michael Parkcfb19e92020-08-17 10:53:59 -0700598 return +[](_Vis&& __vis_, _Vs&&... __vs_) -> decltype(auto) {
Michael Park34321672020-08-11 15:52:49 -0700599 return __invoke_constexpr(
Michael Park74257322020-08-14 16:30:10 -0700600 _VSTD::forward<_Vis>(__vis_),
Michael Park34321672020-08-11 15:52:49 -0700601 __access::__base::__get_alt<decltype(__is)::value>(
Michael Park74257322020-08-14 16:30:10 -0700602 _VSTD::forward<_Vs>(__vs_))...);
Michael Park34321672020-08-11 15:52:49 -0700603 };
604 };
605#define _LIBCPP_VARIANT_CASE(_Ip) \
606 case _Ip: { \
607 if constexpr (_Ip < __multi::__size) { \
608 return __dispatch(integral_constant<size_t, __itable[_Ip][_Is]>{}...)( \
609 _VSTD::forward<_Vis>(__vis), _VSTD::forward<_Vs>(__vs)...); \
610 } else { \
611 _LIBCPP_UNREACHABLE(); \
612 } \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000613 }
Michael Park34321672020-08-11 15:52:49 -0700614 if constexpr (__multi::__size <= _LIBCPP_VARIANT_SWITCH_MAX) {
615 constexpr const auto& __itable =
616 __indices<__uncvref_t<_Vs>::__size()...>::__value;
617 switch (__multi::__index({__vs.index()...})) {
618 _LIBCPP_VARIANT_CASES(_LIBCPP_VARIANT_SWITCH_MAX, _LIBCPP_VARIANT_CASE)
619 default: __throw_bad_variant_access();
620 }
621 } else {
622 constexpr auto __vtable =
623 __make_vtable<__uncvref_t<_Vs>::__size()...>(__dispatch);
624 return __vtable[__multi::__index({__vs.index()...}) + 1](
625 _VSTD::forward<_Vis>(__vis), _VSTD::forward<_Vs>(__vs)...);
626 }
627#undef _LIBCPP_VARIANT_CASE
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000628 }
629};
630
631struct __variant {
Michael Park34321672020-08-11 15:52:49 -0700632 template <class _Vis, class _Vp, class _Wp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000633 inline _LIBCPP_INLINE_VISIBILITY
634 static constexpr decltype(auto)
Michael Park34321672020-08-11 15:52:49 -0700635 __visit_alt_at(size_t __index, _Vis&& __vis, _Vp&& __v, _Wp&& __w) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000636 return __base::__visit_alt_at(__index,
Michael Park34321672020-08-11 15:52:49 -0700637 _VSTD::forward<_Vis>(__vis),
638 _VSTD::forward<_Vp>(__v).__impl,
639 _VSTD::forward<_Wp>(__w).__impl);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000640 }
641
Michael Park34321672020-08-11 15:52:49 -0700642 template <class _Vis, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000643 inline _LIBCPP_INLINE_VISIBILITY
Michael Park34321672020-08-11 15:52:49 -0700644 static constexpr decltype(auto) __visit_alt(_Vis&& __vis, _Vs&&... __vs) {
645 return __base::__visit_alt(_VSTD::forward<_Vis>(__vis),
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000646 _VSTD::forward<_Vs>(__vs).__impl...);
647 }
648
Michael Park34321672020-08-11 15:52:49 -0700649 template <class _Vis, class _Vp, class _Wp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000650 inline _LIBCPP_INLINE_VISIBILITY
651 static constexpr decltype(auto)
Michael Park34321672020-08-11 15:52:49 -0700652 __visit_value_at(size_t __index, _Vis&& __vis, _Vp&& __v, _Wp&& __w) {
653 return __visit_alt_at(__index,
654 __make_value_visitor(_VSTD::forward<_Vis>(__vis)),
655 _VSTD::forward<_Vp>(__v),
656 _VSTD::forward<_Wp>(__w));
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000657 }
658
Michael Park34321672020-08-11 15:52:49 -0700659 template <class _Vis, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000660 inline _LIBCPP_INLINE_VISIBILITY
Michael Park34321672020-08-11 15:52:49 -0700661 static constexpr decltype(auto) __visit_value(_Vis&& __vis, _Vs&&... __vs) {
662 return __visit_alt(__make_value_visitor(_VSTD::forward<_Vis>(__vis)),
663 _VSTD::forward<_Vs>(__vs)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000664 }
665
666private:
Michael Park34321672020-08-11 15:52:49 -0700667 template <class _Vis, class... _Values>
668 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000669 static constexpr void __std_visit_exhaustive_visitor_check() {
Michael Park34321672020-08-11 15:52:49 -0700670 static_assert(is_invocable_v<_Vis, _Values...>,
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000671 "`std::visit` requires the visitor to be exhaustive.");
672 }
673
Michael Park34321672020-08-11 15:52:49 -0700674 template <class _Vis>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000675 struct __value_visitor {
676 template <class... _Alts>
677 inline _LIBCPP_INLINE_VISIBILITY
678 constexpr decltype(auto) operator()(_Alts&&... __alts) const {
679 __std_visit_exhaustive_visitor_check<
Michael Park34321672020-08-11 15:52:49 -0700680 _Vis, decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
681 return __invoke_constexpr(_VSTD::forward<_Vis>(__vis),
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000682 _VSTD::forward<_Alts>(__alts).__value...);
683 }
Michael Park34321672020-08-11 15:52:49 -0700684 _Vis&& __vis;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000685 };
686
Michael Park34321672020-08-11 15:52:49 -0700687 template <class _Vis>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000688 inline _LIBCPP_INLINE_VISIBILITY
Michael Park34321672020-08-11 15:52:49 -0700689 static constexpr auto __make_value_visitor(_Vis&& __vis) {
690 return __value_visitor<_Vis>{_VSTD::forward<_Vis>(__vis)};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000691 }
692};
693
Michael Park34321672020-08-11 15:52:49 -0700694#undef _LIBCPP_VARIANT_SWITCH_MAX
695#undef _LIBCPP_VARIANT_CASES
696#undef _LIBCPP_VARIANT_CASES_256
697#undef _LIBCPP_VARIANT_CASES_64
698#undef _LIBCPP_VARIANT_CASES_16
699#undef _LIBCPP_VARIANT_CASES_4
700
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000701} // namespace __visitation
702
703template <size_t _Index, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000704struct _LIBCPP_TEMPLATE_VIS __alt {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000705 using __value_type = _Tp;
706
707 template <class... _Args>
708 inline _LIBCPP_INLINE_VISIBILITY
709 explicit constexpr __alt(in_place_t, _Args&&... __args)
710 : __value(_VSTD::forward<_Args>(__args)...) {}
711
712 __value_type __value;
713};
714
715template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000716union _LIBCPP_TEMPLATE_VIS __union;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000717
718template <_Trait _DestructibleTrait, size_t _Index>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000719union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000720
721#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \
722 template <size_t _Index, class _Tp, class... _Types> \
Michael Park34321672020-08-11 15:52:49 -0700723 union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000724 _Index, \
725 _Tp, \
726 _Types...> { \
727 public: \
728 inline _LIBCPP_INLINE_VISIBILITY \
729 explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
730 \
731 template <class... _Args> \
732 inline _LIBCPP_INLINE_VISIBILITY \
733 explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
734 : __head(in_place, _VSTD::forward<_Args>(__args)...) {} \
735 \
736 template <size_t _Ip, class... _Args> \
737 inline _LIBCPP_INLINE_VISIBILITY \
738 explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
739 : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
740 \
741 __union(const __union&) = default; \
742 __union(__union&&) = default; \
743 \
744 destructor \
745 \
746 __union& operator=(const __union&) = default; \
747 __union& operator=(__union&&) = default; \
748 \
749 private: \
750 char __dummy; \
751 __alt<_Index, _Tp> __head; \
752 __union<destructible_trait, _Index + 1, _Types...> __tail; \
753 \
754 friend struct __access::__union; \
755 }
756
757_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
758_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
759_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
760
761#undef _LIBCPP_VARIANT_UNION
762
763template <_Trait _DestructibleTrait, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000764class _LIBCPP_TEMPLATE_VIS __base {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000765public:
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000766 using __index_t = __variant_index_t<sizeof...(_Types)>;
767
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000768 inline _LIBCPP_INLINE_VISIBILITY
769 explicit constexpr __base(__valueless_t tag) noexcept
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000770 : __data(tag), __index(__variant_npos<__index_t>) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000771
772 template <size_t _Ip, class... _Args>
773 inline _LIBCPP_INLINE_VISIBILITY
774 explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
Eric Fiselier2adf0872016-12-02 23:17:33 +0000775 :
776 __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
777 __index(_Ip) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000778
779 inline _LIBCPP_INLINE_VISIBILITY
780 constexpr bool valueless_by_exception() const noexcept {
781 return index() == variant_npos;
782 }
783
784 inline _LIBCPP_INLINE_VISIBILITY
785 constexpr size_t index() const noexcept {
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000786 return __index == __variant_npos<__index_t> ? variant_npos : __index;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000787 }
788
789protected:
790 inline _LIBCPP_INLINE_VISIBILITY
791 constexpr auto&& __as_base() & { return *this; }
792
793 inline _LIBCPP_INLINE_VISIBILITY
794 constexpr auto&& __as_base() && { return _VSTD::move(*this); }
795
796 inline _LIBCPP_INLINE_VISIBILITY
797 constexpr auto&& __as_base() const & { return *this; }
798
799 inline _LIBCPP_INLINE_VISIBILITY
800 constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
801
802 inline _LIBCPP_INLINE_VISIBILITY
803 static constexpr size_t __size() { return sizeof...(_Types); }
804
805 __union<_DestructibleTrait, 0, _Types...> __data;
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000806 __index_t __index;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000807
808 friend struct __access::__base;
809 friend struct __visitation::__base;
810};
811
812template <class _Traits, _Trait = _Traits::__destructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000813class _LIBCPP_TEMPLATE_VIS __destructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000814
815#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \
816 template <class... _Types> \
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000817 class _LIBCPP_TEMPLATE_VIS __destructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000818 destructible_trait> \
819 : public __base<destructible_trait, _Types...> { \
820 using __base_type = __base<destructible_trait, _Types...>; \
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000821 using __index_t = typename __base_type::__index_t; \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000822 \
823 public: \
824 using __base_type::__base_type; \
825 using __base_type::operator=; \
826 \
827 __destructor(const __destructor&) = default; \
828 __destructor(__destructor&&) = default; \
829 destructor \
830 __destructor& operator=(const __destructor&) = default; \
831 __destructor& operator=(__destructor&&) = default; \
832 \
833 protected: \
834 inline _LIBCPP_INLINE_VISIBILITY \
835 destroy \
836 }
837
838_LIBCPP_VARIANT_DESTRUCTOR(
839 _Trait::_TriviallyAvailable,
840 ~__destructor() = default;,
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000841 void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000842
843_LIBCPP_VARIANT_DESTRUCTOR(
844 _Trait::_Available,
845 ~__destructor() { __destroy(); },
846 void __destroy() noexcept {
847 if (!this->valueless_by_exception()) {
848 __visitation::__base::__visit_alt(
849 [](auto& __alt) noexcept {
Marshall Clowe61ba952018-02-12 15:41:25 +0000850 using __alt_type = __uncvref_t<decltype(__alt)>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000851 __alt.~__alt_type();
852 },
853 *this);
854 }
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000855 this->__index = __variant_npos<__index_t>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000856 });
857
858_LIBCPP_VARIANT_DESTRUCTOR(
859 _Trait::_Unavailable,
860 ~__destructor() = delete;,
861 void __destroy() noexcept = delete;);
862
863#undef _LIBCPP_VARIANT_DESTRUCTOR
864
865template <class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000866class _LIBCPP_TEMPLATE_VIS __constructor : public __destructor<_Traits> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000867 using __base_type = __destructor<_Traits>;
868
869public:
870 using __base_type::__base_type;
871 using __base_type::operator=;
872
873protected:
874 template <size_t _Ip, class _Tp, class... _Args>
875 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +0000876 static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
Michael Park34321672020-08-11 15:52:49 -0700877 auto* result = ::new ((void*)_VSTD::addressof(__a))
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000878 __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
Michael Park34321672020-08-11 15:52:49 -0700879 return result->__value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000880 }
881
882 template <class _Rhs>
883 inline _LIBCPP_INLINE_VISIBILITY
884 static void __generic_construct(__constructor& __lhs, _Rhs&& __rhs) {
885 __lhs.__destroy();
886 if (!__rhs.valueless_by_exception()) {
887 __visitation::__base::__visit_alt_at(
888 __rhs.index(),
889 [](auto& __lhs_alt, auto&& __rhs_alt) {
890 __construct_alt(
891 __lhs_alt,
892 _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
893 },
894 __lhs, _VSTD::forward<_Rhs>(__rhs));
895 __lhs.__index = __rhs.index();
896 }
897 }
898};
899
900template <class _Traits, _Trait = _Traits::__move_constructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000901class _LIBCPP_TEMPLATE_VIS __move_constructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000902
903#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \
904 move_constructor) \
905 template <class... _Types> \
Michael Park34321672020-08-11 15:52:49 -0700906 class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000907 move_constructible_trait> \
908 : public __constructor<__traits<_Types...>> { \
909 using __base_type = __constructor<__traits<_Types...>>; \
910 \
911 public: \
912 using __base_type::__base_type; \
913 using __base_type::operator=; \
914 \
915 __move_constructor(const __move_constructor&) = default; \
916 move_constructor \
917 ~__move_constructor() = default; \
918 __move_constructor& operator=(const __move_constructor&) = default; \
919 __move_constructor& operator=(__move_constructor&&) = default; \
920 }
921
922_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
923 _Trait::_TriviallyAvailable,
924 __move_constructor(__move_constructor&& __that) = default;);
925
926_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
927 _Trait::_Available,
928 __move_constructor(__move_constructor&& __that) noexcept(
929 __all<is_nothrow_move_constructible_v<_Types>...>::value)
930 : __move_constructor(__valueless_t{}) {
931 this->__generic_construct(*this, _VSTD::move(__that));
932 });
933
934_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
935 _Trait::_Unavailable,
936 __move_constructor(__move_constructor&&) = delete;);
937
938#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
939
940template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000941class _LIBCPP_TEMPLATE_VIS __copy_constructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000942
943#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \
944 copy_constructor) \
945 template <class... _Types> \
Michael Park34321672020-08-11 15:52:49 -0700946 class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000947 copy_constructible_trait> \
948 : public __move_constructor<__traits<_Types...>> { \
949 using __base_type = __move_constructor<__traits<_Types...>>; \
950 \
951 public: \
952 using __base_type::__base_type; \
953 using __base_type::operator=; \
954 \
955 copy_constructor \
956 __copy_constructor(__copy_constructor&&) = default; \
957 ~__copy_constructor() = default; \
958 __copy_constructor& operator=(const __copy_constructor&) = default; \
959 __copy_constructor& operator=(__copy_constructor&&) = default; \
960 }
961
962_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
963 _Trait::_TriviallyAvailable,
964 __copy_constructor(const __copy_constructor& __that) = default;);
965
966_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
967 _Trait::_Available,
968 __copy_constructor(const __copy_constructor& __that)
969 : __copy_constructor(__valueless_t{}) {
970 this->__generic_construct(*this, __that);
971 });
972
973_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
974 _Trait::_Unavailable,
975 __copy_constructor(const __copy_constructor&) = delete;);
976
977#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
978
979template <class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000980class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000981 using __base_type = __copy_constructor<_Traits>;
982
983public:
984 using __base_type::__base_type;
985 using __base_type::operator=;
986
987 template <size_t _Ip, class... _Args>
988 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +0000989 auto& __emplace(_Args&&... __args) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000990 this->__destroy();
Eric Fiselier62928442017-04-15 19:32:02 +0000991 auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
Michael Park34321672020-08-11 15:52:49 -0700992 _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000993 this->__index = _Ip;
Eric Fiselier62928442017-04-15 19:32:02 +0000994 return __res;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000995 }
996
997protected:
Michael Park62118352017-06-07 10:22:43 +0000998 template <size_t _Ip, class _Tp, class _Arg>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000999 inline _LIBCPP_INLINE_VISIBILITY
Michael Park62118352017-06-07 10:22:43 +00001000 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001001 if (this->index() == _Ip) {
1002 __a.__value = _VSTD::forward<_Arg>(__arg);
1003 } else {
1004 struct {
1005 void operator()(true_type) const {
Michael Park62118352017-06-07 10:22:43 +00001006 __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001007 }
1008 void operator()(false_type) const {
Michael Park62118352017-06-07 10:22:43 +00001009 __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001010 }
1011 __assignment* __this;
1012 _Arg&& __arg;
1013 } __impl{this, _VSTD::forward<_Arg>(__arg)};
Michael Park62118352017-06-07 10:22:43 +00001014 __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
1015 !is_nothrow_move_constructible_v<_Tp>>{});
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001016 }
1017 }
1018
1019 template <class _That>
1020 inline _LIBCPP_INLINE_VISIBILITY
1021 void __generic_assign(_That&& __that) {
1022 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1023 // do nothing.
1024 } else if (__that.valueless_by_exception()) {
1025 this->__destroy();
1026 } else {
1027 __visitation::__base::__visit_alt_at(
1028 __that.index(),
1029 [this](auto& __this_alt, auto&& __that_alt) {
1030 this->__assign_alt(
1031 __this_alt,
Michael Park62118352017-06-07 10:22:43 +00001032 _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001033 },
1034 *this, _VSTD::forward<_That>(__that));
1035 }
1036 }
1037};
1038
1039template <class _Traits, _Trait = _Traits::__move_assignable_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001040class _LIBCPP_TEMPLATE_VIS __move_assignment;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001041
1042#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \
1043 move_assignment) \
1044 template <class... _Types> \
Michael Park34321672020-08-11 15:52:49 -07001045 class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001046 move_assignable_trait> \
1047 : public __assignment<__traits<_Types...>> { \
1048 using __base_type = __assignment<__traits<_Types...>>; \
1049 \
1050 public: \
1051 using __base_type::__base_type; \
1052 using __base_type::operator=; \
1053 \
1054 __move_assignment(const __move_assignment&) = default; \
1055 __move_assignment(__move_assignment&&) = default; \
1056 ~__move_assignment() = default; \
1057 __move_assignment& operator=(const __move_assignment&) = default; \
1058 move_assignment \
1059 }
1060
1061_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
1062 _Trait::_TriviallyAvailable,
1063 __move_assignment& operator=(__move_assignment&& __that) = default;);
1064
1065_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
1066 _Trait::_Available,
1067 __move_assignment& operator=(__move_assignment&& __that) noexcept(
1068 __all<(is_nothrow_move_constructible_v<_Types> &&
1069 is_nothrow_move_assignable_v<_Types>)...>::value) {
1070 this->__generic_assign(_VSTD::move(__that));
1071 return *this;
1072 });
1073
1074_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
1075 _Trait::_Unavailable,
1076 __move_assignment& operator=(__move_assignment&&) = delete;);
1077
1078#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
1079
1080template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001081class _LIBCPP_TEMPLATE_VIS __copy_assignment;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001082
1083#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \
1084 copy_assignment) \
1085 template <class... _Types> \
Michael Park34321672020-08-11 15:52:49 -07001086 class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001087 copy_assignable_trait> \
1088 : public __move_assignment<__traits<_Types...>> { \
1089 using __base_type = __move_assignment<__traits<_Types...>>; \
1090 \
1091 public: \
1092 using __base_type::__base_type; \
1093 using __base_type::operator=; \
1094 \
1095 __copy_assignment(const __copy_assignment&) = default; \
1096 __copy_assignment(__copy_assignment&&) = default; \
1097 ~__copy_assignment() = default; \
1098 copy_assignment \
1099 __copy_assignment& operator=(__copy_assignment&&) = default; \
1100 }
1101
1102_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1103 _Trait::_TriviallyAvailable,
1104 __copy_assignment& operator=(const __copy_assignment& __that) = default;);
1105
1106_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1107 _Trait::_Available,
1108 __copy_assignment& operator=(const __copy_assignment& __that) {
1109 this->__generic_assign(__that);
1110 return *this;
1111 });
1112
1113_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1114 _Trait::_Unavailable,
1115 __copy_assignment& operator=(const __copy_assignment&) = delete;);
1116
1117#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1118
1119template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001120class _LIBCPP_TEMPLATE_VIS __impl
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001121 : public __copy_assignment<__traits<_Types...>> {
1122 using __base_type = __copy_assignment<__traits<_Types...>>;
1123
1124public:
1125 using __base_type::__base_type;
1126 using __base_type::operator=;
1127
1128 template <size_t _Ip, class _Arg>
1129 inline _LIBCPP_INLINE_VISIBILITY
1130 void __assign(_Arg&& __arg) {
1131 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
Michael Park62118352017-06-07 10:22:43 +00001132 _VSTD::forward<_Arg>(__arg));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001133 }
1134
1135 inline _LIBCPP_INLINE_VISIBILITY
1136 void __swap(__impl& __that) {
1137 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1138 // do nothing.
1139 } else if (this->index() == __that.index()) {
1140 __visitation::__base::__visit_alt_at(
1141 this->index(),
1142 [](auto& __this_alt, auto& __that_alt) {
1143 using _VSTD::swap;
1144 swap(__this_alt.__value, __that_alt.__value);
1145 },
1146 *this,
1147 __that);
1148 } else {
1149 __impl* __lhs = this;
1150 __impl* __rhs = _VSTD::addressof(__that);
1151 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1152 _VSTD::swap(__lhs, __rhs);
1153 }
1154 __impl __tmp(_VSTD::move(*__rhs));
Michael Park5dee7342020-06-16 15:15:10 -07001155#ifndef _LIBCPP_NO_EXCEPTIONS
Michael Park6a9ef642020-07-08 10:46:02 -07001156 if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
Michael Park31967aa2020-06-16 13:29:23 -07001157 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1158 } else {
1159 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1160 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1161 // into `__rhs` and provide the strong exception safety guarantee.
1162 try {
1163 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1164 } catch (...) {
1165 if (__tmp.__move_nothrow()) {
1166 this->__generic_construct(*__rhs, _VSTD::move(__tmp));
1167 }
1168 throw;
1169 }
1170 }
Michael Park5dee7342020-06-16 15:15:10 -07001171#else
1172 // this isn't consolidated with the `if constexpr` branch above due to
1173 // `throw` being ill-formed with exceptions disabled even when discarded.
1174 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1175#endif
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001176 this->__generic_construct(*__lhs, _VSTD::move(__tmp));
1177 }
1178 }
1179
1180private:
1181 inline _LIBCPP_INLINE_VISIBILITY
1182 bool __move_nothrow() const {
1183 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1184 return this->valueless_by_exception() || __results[this->index()];
1185 }
1186};
1187
Eric Fiselier3aaf8a32019-07-14 18:21:15 +00001188struct __no_narrowing_check {
1189 template <class _Dest, class _Source>
1190 using _Apply = __identity<_Dest>;
1191};
1192
1193struct __narrowing_check {
1194 template <class _Dest>
1195 static auto __test_impl(_Dest (&&)[1]) -> __identity<_Dest>;
1196 template <class _Dest, class _Source>
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001197 using _Apply _LIBCPP_NODEBUG_TYPE = decltype(__test_impl<_Dest>({std::declval<_Source>()}));
Eric Fiselier3aaf8a32019-07-14 18:21:15 +00001198};
1199
1200template <class _Dest, class _Source>
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001201using __check_for_narrowing _LIBCPP_NODEBUG_TYPE =
1202 typename _If<
Eric Fiselier3aaf8a32019-07-14 18:21:15 +00001203#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
1204 false &&
1205#endif
1206 is_arithmetic<_Dest>::value,
1207 __narrowing_check,
1208 __no_narrowing_check
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001209 >::template _Apply<_Dest, _Source>;
Eric Fiselier3aaf8a32019-07-14 18:21:15 +00001210
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001211template <class _Tp, size_t _Idx>
1212struct __overload {
Zhihao Yuan821efb82019-06-20 22:09:40 +00001213 template <class _Up>
Eric Fiselier80f5dfb2019-07-14 18:31:55 +00001214 auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001215};
1216
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001217template <class _Tp, size_t>
1218struct __overload_bool {
Zhihao Yuan821efb82019-06-20 22:09:40 +00001219 template <class _Up, class _Ap = __uncvref_t<_Up>>
1220 auto operator()(bool, _Up&&) const
1221 -> enable_if_t<is_same_v<_Ap, bool>, __identity<_Tp>>;
1222};
1223
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001224template <size_t _Idx>
1225struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
1226template <size_t _Idx>
1227struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
1228template <size_t _Idx>
1229struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
1230template <size_t _Idx>
1231struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
1232
1233template <class ..._Bases>
1234struct __all_overloads : _Bases... {
1235 void operator()() const;
1236 using _Bases::operator()...;
1237};
1238
1239template <class IdxSeq>
1240struct __make_overloads_imp;
1241
1242template <size_t ..._Idx>
1243struct __make_overloads_imp<__tuple_indices<_Idx...> > {
1244 template <class ..._Types>
1245 using _Apply _LIBCPP_NODEBUG_TYPE = __all_overloads<__overload<_Types, _Idx>...>;
1246};
1247
1248template <class ..._Types>
1249using _MakeOverloads _LIBCPP_NODEBUG_TYPE = typename __make_overloads_imp<
1250 __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
Zhihao Yuan821efb82019-06-20 22:09:40 +00001251
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001252template <class _Tp, class... _Types>
Zhihao Yuan821efb82019-06-20 22:09:40 +00001253using __best_match_t =
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001254 typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001255
1256} // __variant_detail
1257
1258template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001259class _LIBCPP_TEMPLATE_VIS variant
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001260 : private __sfinae_ctor_base<
1261 __all<is_copy_constructible_v<_Types>...>::value,
1262 __all<is_move_constructible_v<_Types>...>::value>,
1263 private __sfinae_assign_base<
1264 __all<(is_copy_constructible_v<_Types> &&
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001265 is_copy_assignable_v<_Types>)...>::value,
1266 __all<(is_move_constructible_v<_Types> &&
1267 is_move_assignable_v<_Types>)...>::value> {
1268 static_assert(0 < sizeof...(_Types),
1269 "variant must consist of at least one alternative.");
1270
1271 static_assert(__all<!is_array_v<_Types>...>::value,
1272 "variant can not have an array type as an alternative.");
1273
1274 static_assert(__all<!is_reference_v<_Types>...>::value,
1275 "variant can not have a reference type as an alternative.");
1276
1277 static_assert(__all<!is_void_v<_Types>...>::value,
1278 "variant can not have a void type as an alternative.");
1279
1280 using __first_type = variant_alternative_t<0, variant>;
1281
1282public:
1283 template <bool _Dummy = true,
1284 enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1285 _Dummy>::value,
1286 int> = 0>
1287 inline _LIBCPP_INLINE_VISIBILITY
1288 constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1289 : __impl(in_place_index<0>) {}
1290
1291 variant(const variant&) = default;
1292 variant(variant&&) = default;
1293
1294 template <
1295 class _Arg,
Marshall Clowd5bbc242018-02-06 20:56:55 +00001296 enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
1297 enable_if_t<!__is_inplace_type<__uncvref_t<_Arg>>::value, int> = 0,
1298 enable_if_t<!__is_inplace_index<__uncvref_t<_Arg>>::value, int> = 0,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001299 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1300 size_t _Ip =
1301 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1302 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1303 inline _LIBCPP_INLINE_VISIBILITY
1304 constexpr variant(_Arg&& __arg) noexcept(
1305 is_nothrow_constructible_v<_Tp, _Arg>)
1306 : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
1307
1308 template <size_t _Ip, class... _Args,
Eric Fiselier5d2df752018-03-23 23:42:30 +00001309 class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
1310 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1311 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001312 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5d2df752018-03-23 23:42:30 +00001313 explicit constexpr variant(
1314 in_place_index_t<_Ip>,
1315 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001316 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1317
Eric Fiselier5d2df752018-03-23 23:42:30 +00001318 template <
1319 size_t _Ip,
1320 class _Up,
1321 class... _Args,
1322 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1323 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001324 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1325 int> = 0>
1326 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5d2df752018-03-23 23:42:30 +00001327 explicit constexpr variant(
1328 in_place_index_t<_Ip>,
1329 initializer_list<_Up> __il,
1330 _Args&&... __args) noexcept(
1331 is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001332 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1333
1334 template <
1335 class _Tp,
1336 class... _Args,
1337 size_t _Ip =
1338 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1339 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1340 inline _LIBCPP_INLINE_VISIBILITY
1341 explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1342 is_nothrow_constructible_v<_Tp, _Args...>)
1343 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1344
1345 template <
1346 class _Tp,
1347 class _Up,
1348 class... _Args,
1349 size_t _Ip =
1350 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1351 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1352 int> = 0>
1353 inline _LIBCPP_INLINE_VISIBILITY
1354 explicit constexpr variant(
1355 in_place_type_t<_Tp>,
1356 initializer_list<_Up> __il,
1357 _Args&&... __args) noexcept(
1358 is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1359 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1360
1361 ~variant() = default;
1362
1363 variant& operator=(const variant&) = default;
1364 variant& operator=(variant&&) = default;
1365
1366 template <
1367 class _Arg,
Marshall Clowd5bbc242018-02-06 20:56:55 +00001368 enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001369 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1370 size_t _Ip =
1371 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1372 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
1373 int> = 0>
1374 inline _LIBCPP_INLINE_VISIBILITY
1375 variant& operator=(_Arg&& __arg) noexcept(
1376 is_nothrow_assignable_v<_Tp&, _Arg> &&
1377 is_nothrow_constructible_v<_Tp, _Arg>) {
1378 __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
1379 return *this;
1380 }
1381
1382 template <
1383 size_t _Ip,
1384 class... _Args,
1385 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1386 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1387 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1388 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001389 _Tp& emplace(_Args&&... __args) {
1390 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001391 }
1392
1393 template <
1394 size_t _Ip,
1395 class _Up,
1396 class... _Args,
1397 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1398 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1399 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1400 int> = 0>
1401 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001402 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1403 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001404 }
1405
1406 template <
1407 class _Tp,
1408 class... _Args,
1409 size_t _Ip =
1410 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1411 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1412 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001413 _Tp& emplace(_Args&&... __args) {
1414 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001415 }
1416
1417 template <
1418 class _Tp,
1419 class _Up,
1420 class... _Args,
1421 size_t _Ip =
1422 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1423 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1424 int> = 0>
1425 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001426 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1427 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001428 }
1429
1430 inline _LIBCPP_INLINE_VISIBILITY
1431 constexpr bool valueless_by_exception() const noexcept {
1432 return __impl.valueless_by_exception();
1433 }
1434
1435 inline _LIBCPP_INLINE_VISIBILITY
1436 constexpr size_t index() const noexcept { return __impl.index(); }
1437
1438 template <
1439 bool _Dummy = true,
1440 enable_if_t<
1441 __all<(
1442 __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1443 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1444 int> = 0>
1445 inline _LIBCPP_INLINE_VISIBILITY
1446 void swap(variant& __that) noexcept(
1447 __all<(is_nothrow_move_constructible_v<_Types> &&
1448 is_nothrow_swappable_v<_Types>)...>::value) {
1449 __impl.__swap(__that.__impl);
1450 }
1451
1452private:
1453 __variant_detail::__impl<_Types...> __impl;
1454
1455 friend struct __variant_detail::__access::__variant;
1456 friend struct __variant_detail::__visitation::__variant;
1457};
1458
1459template <size_t _Ip, class... _Types>
1460inline _LIBCPP_INLINE_VISIBILITY
1461constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1462 return __v.index() == _Ip;
1463}
1464
1465template <class _Tp, class... _Types>
1466inline _LIBCPP_INLINE_VISIBILITY
1467constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1468 return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1469}
1470
1471template <size_t _Ip, class _Vp>
1472inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001473_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Richard Smith66d1c552018-08-27 21:41:50 +00001474constexpr auto&& __generic_get(_Vp&& __v) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001475 using __variant_detail::__access::__variant;
1476 if (!__holds_alternative<_Ip>(__v)) {
Eric Fiselier10642ea2016-12-03 01:58:07 +00001477 __throw_bad_variant_access();
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001478 }
1479 return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
1480}
1481
1482template <size_t _Ip, class... _Types>
1483inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001484_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001485constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
1486 variant<_Types...>& __v) {
1487 static_assert(_Ip < sizeof...(_Types));
1488 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1489 return __generic_get<_Ip>(__v);
1490}
1491
1492template <size_t _Ip, class... _Types>
1493inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001494_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001495constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
1496 variant<_Types...>&& __v) {
1497 static_assert(_Ip < sizeof...(_Types));
1498 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1499 return __generic_get<_Ip>(_VSTD::move(__v));
1500}
1501
1502template <size_t _Ip, class... _Types>
1503inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001504_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001505constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
1506 const variant<_Types...>& __v) {
1507 static_assert(_Ip < sizeof...(_Types));
1508 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1509 return __generic_get<_Ip>(__v);
1510}
1511
1512template <size_t _Ip, class... _Types>
1513inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001514_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001515constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
1516 const variant<_Types...>&& __v) {
1517 static_assert(_Ip < sizeof...(_Types));
1518 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1519 return __generic_get<_Ip>(_VSTD::move(__v));
1520}
1521
1522template <class _Tp, class... _Types>
1523inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001524_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001525constexpr _Tp& get(variant<_Types...>& __v) {
1526 static_assert(!is_void_v<_Tp>);
1527 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1528}
1529
1530template <class _Tp, class... _Types>
1531inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001532_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001533constexpr _Tp&& get(variant<_Types...>&& __v) {
1534 static_assert(!is_void_v<_Tp>);
1535 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1536 _VSTD::move(__v));
1537}
1538
1539template <class _Tp, class... _Types>
1540inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001541_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001542constexpr const _Tp& get(const variant<_Types...>& __v) {
1543 static_assert(!is_void_v<_Tp>);
1544 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1545}
1546
1547template <class _Tp, class... _Types>
1548inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001549_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001550constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1551 static_assert(!is_void_v<_Tp>);
1552 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1553 _VSTD::move(__v));
1554}
1555
1556template <size_t _Ip, class _Vp>
1557inline _LIBCPP_INLINE_VISIBILITY
1558constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1559 using __variant_detail::__access::__variant;
1560 return __v && __holds_alternative<_Ip>(*__v)
1561 ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
1562 : nullptr;
1563}
1564
1565template <size_t _Ip, class... _Types>
1566inline _LIBCPP_INLINE_VISIBILITY
1567constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1568get_if(variant<_Types...>* __v) noexcept {
1569 static_assert(_Ip < sizeof...(_Types));
1570 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1571 return __generic_get_if<_Ip>(__v);
1572}
1573
1574template <size_t _Ip, class... _Types>
1575inline _LIBCPP_INLINE_VISIBILITY
1576constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1577get_if(const variant<_Types...>* __v) noexcept {
1578 static_assert(_Ip < sizeof...(_Types));
1579 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1580 return __generic_get_if<_Ip>(__v);
1581}
1582
1583template <class _Tp, class... _Types>
1584inline _LIBCPP_INLINE_VISIBILITY
1585constexpr add_pointer_t<_Tp>
1586get_if(variant<_Types...>* __v) noexcept {
1587 static_assert(!is_void_v<_Tp>);
1588 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1589}
1590
1591template <class _Tp, class... _Types>
1592inline _LIBCPP_INLINE_VISIBILITY
1593constexpr add_pointer_t<const _Tp>
1594get_if(const variant<_Types...>* __v) noexcept {
1595 static_assert(!is_void_v<_Tp>);
1596 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1597}
1598
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001599template <class _Operator>
1600struct __convert_to_bool {
1601 template <class _T1, class _T2>
1602 _LIBCPP_INLINE_VISIBILITY constexpr bool operator()(_T1 && __t1, _T2&& __t2) const {
1603 static_assert(std::is_convertible<decltype(_Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2))), bool>::value,
1604 "the relational operator does not return a type which is implicitly convertible to bool");
1605 return _Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
1606 }
1607};
1608
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001609template <class... _Types>
1610inline _LIBCPP_INLINE_VISIBILITY
1611constexpr bool operator==(const variant<_Types...>& __lhs,
1612 const variant<_Types...>& __rhs) {
1613 using __variant_detail::__visitation::__variant;
1614 if (__lhs.index() != __rhs.index()) return false;
1615 if (__lhs.valueless_by_exception()) return true;
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001616 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001617}
1618
1619template <class... _Types>
1620inline _LIBCPP_INLINE_VISIBILITY
1621constexpr bool operator!=(const variant<_Types...>& __lhs,
1622 const variant<_Types...>& __rhs) {
1623 using __variant_detail::__visitation::__variant;
1624 if (__lhs.index() != __rhs.index()) return true;
1625 if (__lhs.valueless_by_exception()) return false;
1626 return __variant::__visit_value_at(
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001627 __lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001628}
1629
1630template <class... _Types>
1631inline _LIBCPP_INLINE_VISIBILITY
1632constexpr bool operator<(const variant<_Types...>& __lhs,
1633 const variant<_Types...>& __rhs) {
1634 using __variant_detail::__visitation::__variant;
1635 if (__rhs.valueless_by_exception()) return false;
1636 if (__lhs.valueless_by_exception()) return true;
1637 if (__lhs.index() < __rhs.index()) return true;
1638 if (__lhs.index() > __rhs.index()) return false;
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001639 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001640}
1641
1642template <class... _Types>
1643inline _LIBCPP_INLINE_VISIBILITY
1644constexpr bool operator>(const variant<_Types...>& __lhs,
1645 const variant<_Types...>& __rhs) {
1646 using __variant_detail::__visitation::__variant;
1647 if (__lhs.valueless_by_exception()) return false;
1648 if (__rhs.valueless_by_exception()) return true;
1649 if (__lhs.index() > __rhs.index()) return true;
1650 if (__lhs.index() < __rhs.index()) return false;
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001651 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001652}
1653
1654template <class... _Types>
1655inline _LIBCPP_INLINE_VISIBILITY
1656constexpr bool operator<=(const variant<_Types...>& __lhs,
1657 const variant<_Types...>& __rhs) {
1658 using __variant_detail::__visitation::__variant;
1659 if (__lhs.valueless_by_exception()) return true;
1660 if (__rhs.valueless_by_exception()) return false;
1661 if (__lhs.index() < __rhs.index()) return true;
1662 if (__lhs.index() > __rhs.index()) return false;
1663 return __variant::__visit_value_at(
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001664 __lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001665}
1666
1667template <class... _Types>
1668inline _LIBCPP_INLINE_VISIBILITY
1669constexpr bool operator>=(const variant<_Types...>& __lhs,
1670 const variant<_Types...>& __rhs) {
1671 using __variant_detail::__visitation::__variant;
1672 if (__rhs.valueless_by_exception()) return true;
1673 if (__lhs.valueless_by_exception()) return false;
1674 if (__lhs.index() > __rhs.index()) return true;
1675 if (__lhs.index() < __rhs.index()) return false;
1676 return __variant::__visit_value_at(
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001677 __lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001678}
1679
Michael Park34321672020-08-11 15:52:49 -07001680template <class _Vis, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001681inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001682_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Michael Park34321672020-08-11 15:52:49 -07001683constexpr decltype(auto) visit(_Vis&& __vis, _Vs&&... __vs) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001684 using __variant_detail::__visitation::__variant;
Michael Park34321672020-08-11 15:52:49 -07001685 return __variant::__visit_value(_VSTD::forward<_Vis>(__vis),
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001686 _VSTD::forward<_Vs>(__vs)...);
1687}
1688
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001689struct _LIBCPP_TEMPLATE_VIS monostate {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001690
1691inline _LIBCPP_INLINE_VISIBILITY
1692constexpr bool operator<(monostate, monostate) noexcept { return false; }
1693
1694inline _LIBCPP_INLINE_VISIBILITY
1695constexpr bool operator>(monostate, monostate) noexcept { return false; }
1696
1697inline _LIBCPP_INLINE_VISIBILITY
1698constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1699
1700inline _LIBCPP_INLINE_VISIBILITY
1701constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1702
1703inline _LIBCPP_INLINE_VISIBILITY
1704constexpr bool operator==(monostate, monostate) noexcept { return true; }
1705
1706inline _LIBCPP_INLINE_VISIBILITY
1707constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1708
1709template <class... _Types>
1710inline _LIBCPP_INLINE_VISIBILITY
1711auto swap(variant<_Types...>& __lhs,
1712 variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1713 -> decltype(__lhs.swap(__rhs)) {
1714 __lhs.swap(__rhs);
1715}
1716
1717template <class... _Types>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001718struct _LIBCPP_TEMPLATE_VIS hash<
1719 __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001720 using argument_type = variant<_Types...>;
1721 using result_type = size_t;
1722
1723 inline _LIBCPP_INLINE_VISIBILITY
1724 result_type operator()(const argument_type& __v) const {
1725 using __variant_detail::__visitation::__variant;
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001726 size_t __res =
1727 __v.valueless_by_exception()
Eric Fiselier6b1683c2016-12-04 21:37:37 +00001728 ? 299792458 // Random value chosen by the universe upon creation
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001729 : __variant::__visit_alt(
1730 [](const auto& __alt) {
Marshall Clowe61ba952018-02-12 15:41:25 +00001731 using __alt_type = __uncvref_t<decltype(__alt)>;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001732 using __value_type = remove_const_t<
1733 typename __alt_type::__value_type>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001734 return hash<__value_type>{}(__alt.__value);
1735 },
1736 __v);
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001737 return __hash_combine(__res, hash<size_t>{}(__v.index()));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001738 }
1739};
1740
1741template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001742struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001743 using argument_type = monostate;
1744 using result_type = size_t;
1745
1746 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001747 result_type operator()(const argument_type&) const _NOEXCEPT {
Eric Fiselier6b1683c2016-12-04 21:37:37 +00001748 return 66740831; // return a fundamentally attractive random value.
1749 }
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001750};
1751
1752#endif // _LIBCPP_STD_VER > 14
1753
1754_LIBCPP_END_NAMESPACE_STD
1755
Eric Fiselier8625d332017-11-19 04:57:22 +00001756_LIBCPP_POP_MACROS
1757
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001758#endif // _LIBCPP_VARIANT