blob: cb92a69d14d1894874dcfc8a2038fb3710d3ada3 [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>
Louis Dionne73912b22020-11-04 15:01:25 -0500200#include <__availability>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000201#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>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000211#include <version>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000212
213#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
214#pragma GCC system_header
215#endif
216
Eric Fiselier8625d332017-11-19 04:57:22 +0000217_LIBCPP_PUSH_MACROS
218#include <__undef_macros>
219
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000220namespace std { // explicitly not using versioning namespace
221
Louis Dionnecef92e62018-11-19 15:37:04 +0000222class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000223public:
Saleem Abdulrasool5ee83382016-12-31 17:34:26 +0000224 virtual const char* what() const _NOEXCEPT;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000225};
226
227} // namespace std
228
229_LIBCPP_BEGIN_NAMESPACE_STD
230
Louis Dionne7202cd62020-07-22 15:24:16 -0400231// TODO: GCC 5 lies about its support for C++17 (it says it supports it but it
232// really doesn't). That breaks variant, which uses some C++17 features.
233// Remove this once we drop support for GCC 5.
Louis Dionne129391d2020-07-22 15:59:09 -0400234#if _LIBCPP_STD_VER > 14 && !(defined(_LIBCPP_COMPILER_GCC) && _GNUC_VER_NEW < 6000)
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000235
Eric Fiselier10642ea2016-12-03 01:58:07 +0000236_LIBCPP_NORETURN
237inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +0000238_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier10642ea2016-12-03 01:58:07 +0000239void __throw_bad_variant_access() {
240#ifndef _LIBCPP_NO_EXCEPTIONS
241 throw bad_variant_access();
242#else
243 _VSTD::abort();
244#endif
245}
246
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000247template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000248class _LIBCPP_TEMPLATE_VIS variant;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000249
250template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000251struct _LIBCPP_TEMPLATE_VIS variant_size;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000252
253template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000254_LIBCPP_INLINE_VAR constexpr size_t variant_size_v = variant_size<_Tp>::value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000255
256template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000257struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000258
259template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000260struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000261
262template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000263struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000264 : variant_size<_Tp> {};
265
266template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000267struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000268 : integral_constant<size_t, sizeof...(_Types)> {};
269
270template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000271struct _LIBCPP_TEMPLATE_VIS variant_alternative;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000272
273template <size_t _Ip, class _Tp>
274using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
275
276template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000277struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000278 : add_const<variant_alternative_t<_Ip, _Tp>> {};
279
280template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000281struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000282 : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
283
284template <size_t _Ip, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000285struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000286 : add_cv<variant_alternative_t<_Ip, _Tp>> {};
287
288template <size_t _Ip, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000289struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
Marshall Clow185577f2017-06-12 16:13:17 +0000290 static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000291 using type = __type_pack_element<_Ip, _Types...>;
292};
293
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000294_LIBCPP_INLINE_VAR constexpr size_t variant_npos = static_cast<size_t>(-1);
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000295
296constexpr int __choose_index_type(unsigned int __num_elem) {
297 if (__num_elem < std::numeric_limits<unsigned char>::max())
298 return 0;
299 if (__num_elem < std::numeric_limits<unsigned short>::max())
300 return 1;
301 return 2;
302}
303
304template <size_t _NumAlts>
305using __variant_index_t =
306#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
307 unsigned int;
308#else
309 std::tuple_element_t<
310 __choose_index_type(_NumAlts),
311 std::tuple<unsigned char, unsigned short, unsigned int>
312 >;
313#endif
314
315template <class _IndexType>
316constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000317
318namespace __find_detail {
319
320template <class _Tp, class... _Types>
321inline _LIBCPP_INLINE_VISIBILITY
322constexpr size_t __find_index() {
323 constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
324 size_t __result = __not_found;
325 for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
326 if (__matches[__i]) {
327 if (__result != __not_found) {
328 return __ambiguous;
329 }
330 __result = __i;
331 }
332 }
333 return __result;
334}
335
336template <size_t _Index>
337struct __find_unambiguous_index_sfinae_impl
338 : integral_constant<size_t, _Index> {};
339
340template <>
341struct __find_unambiguous_index_sfinae_impl<__not_found> {};
342
343template <>
344struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
345
346template <class _Tp, class... _Types>
347struct __find_unambiguous_index_sfinae
348 : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
349
350} // namespace __find_detail
351
352namespace __variant_detail {
353
354struct __valueless_t {};
355
356enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
357
Eric Fiselier87994112020-11-17 17:34:23 -0500358template <typename _Tp,
359 template <typename> class _IsTriviallyAvailable,
360 template <typename> class _IsAvailable>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000361constexpr _Trait __trait =
362 _IsTriviallyAvailable<_Tp>::value
363 ? _Trait::_TriviallyAvailable
364 : _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
365
366inline _LIBCPP_INLINE_VISIBILITY
367constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
368 _Trait __result = _Trait::_TriviallyAvailable;
369 for (_Trait __t : __traits) {
370 if (static_cast<int>(__t) > static_cast<int>(__result)) {
371 __result = __t;
372 }
373 }
374 return __result;
375}
376
Eric Fiselier87994112020-11-17 17:34:23 -0500377template <typename... _Types>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000378struct __traits {
379 static constexpr _Trait __copy_constructible_trait =
380 __common_trait({__trait<_Types,
381 is_trivially_copy_constructible,
382 is_copy_constructible>...});
383
384 static constexpr _Trait __move_constructible_trait =
385 __common_trait({__trait<_Types,
386 is_trivially_move_constructible,
387 is_move_constructible>...});
388
389 static constexpr _Trait __copy_assignable_trait = __common_trait(
390 {__copy_constructible_trait,
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000391 __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
392
393 static constexpr _Trait __move_assignable_trait = __common_trait(
394 {__move_constructible_trait,
395 __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
396
397 static constexpr _Trait __destructible_trait = __common_trait(
398 {__trait<_Types, is_trivially_destructible, is_destructible>...});
399};
400
401namespace __access {
402
403struct __union {
404 template <class _Vp>
405 inline _LIBCPP_INLINE_VISIBILITY
406 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
407 return _VSTD::forward<_Vp>(__v).__head;
408 }
409
410 template <class _Vp, size_t _Ip>
411 inline _LIBCPP_INLINE_VISIBILITY
412 static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
413 return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
414 }
415};
416
417struct __base {
418 template <size_t _Ip, class _Vp>
419 inline _LIBCPP_INLINE_VISIBILITY
420 static constexpr auto&& __get_alt(_Vp&& __v) {
421 return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
422 in_place_index<_Ip>);
423 }
424};
425
426struct __variant {
427 template <size_t _Ip, class _Vp>
428 inline _LIBCPP_INLINE_VISIBILITY
429 static constexpr auto&& __get_alt(_Vp&& __v) {
430 return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
431 }
432};
433
434} // namespace __access
435
436namespace __visitation {
437
438struct __base {
Eric Fiselier87994112020-11-17 17:34:23 -0500439 template <class _Visitor, class... _Vs>
440 inline _LIBCPP_INLINE_VISIBILITY
441 static constexpr decltype(auto)
442 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
443 constexpr auto __fdiagonal =
444 __make_fdiagonal<_Visitor&&,
445 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
446 return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
447 _VSTD::forward<_Vs>(__vs).__as_base()...);
448 }
449
450 template <class _Visitor, class... _Vs>
451 inline _LIBCPP_INLINE_VISIBILITY
452 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
453 _Vs&&... __vs) {
454 constexpr auto __fmatrix =
455 __make_fmatrix<_Visitor&&,
456 decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
457 return __at(__fmatrix, __vs.index()...)(
458 _VSTD::forward<_Visitor>(__visitor),
459 _VSTD::forward<_Vs>(__vs).__as_base()...);
460 }
461
462private:
463 template <class _Tp>
464 inline _LIBCPP_INLINE_VISIBILITY
465 static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
466
467 template <class _Tp, size_t _Np, typename... _Indices>
468 inline _LIBCPP_INLINE_VISIBILITY
469 static constexpr auto&& __at(const array<_Tp, _Np>& __elems,
470 size_t __index, _Indices... __indices) {
471 return __at(__elems[__index], __indices...);
472 }
473
474 template <class _Fp, class... _Fs>
475 static constexpr void __std_visit_visitor_return_type_check() {
476 static_assert(
477 __all<is_same_v<_Fp, _Fs>...>::value,
478 "`std::visit` requires the visitor to have a single return type.");
479 }
480
481 template <class... _Fs>
482 inline _LIBCPP_INLINE_VISIBILITY
483 static constexpr auto __make_farray(_Fs&&... __fs) {
484 __std_visit_visitor_return_type_check<__uncvref_t<_Fs>...>();
485 using __result = array<common_type_t<__uncvref_t<_Fs>...>, sizeof...(_Fs)>;
486 return __result{{_VSTD::forward<_Fs>(__fs)...}};
487 }
488
489 template <std::size_t... _Is>
490 struct __dispatcher {
491 template <class _Fp, class... _Vs>
Michael Park32334da2020-08-30 12:42:35 -0400492 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier87994112020-11-17 17:34:23 -0500493 static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
Michael Park32334da2020-08-30 12:42:35 -0400494 return __invoke_constexpr(
Eric Fiselier87994112020-11-17 17:34:23 -0500495 static_cast<_Fp>(__f),
496 __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
Michael Park32334da2020-08-30 12:42:35 -0400497 }
498 };
499
Eric Fiselier87994112020-11-17 17:34:23 -0500500 template <class _Fp, class... _Vs, size_t... _Is>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000501 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier87994112020-11-17 17:34:23 -0500502 static constexpr auto __make_dispatch(index_sequence<_Is...>) {
503 return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000504 }
505
Eric Fiselier87994112020-11-17 17:34:23 -0500506 template <size_t _Ip, class _Fp, class... _Vs>
507 inline _LIBCPP_INLINE_VISIBILITY
508 static constexpr auto __make_fdiagonal_impl() {
509 return __make_dispatch<_Fp, _Vs...>(
510 index_sequence<(__identity<_Vs>{}, _Ip)...>{});
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000511 }
Eric Fiselier87994112020-11-17 17:34:23 -0500512
513 template <class _Fp, class... _Vs, size_t... _Is>
514 inline _LIBCPP_INLINE_VISIBILITY
515 static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
516 return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
517 }
518
519 template <class _Fp, class _Vp, class... _Vs>
520 inline _LIBCPP_INLINE_VISIBILITY
521 static constexpr auto __make_fdiagonal() {
522 constexpr size_t _Np = __uncvref_t<_Vp>::__size();
523 static_assert(__all<(_Np == __uncvref_t<_Vs>::__size())...>::value);
524 return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
525 }
526
527 template <class _Fp, class... _Vs, size_t... _Is>
528 inline _LIBCPP_INLINE_VISIBILITY
529 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
530 return __make_dispatch<_Fp, _Vs...>(__is);
531 }
532
533 template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
534 inline _LIBCPP_INLINE_VISIBILITY
535 static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
536 index_sequence<_Js...>,
537 _Ls... __ls) {
538 return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
539 index_sequence<_Is..., _Js>{}, __ls...)...);
540 }
541
542 template <class _Fp, class... _Vs>
543 inline _LIBCPP_INLINE_VISIBILITY
544 static constexpr auto __make_fmatrix() {
545 return __make_fmatrix_impl<_Fp, _Vs...>(
546 index_sequence<>{}, make_index_sequence<__uncvref_t<_Vs>::__size()>{}...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000547 }
548};
549
550struct __variant {
Eric Fiselier87994112020-11-17 17:34:23 -0500551 template <class _Visitor, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000552 inline _LIBCPP_INLINE_VISIBILITY
553 static constexpr decltype(auto)
Eric Fiselier87994112020-11-17 17:34:23 -0500554 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000555 return __base::__visit_alt_at(__index,
Eric Fiselier87994112020-11-17 17:34:23 -0500556 _VSTD::forward<_Visitor>(__visitor),
557 _VSTD::forward<_Vs>(__vs).__impl...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000558 }
559
Eric Fiselier87994112020-11-17 17:34:23 -0500560 template <class _Visitor, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000561 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier87994112020-11-17 17:34:23 -0500562 static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
563 _Vs&&... __vs) {
564 return __base::__visit_alt(_VSTD::forward<_Visitor>(__visitor),
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000565 _VSTD::forward<_Vs>(__vs).__impl...);
566 }
567
Eric Fiselier87994112020-11-17 17:34:23 -0500568 template <class _Visitor, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000569 inline _LIBCPP_INLINE_VISIBILITY
570 static constexpr decltype(auto)
Eric Fiselier87994112020-11-17 17:34:23 -0500571 __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
572 return __visit_alt_at(
573 __index,
574 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
575 _VSTD::forward<_Vs>(__vs)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000576 }
577
Eric Fiselier87994112020-11-17 17:34:23 -0500578 template <class _Visitor, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000579 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier87994112020-11-17 17:34:23 -0500580 static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
581 _Vs&&... __vs) {
582 return __visit_alt(
583 __make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
584 _VSTD::forward<_Vs>(__vs)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000585 }
586
587private:
Eric Fiselier87994112020-11-17 17:34:23 -0500588 template <class _Visitor, class... _Values>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000589 static constexpr void __std_visit_exhaustive_visitor_check() {
Eric Fiselier87994112020-11-17 17:34:23 -0500590 static_assert(is_invocable_v<_Visitor, _Values...>,
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000591 "`std::visit` requires the visitor to be exhaustive.");
592 }
593
Eric Fiselier87994112020-11-17 17:34:23 -0500594 template <class _Visitor>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000595 struct __value_visitor {
596 template <class... _Alts>
597 inline _LIBCPP_INLINE_VISIBILITY
598 constexpr decltype(auto) operator()(_Alts&&... __alts) const {
599 __std_visit_exhaustive_visitor_check<
Eric Fiselier87994112020-11-17 17:34:23 -0500600 _Visitor,
601 decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
602 return __invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000603 _VSTD::forward<_Alts>(__alts).__value...);
604 }
Eric Fiselier87994112020-11-17 17:34:23 -0500605 _Visitor&& __visitor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000606 };
607
Eric Fiselier87994112020-11-17 17:34:23 -0500608 template <class _Visitor>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000609 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier87994112020-11-17 17:34:23 -0500610 static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
611 return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000612 }
613};
614
615} // namespace __visitation
616
617template <size_t _Index, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000618struct _LIBCPP_TEMPLATE_VIS __alt {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000619 using __value_type = _Tp;
620
621 template <class... _Args>
622 inline _LIBCPP_INLINE_VISIBILITY
623 explicit constexpr __alt(in_place_t, _Args&&... __args)
624 : __value(_VSTD::forward<_Args>(__args)...) {}
625
626 __value_type __value;
627};
628
629template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000630union _LIBCPP_TEMPLATE_VIS __union;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000631
632template <_Trait _DestructibleTrait, size_t _Index>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000633union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000634
635#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \
636 template <size_t _Index, class _Tp, class... _Types> \
Eric Fiselier87994112020-11-17 17:34:23 -0500637 union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000638 _Index, \
639 _Tp, \
640 _Types...> { \
641 public: \
642 inline _LIBCPP_INLINE_VISIBILITY \
643 explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
644 \
645 template <class... _Args> \
646 inline _LIBCPP_INLINE_VISIBILITY \
647 explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
648 : __head(in_place, _VSTD::forward<_Args>(__args)...) {} \
649 \
650 template <size_t _Ip, class... _Args> \
651 inline _LIBCPP_INLINE_VISIBILITY \
652 explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
653 : __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
654 \
655 __union(const __union&) = default; \
656 __union(__union&&) = default; \
657 \
658 destructor \
659 \
660 __union& operator=(const __union&) = default; \
661 __union& operator=(__union&&) = default; \
662 \
663 private: \
664 char __dummy; \
665 __alt<_Index, _Tp> __head; \
666 __union<destructible_trait, _Index + 1, _Types...> __tail; \
667 \
668 friend struct __access::__union; \
669 }
670
671_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
672_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
673_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
674
675#undef _LIBCPP_VARIANT_UNION
676
677template <_Trait _DestructibleTrait, class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000678class _LIBCPP_TEMPLATE_VIS __base {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000679public:
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000680 using __index_t = __variant_index_t<sizeof...(_Types)>;
681
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000682 inline _LIBCPP_INLINE_VISIBILITY
683 explicit constexpr __base(__valueless_t tag) noexcept
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000684 : __data(tag), __index(__variant_npos<__index_t>) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000685
686 template <size_t _Ip, class... _Args>
687 inline _LIBCPP_INLINE_VISIBILITY
688 explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
Eric Fiselier2adf0872016-12-02 23:17:33 +0000689 :
690 __data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
691 __index(_Ip) {}
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000692
693 inline _LIBCPP_INLINE_VISIBILITY
694 constexpr bool valueless_by_exception() const noexcept {
695 return index() == variant_npos;
696 }
697
698 inline _LIBCPP_INLINE_VISIBILITY
699 constexpr size_t index() const noexcept {
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000700 return __index == __variant_npos<__index_t> ? variant_npos : __index;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000701 }
702
703protected:
704 inline _LIBCPP_INLINE_VISIBILITY
705 constexpr auto&& __as_base() & { return *this; }
706
707 inline _LIBCPP_INLINE_VISIBILITY
708 constexpr auto&& __as_base() && { return _VSTD::move(*this); }
709
710 inline _LIBCPP_INLINE_VISIBILITY
711 constexpr auto&& __as_base() const & { return *this; }
712
713 inline _LIBCPP_INLINE_VISIBILITY
714 constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
715
716 inline _LIBCPP_INLINE_VISIBILITY
717 static constexpr size_t __size() { return sizeof...(_Types); }
718
719 __union<_DestructibleTrait, 0, _Types...> __data;
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000720 __index_t __index;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000721
722 friend struct __access::__base;
723 friend struct __visitation::__base;
724};
725
726template <class _Traits, _Trait = _Traits::__destructible_trait>
Louis Dionneff05add2020-10-05 16:30:23 -0400727class _LIBCPP_TEMPLATE_VIS __dtor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000728
729#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \
730 template <class... _Types> \
Louis Dionneff05add2020-10-05 16:30:23 -0400731 class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, \
732 destructible_trait> \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000733 : public __base<destructible_trait, _Types...> { \
734 using __base_type = __base<destructible_trait, _Types...>; \
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000735 using __index_t = typename __base_type::__index_t; \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000736 \
737 public: \
738 using __base_type::__base_type; \
739 using __base_type::operator=; \
740 \
Louis Dionneff05add2020-10-05 16:30:23 -0400741 __dtor(const __dtor&) = default; \
742 __dtor(__dtor&&) = default; \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000743 destructor \
Louis Dionneff05add2020-10-05 16:30:23 -0400744 __dtor& operator=(const __dtor&) = default; \
745 __dtor& operator=(__dtor&&) = default; \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000746 \
747 protected: \
748 inline _LIBCPP_INLINE_VISIBILITY \
749 destroy \
750 }
751
752_LIBCPP_VARIANT_DESTRUCTOR(
753 _Trait::_TriviallyAvailable,
Louis Dionneff05add2020-10-05 16:30:23 -0400754 ~__dtor() = default;,
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000755 void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000756
757_LIBCPP_VARIANT_DESTRUCTOR(
758 _Trait::_Available,
Louis Dionneff05add2020-10-05 16:30:23 -0400759 ~__dtor() { __destroy(); },
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000760 void __destroy() noexcept {
761 if (!this->valueless_by_exception()) {
762 __visitation::__base::__visit_alt(
763 [](auto& __alt) noexcept {
Marshall Clowe61ba952018-02-12 15:41:25 +0000764 using __alt_type = __uncvref_t<decltype(__alt)>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000765 __alt.~__alt_type();
766 },
767 *this);
768 }
Eric Fiseliercaccc7b2017-11-19 04:19:44 +0000769 this->__index = __variant_npos<__index_t>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000770 });
771
772_LIBCPP_VARIANT_DESTRUCTOR(
773 _Trait::_Unavailable,
Louis Dionneff05add2020-10-05 16:30:23 -0400774 ~__dtor() = delete;,
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000775 void __destroy() noexcept = delete;);
776
777#undef _LIBCPP_VARIANT_DESTRUCTOR
778
779template <class _Traits>
Louis Dionneff05add2020-10-05 16:30:23 -0400780class _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
781 using __base_type = __dtor<_Traits>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000782
783public:
784 using __base_type::__base_type;
785 using __base_type::operator=;
786
787protected:
788 template <size_t _Ip, class _Tp, class... _Args>
789 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +0000790 static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
Eric Fiselier87994112020-11-17 17:34:23 -0500791 ::new ((void*)_VSTD::addressof(__a))
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000792 __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
Eric Fiselier87994112020-11-17 17:34:23 -0500793 return __a.__value;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000794 }
795
796 template <class _Rhs>
797 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionneff05add2020-10-05 16:30:23 -0400798 static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000799 __lhs.__destroy();
800 if (!__rhs.valueless_by_exception()) {
801 __visitation::__base::__visit_alt_at(
802 __rhs.index(),
803 [](auto& __lhs_alt, auto&& __rhs_alt) {
804 __construct_alt(
805 __lhs_alt,
806 _VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
807 },
808 __lhs, _VSTD::forward<_Rhs>(__rhs));
809 __lhs.__index = __rhs.index();
810 }
811 }
812};
813
814template <class _Traits, _Trait = _Traits::__move_constructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000815class _LIBCPP_TEMPLATE_VIS __move_constructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000816
817#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \
818 move_constructor) \
819 template <class... _Types> \
Louis Dionneff05add2020-10-05 16:30:23 -0400820 class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \
821 move_constructible_trait> \
822 : public __ctor<__traits<_Types...>> { \
823 using __base_type = __ctor<__traits<_Types...>>; \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000824 \
825 public: \
826 using __base_type::__base_type; \
827 using __base_type::operator=; \
828 \
829 __move_constructor(const __move_constructor&) = default; \
830 move_constructor \
831 ~__move_constructor() = default; \
832 __move_constructor& operator=(const __move_constructor&) = default; \
833 __move_constructor& operator=(__move_constructor&&) = default; \
834 }
835
836_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
837 _Trait::_TriviallyAvailable,
838 __move_constructor(__move_constructor&& __that) = default;);
839
840_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
841 _Trait::_Available,
842 __move_constructor(__move_constructor&& __that) noexcept(
843 __all<is_nothrow_move_constructible_v<_Types>...>::value)
844 : __move_constructor(__valueless_t{}) {
845 this->__generic_construct(*this, _VSTD::move(__that));
846 });
847
848_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
849 _Trait::_Unavailable,
850 __move_constructor(__move_constructor&&) = delete;);
851
852#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
853
854template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000855class _LIBCPP_TEMPLATE_VIS __copy_constructor;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000856
857#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \
858 copy_constructor) \
859 template <class... _Types> \
Eric Fiselier87994112020-11-17 17:34:23 -0500860 class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000861 copy_constructible_trait> \
862 : public __move_constructor<__traits<_Types...>> { \
863 using __base_type = __move_constructor<__traits<_Types...>>; \
864 \
865 public: \
866 using __base_type::__base_type; \
867 using __base_type::operator=; \
868 \
869 copy_constructor \
870 __copy_constructor(__copy_constructor&&) = default; \
871 ~__copy_constructor() = default; \
872 __copy_constructor& operator=(const __copy_constructor&) = default; \
873 __copy_constructor& operator=(__copy_constructor&&) = default; \
874 }
875
876_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
877 _Trait::_TriviallyAvailable,
878 __copy_constructor(const __copy_constructor& __that) = default;);
879
880_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
881 _Trait::_Available,
882 __copy_constructor(const __copy_constructor& __that)
883 : __copy_constructor(__valueless_t{}) {
884 this->__generic_construct(*this, __that);
885 });
886
887_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
888 _Trait::_Unavailable,
889 __copy_constructor(const __copy_constructor&) = delete;);
890
891#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
892
893template <class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000894class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000895 using __base_type = __copy_constructor<_Traits>;
896
897public:
898 using __base_type::__base_type;
899 using __base_type::operator=;
900
901 template <size_t _Ip, class... _Args>
902 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +0000903 auto& __emplace(_Args&&... __args) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000904 this->__destroy();
Eric Fiselier62928442017-04-15 19:32:02 +0000905 auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
Eric Fiselier87994112020-11-17 17:34:23 -0500906 _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000907 this->__index = _Ip;
Eric Fiselier62928442017-04-15 19:32:02 +0000908 return __res;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000909 }
910
911protected:
Michael Park62118352017-06-07 10:22:43 +0000912 template <size_t _Ip, class _Tp, class _Arg>
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000913 inline _LIBCPP_INLINE_VISIBILITY
Michael Park62118352017-06-07 10:22:43 +0000914 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000915 if (this->index() == _Ip) {
916 __a.__value = _VSTD::forward<_Arg>(__arg);
917 } else {
918 struct {
919 void operator()(true_type) const {
Michael Park62118352017-06-07 10:22:43 +0000920 __this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000921 }
922 void operator()(false_type) const {
Michael Park62118352017-06-07 10:22:43 +0000923 __this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000924 }
925 __assignment* __this;
926 _Arg&& __arg;
927 } __impl{this, _VSTD::forward<_Arg>(__arg)};
Michael Park62118352017-06-07 10:22:43 +0000928 __impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
929 !is_nothrow_move_constructible_v<_Tp>>{});
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000930 }
931 }
932
933 template <class _That>
934 inline _LIBCPP_INLINE_VISIBILITY
935 void __generic_assign(_That&& __that) {
936 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
937 // do nothing.
938 } else if (__that.valueless_by_exception()) {
939 this->__destroy();
940 } else {
941 __visitation::__base::__visit_alt_at(
942 __that.index(),
943 [this](auto& __this_alt, auto&& __that_alt) {
944 this->__assign_alt(
945 __this_alt,
Michael Park62118352017-06-07 10:22:43 +0000946 _VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000947 },
948 *this, _VSTD::forward<_That>(__that));
949 }
950 }
951};
952
953template <class _Traits, _Trait = _Traits::__move_assignable_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000954class _LIBCPP_TEMPLATE_VIS __move_assignment;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000955
956#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \
957 move_assignment) \
958 template <class... _Types> \
Eric Fiselier87994112020-11-17 17:34:23 -0500959 class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000960 move_assignable_trait> \
961 : public __assignment<__traits<_Types...>> { \
962 using __base_type = __assignment<__traits<_Types...>>; \
963 \
964 public: \
965 using __base_type::__base_type; \
966 using __base_type::operator=; \
967 \
968 __move_assignment(const __move_assignment&) = default; \
969 __move_assignment(__move_assignment&&) = default; \
970 ~__move_assignment() = default; \
971 __move_assignment& operator=(const __move_assignment&) = default; \
972 move_assignment \
973 }
974
975_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
976 _Trait::_TriviallyAvailable,
977 __move_assignment& operator=(__move_assignment&& __that) = default;);
978
979_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
980 _Trait::_Available,
981 __move_assignment& operator=(__move_assignment&& __that) noexcept(
982 __all<(is_nothrow_move_constructible_v<_Types> &&
983 is_nothrow_move_assignable_v<_Types>)...>::value) {
984 this->__generic_assign(_VSTD::move(__that));
985 return *this;
986 });
987
988_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
989 _Trait::_Unavailable,
990 __move_assignment& operator=(__move_assignment&&) = delete;);
991
992#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
993
994template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000995class _LIBCPP_TEMPLATE_VIS __copy_assignment;
Eric Fiselier94cdacc2016-12-02 23:00:05 +0000996
997#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \
998 copy_assignment) \
999 template <class... _Types> \
Eric Fiselier87994112020-11-17 17:34:23 -05001000 class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001001 copy_assignable_trait> \
1002 : public __move_assignment<__traits<_Types...>> { \
1003 using __base_type = __move_assignment<__traits<_Types...>>; \
1004 \
1005 public: \
1006 using __base_type::__base_type; \
1007 using __base_type::operator=; \
1008 \
1009 __copy_assignment(const __copy_assignment&) = default; \
1010 __copy_assignment(__copy_assignment&&) = default; \
1011 ~__copy_assignment() = default; \
1012 copy_assignment \
1013 __copy_assignment& operator=(__copy_assignment&&) = default; \
1014 }
1015
1016_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1017 _Trait::_TriviallyAvailable,
1018 __copy_assignment& operator=(const __copy_assignment& __that) = default;);
1019
1020_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1021 _Trait::_Available,
1022 __copy_assignment& operator=(const __copy_assignment& __that) {
1023 this->__generic_assign(__that);
1024 return *this;
1025 });
1026
1027_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1028 _Trait::_Unavailable,
1029 __copy_assignment& operator=(const __copy_assignment&) = delete;);
1030
1031#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1032
1033template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001034class _LIBCPP_TEMPLATE_VIS __impl
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001035 : public __copy_assignment<__traits<_Types...>> {
1036 using __base_type = __copy_assignment<__traits<_Types...>>;
1037
1038public:
1039 using __base_type::__base_type;
1040 using __base_type::operator=;
1041
1042 template <size_t _Ip, class _Arg>
1043 inline _LIBCPP_INLINE_VISIBILITY
1044 void __assign(_Arg&& __arg) {
1045 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
Michael Park62118352017-06-07 10:22:43 +00001046 _VSTD::forward<_Arg>(__arg));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001047 }
1048
1049 inline _LIBCPP_INLINE_VISIBILITY
1050 void __swap(__impl& __that) {
1051 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1052 // do nothing.
1053 } else if (this->index() == __that.index()) {
1054 __visitation::__base::__visit_alt_at(
1055 this->index(),
1056 [](auto& __this_alt, auto& __that_alt) {
1057 using _VSTD::swap;
1058 swap(__this_alt.__value, __that_alt.__value);
1059 },
1060 *this,
1061 __that);
1062 } else {
1063 __impl* __lhs = this;
1064 __impl* __rhs = _VSTD::addressof(__that);
1065 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1066 _VSTD::swap(__lhs, __rhs);
1067 }
1068 __impl __tmp(_VSTD::move(*__rhs));
Michael Park5dee7342020-06-16 15:15:10 -07001069#ifndef _LIBCPP_NO_EXCEPTIONS
Michael Park6a9ef642020-07-08 10:46:02 -07001070 if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
Michael Park31967aa2020-06-16 13:29:23 -07001071 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1072 } else {
1073 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1074 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1075 // into `__rhs` and provide the strong exception safety guarantee.
1076 try {
1077 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1078 } catch (...) {
1079 if (__tmp.__move_nothrow()) {
1080 this->__generic_construct(*__rhs, _VSTD::move(__tmp));
1081 }
1082 throw;
1083 }
1084 }
Michael Park5dee7342020-06-16 15:15:10 -07001085#else
1086 // this isn't consolidated with the `if constexpr` branch above due to
1087 // `throw` being ill-formed with exceptions disabled even when discarded.
1088 this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
1089#endif
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001090 this->__generic_construct(*__lhs, _VSTD::move(__tmp));
1091 }
1092 }
1093
1094private:
1095 inline _LIBCPP_INLINE_VISIBILITY
1096 bool __move_nothrow() const {
1097 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1098 return this->valueless_by_exception() || __results[this->index()];
1099 }
1100};
1101
Eric Fiselier3aaf8a32019-07-14 18:21:15 +00001102struct __no_narrowing_check {
1103 template <class _Dest, class _Source>
1104 using _Apply = __identity<_Dest>;
1105};
1106
1107struct __narrowing_check {
1108 template <class _Dest>
1109 static auto __test_impl(_Dest (&&)[1]) -> __identity<_Dest>;
1110 template <class _Dest, class _Source>
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001111 using _Apply _LIBCPP_NODEBUG_TYPE = decltype(__test_impl<_Dest>({std::declval<_Source>()}));
Eric Fiselier3aaf8a32019-07-14 18:21:15 +00001112};
1113
1114template <class _Dest, class _Source>
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001115using __check_for_narrowing _LIBCPP_NODEBUG_TYPE =
1116 typename _If<
Eric Fiselier3aaf8a32019-07-14 18:21:15 +00001117#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
1118 false &&
1119#endif
1120 is_arithmetic<_Dest>::value,
1121 __narrowing_check,
1122 __no_narrowing_check
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001123 >::template _Apply<_Dest, _Source>;
Eric Fiselier3aaf8a32019-07-14 18:21:15 +00001124
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001125template <class _Tp, size_t _Idx>
1126struct __overload {
Zhihao Yuan821efb82019-06-20 22:09:40 +00001127 template <class _Up>
Eric Fiselier80f5dfb2019-07-14 18:31:55 +00001128 auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001129};
1130
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001131template <class _Tp, size_t>
1132struct __overload_bool {
Zhihao Yuan821efb82019-06-20 22:09:40 +00001133 template <class _Up, class _Ap = __uncvref_t<_Up>>
1134 auto operator()(bool, _Up&&) const
1135 -> enable_if_t<is_same_v<_Ap, bool>, __identity<_Tp>>;
1136};
1137
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001138template <size_t _Idx>
1139struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
1140template <size_t _Idx>
1141struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
1142template <size_t _Idx>
1143struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
1144template <size_t _Idx>
1145struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
1146
1147template <class ..._Bases>
1148struct __all_overloads : _Bases... {
1149 void operator()() const;
1150 using _Bases::operator()...;
1151};
1152
1153template <class IdxSeq>
1154struct __make_overloads_imp;
1155
1156template <size_t ..._Idx>
1157struct __make_overloads_imp<__tuple_indices<_Idx...> > {
1158 template <class ..._Types>
1159 using _Apply _LIBCPP_NODEBUG_TYPE = __all_overloads<__overload<_Types, _Idx>...>;
1160};
1161
1162template <class ..._Types>
1163using _MakeOverloads _LIBCPP_NODEBUG_TYPE = typename __make_overloads_imp<
1164 __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
Zhihao Yuan821efb82019-06-20 22:09:40 +00001165
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001166template <class _Tp, class... _Types>
Zhihao Yuan821efb82019-06-20 22:09:40 +00001167using __best_match_t =
Eric Fiselieraa7cdca2019-07-14 21:29:39 +00001168 typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001169
1170} // __variant_detail
1171
1172template <class... _Types>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001173class _LIBCPP_TEMPLATE_VIS variant
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001174 : private __sfinae_ctor_base<
1175 __all<is_copy_constructible_v<_Types>...>::value,
1176 __all<is_move_constructible_v<_Types>...>::value>,
1177 private __sfinae_assign_base<
1178 __all<(is_copy_constructible_v<_Types> &&
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001179 is_copy_assignable_v<_Types>)...>::value,
1180 __all<(is_move_constructible_v<_Types> &&
1181 is_move_assignable_v<_Types>)...>::value> {
1182 static_assert(0 < sizeof...(_Types),
1183 "variant must consist of at least one alternative.");
1184
1185 static_assert(__all<!is_array_v<_Types>...>::value,
1186 "variant can not have an array type as an alternative.");
1187
1188 static_assert(__all<!is_reference_v<_Types>...>::value,
1189 "variant can not have a reference type as an alternative.");
1190
1191 static_assert(__all<!is_void_v<_Types>...>::value,
1192 "variant can not have a void type as an alternative.");
1193
1194 using __first_type = variant_alternative_t<0, variant>;
1195
1196public:
1197 template <bool _Dummy = true,
1198 enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1199 _Dummy>::value,
1200 int> = 0>
1201 inline _LIBCPP_INLINE_VISIBILITY
1202 constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1203 : __impl(in_place_index<0>) {}
1204
1205 variant(const variant&) = default;
1206 variant(variant&&) = default;
1207
1208 template <
1209 class _Arg,
Marshall Clowd5bbc242018-02-06 20:56:55 +00001210 enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
1211 enable_if_t<!__is_inplace_type<__uncvref_t<_Arg>>::value, int> = 0,
1212 enable_if_t<!__is_inplace_index<__uncvref_t<_Arg>>::value, int> = 0,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001213 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1214 size_t _Ip =
1215 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1216 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1217 inline _LIBCPP_INLINE_VISIBILITY
1218 constexpr variant(_Arg&& __arg) noexcept(
1219 is_nothrow_constructible_v<_Tp, _Arg>)
1220 : __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
1221
1222 template <size_t _Ip, class... _Args,
Eric Fiselier5d2df752018-03-23 23:42:30 +00001223 class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
1224 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1225 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001226 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5d2df752018-03-23 23:42:30 +00001227 explicit constexpr variant(
1228 in_place_index_t<_Ip>,
1229 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001230 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1231
Eric Fiselier5d2df752018-03-23 23:42:30 +00001232 template <
1233 size_t _Ip,
1234 class _Up,
1235 class... _Args,
1236 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1237 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001238 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1239 int> = 0>
1240 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5d2df752018-03-23 23:42:30 +00001241 explicit constexpr variant(
1242 in_place_index_t<_Ip>,
1243 initializer_list<_Up> __il,
1244 _Args&&... __args) noexcept(
1245 is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001246 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1247
1248 template <
1249 class _Tp,
1250 class... _Args,
1251 size_t _Ip =
1252 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1253 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1254 inline _LIBCPP_INLINE_VISIBILITY
1255 explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1256 is_nothrow_constructible_v<_Tp, _Args...>)
1257 : __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
1258
1259 template <
1260 class _Tp,
1261 class _Up,
1262 class... _Args,
1263 size_t _Ip =
1264 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1265 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1266 int> = 0>
1267 inline _LIBCPP_INLINE_VISIBILITY
1268 explicit constexpr variant(
1269 in_place_type_t<_Tp>,
1270 initializer_list<_Up> __il,
1271 _Args&&... __args) noexcept(
1272 is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1273 : __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
1274
1275 ~variant() = default;
1276
1277 variant& operator=(const variant&) = default;
1278 variant& operator=(variant&&) = default;
1279
1280 template <
1281 class _Arg,
Marshall Clowd5bbc242018-02-06 20:56:55 +00001282 enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001283 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1284 size_t _Ip =
1285 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1286 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
1287 int> = 0>
1288 inline _LIBCPP_INLINE_VISIBILITY
1289 variant& operator=(_Arg&& __arg) noexcept(
1290 is_nothrow_assignable_v<_Tp&, _Arg> &&
1291 is_nothrow_constructible_v<_Tp, _Arg>) {
1292 __impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
1293 return *this;
1294 }
1295
1296 template <
1297 size_t _Ip,
1298 class... _Args,
1299 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1300 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1301 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1302 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001303 _Tp& emplace(_Args&&... __args) {
1304 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001305 }
1306
1307 template <
1308 size_t _Ip,
1309 class _Up,
1310 class... _Args,
1311 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1312 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1313 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1314 int> = 0>
1315 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001316 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1317 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001318 }
1319
1320 template <
1321 class _Tp,
1322 class... _Args,
1323 size_t _Ip =
1324 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1325 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1326 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001327 _Tp& emplace(_Args&&... __args) {
1328 return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001329 }
1330
1331 template <
1332 class _Tp,
1333 class _Up,
1334 class... _Args,
1335 size_t _Ip =
1336 __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1337 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1338 int> = 0>
1339 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier62928442017-04-15 19:32:02 +00001340 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1341 return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001342 }
1343
1344 inline _LIBCPP_INLINE_VISIBILITY
1345 constexpr bool valueless_by_exception() const noexcept {
1346 return __impl.valueless_by_exception();
1347 }
1348
1349 inline _LIBCPP_INLINE_VISIBILITY
1350 constexpr size_t index() const noexcept { return __impl.index(); }
1351
1352 template <
1353 bool _Dummy = true,
1354 enable_if_t<
1355 __all<(
1356 __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1357 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1358 int> = 0>
1359 inline _LIBCPP_INLINE_VISIBILITY
1360 void swap(variant& __that) noexcept(
1361 __all<(is_nothrow_move_constructible_v<_Types> &&
1362 is_nothrow_swappable_v<_Types>)...>::value) {
1363 __impl.__swap(__that.__impl);
1364 }
1365
1366private:
1367 __variant_detail::__impl<_Types...> __impl;
1368
1369 friend struct __variant_detail::__access::__variant;
1370 friend struct __variant_detail::__visitation::__variant;
1371};
1372
1373template <size_t _Ip, class... _Types>
1374inline _LIBCPP_INLINE_VISIBILITY
1375constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1376 return __v.index() == _Ip;
1377}
1378
1379template <class _Tp, class... _Types>
1380inline _LIBCPP_INLINE_VISIBILITY
1381constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1382 return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1383}
1384
1385template <size_t _Ip, class _Vp>
1386inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001387_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Richard Smith66d1c552018-08-27 21:41:50 +00001388constexpr auto&& __generic_get(_Vp&& __v) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001389 using __variant_detail::__access::__variant;
1390 if (!__holds_alternative<_Ip>(__v)) {
Eric Fiselier10642ea2016-12-03 01:58:07 +00001391 __throw_bad_variant_access();
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001392 }
1393 return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
1394}
1395
1396template <size_t _Ip, class... _Types>
1397inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001398_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001399constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
1400 variant<_Types...>& __v) {
1401 static_assert(_Ip < sizeof...(_Types));
1402 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1403 return __generic_get<_Ip>(__v);
1404}
1405
1406template <size_t _Ip, class... _Types>
1407inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001408_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001409constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
1410 variant<_Types...>&& __v) {
1411 static_assert(_Ip < sizeof...(_Types));
1412 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1413 return __generic_get<_Ip>(_VSTD::move(__v));
1414}
1415
1416template <size_t _Ip, class... _Types>
1417inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001418_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001419constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
1420 const variant<_Types...>& __v) {
1421 static_assert(_Ip < sizeof...(_Types));
1422 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1423 return __generic_get<_Ip>(__v);
1424}
1425
1426template <size_t _Ip, class... _Types>
1427inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001428_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001429constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
1430 const variant<_Types...>&& __v) {
1431 static_assert(_Ip < sizeof...(_Types));
1432 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1433 return __generic_get<_Ip>(_VSTD::move(__v));
1434}
1435
1436template <class _Tp, class... _Types>
1437inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001438_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001439constexpr _Tp& get(variant<_Types...>& __v) {
1440 static_assert(!is_void_v<_Tp>);
1441 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1442}
1443
1444template <class _Tp, class... _Types>
1445inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001446_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001447constexpr _Tp&& get(variant<_Types...>&& __v) {
1448 static_assert(!is_void_v<_Tp>);
1449 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1450 _VSTD::move(__v));
1451}
1452
1453template <class _Tp, class... _Types>
1454inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001455_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001456constexpr const _Tp& get(const variant<_Types...>& __v) {
1457 static_assert(!is_void_v<_Tp>);
1458 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1459}
1460
1461template <class _Tp, class... _Types>
1462inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001463_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001464constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1465 static_assert(!is_void_v<_Tp>);
1466 return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
1467 _VSTD::move(__v));
1468}
1469
1470template <size_t _Ip, class _Vp>
1471inline _LIBCPP_INLINE_VISIBILITY
1472constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1473 using __variant_detail::__access::__variant;
1474 return __v && __holds_alternative<_Ip>(*__v)
1475 ? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
1476 : nullptr;
1477}
1478
1479template <size_t _Ip, class... _Types>
1480inline _LIBCPP_INLINE_VISIBILITY
1481constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1482get_if(variant<_Types...>* __v) noexcept {
1483 static_assert(_Ip < sizeof...(_Types));
1484 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1485 return __generic_get_if<_Ip>(__v);
1486}
1487
1488template <size_t _Ip, class... _Types>
1489inline _LIBCPP_INLINE_VISIBILITY
1490constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1491get_if(const variant<_Types...>* __v) noexcept {
1492 static_assert(_Ip < sizeof...(_Types));
1493 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1494 return __generic_get_if<_Ip>(__v);
1495}
1496
1497template <class _Tp, class... _Types>
1498inline _LIBCPP_INLINE_VISIBILITY
1499constexpr add_pointer_t<_Tp>
1500get_if(variant<_Types...>* __v) noexcept {
1501 static_assert(!is_void_v<_Tp>);
1502 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1503}
1504
1505template <class _Tp, class... _Types>
1506inline _LIBCPP_INLINE_VISIBILITY
1507constexpr add_pointer_t<const _Tp>
1508get_if(const variant<_Types...>* __v) noexcept {
1509 static_assert(!is_void_v<_Tp>);
1510 return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1511}
1512
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001513template <class _Operator>
1514struct __convert_to_bool {
1515 template <class _T1, class _T2>
1516 _LIBCPP_INLINE_VISIBILITY constexpr bool operator()(_T1 && __t1, _T2&& __t2) const {
1517 static_assert(std::is_convertible<decltype(_Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2))), bool>::value,
1518 "the relational operator does not return a type which is implicitly convertible to bool");
1519 return _Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
1520 }
1521};
1522
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001523template <class... _Types>
1524inline _LIBCPP_INLINE_VISIBILITY
1525constexpr bool operator==(const variant<_Types...>& __lhs,
1526 const variant<_Types...>& __rhs) {
1527 using __variant_detail::__visitation::__variant;
1528 if (__lhs.index() != __rhs.index()) return false;
1529 if (__lhs.valueless_by_exception()) return true;
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001530 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001531}
1532
1533template <class... _Types>
1534inline _LIBCPP_INLINE_VISIBILITY
1535constexpr bool operator!=(const variant<_Types...>& __lhs,
1536 const variant<_Types...>& __rhs) {
1537 using __variant_detail::__visitation::__variant;
1538 if (__lhs.index() != __rhs.index()) return true;
1539 if (__lhs.valueless_by_exception()) return false;
1540 return __variant::__visit_value_at(
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001541 __lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001542}
1543
1544template <class... _Types>
1545inline _LIBCPP_INLINE_VISIBILITY
1546constexpr bool operator<(const variant<_Types...>& __lhs,
1547 const variant<_Types...>& __rhs) {
1548 using __variant_detail::__visitation::__variant;
1549 if (__rhs.valueless_by_exception()) return false;
1550 if (__lhs.valueless_by_exception()) return true;
1551 if (__lhs.index() < __rhs.index()) return true;
1552 if (__lhs.index() > __rhs.index()) return false;
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001553 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001554}
1555
1556template <class... _Types>
1557inline _LIBCPP_INLINE_VISIBILITY
1558constexpr bool operator>(const variant<_Types...>& __lhs,
1559 const variant<_Types...>& __rhs) {
1560 using __variant_detail::__visitation::__variant;
1561 if (__lhs.valueless_by_exception()) return false;
1562 if (__rhs.valueless_by_exception()) return true;
1563 if (__lhs.index() > __rhs.index()) return true;
1564 if (__lhs.index() < __rhs.index()) return false;
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001565 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001566}
1567
1568template <class... _Types>
1569inline _LIBCPP_INLINE_VISIBILITY
1570constexpr bool operator<=(const variant<_Types...>& __lhs,
1571 const variant<_Types...>& __rhs) {
1572 using __variant_detail::__visitation::__variant;
1573 if (__lhs.valueless_by_exception()) return true;
1574 if (__rhs.valueless_by_exception()) return false;
1575 if (__lhs.index() < __rhs.index()) return true;
1576 if (__lhs.index() > __rhs.index()) return false;
1577 return __variant::__visit_value_at(
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001578 __lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001579}
1580
1581template <class... _Types>
1582inline _LIBCPP_INLINE_VISIBILITY
1583constexpr bool operator>=(const variant<_Types...>& __lhs,
1584 const variant<_Types...>& __rhs) {
1585 using __variant_detail::__visitation::__variant;
1586 if (__rhs.valueless_by_exception()) return true;
1587 if (__lhs.valueless_by_exception()) return false;
1588 if (__lhs.index() > __rhs.index()) return true;
1589 if (__lhs.index() < __rhs.index()) return false;
1590 return __variant::__visit_value_at(
Eric Fiselierfb6a2bc2018-09-19 17:53:21 +00001591 __lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001592}
1593
Eric Fiselier87994112020-11-17 17:34:23 -05001594template <class _Visitor, class... _Vs>
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001595inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +00001596_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
Eric Fiselier87994112020-11-17 17:34:23 -05001597constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001598 using __variant_detail::__visitation::__variant;
Eric Fiselier87994112020-11-17 17:34:23 -05001599 bool __results[] = {__vs.valueless_by_exception()...};
1600 for (bool __result : __results) {
1601 if (__result) {
1602 __throw_bad_variant_access();
1603 }
1604 }
1605 return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001606 _VSTD::forward<_Vs>(__vs)...);
1607}
1608
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001609struct _LIBCPP_TEMPLATE_VIS monostate {};
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001610
1611inline _LIBCPP_INLINE_VISIBILITY
1612constexpr bool operator<(monostate, monostate) noexcept { return false; }
1613
1614inline _LIBCPP_INLINE_VISIBILITY
1615constexpr bool operator>(monostate, monostate) noexcept { return false; }
1616
1617inline _LIBCPP_INLINE_VISIBILITY
1618constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1619
1620inline _LIBCPP_INLINE_VISIBILITY
1621constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1622
1623inline _LIBCPP_INLINE_VISIBILITY
1624constexpr bool operator==(monostate, monostate) noexcept { return true; }
1625
1626inline _LIBCPP_INLINE_VISIBILITY
1627constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1628
1629template <class... _Types>
1630inline _LIBCPP_INLINE_VISIBILITY
1631auto swap(variant<_Types...>& __lhs,
1632 variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1633 -> decltype(__lhs.swap(__rhs)) {
1634 __lhs.swap(__rhs);
1635}
1636
1637template <class... _Types>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001638struct _LIBCPP_TEMPLATE_VIS hash<
1639 __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001640 using argument_type = variant<_Types...>;
1641 using result_type = size_t;
1642
1643 inline _LIBCPP_INLINE_VISIBILITY
1644 result_type operator()(const argument_type& __v) const {
1645 using __variant_detail::__visitation::__variant;
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001646 size_t __res =
1647 __v.valueless_by_exception()
Eric Fiselier6b1683c2016-12-04 21:37:37 +00001648 ? 299792458 // Random value chosen by the universe upon creation
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001649 : __variant::__visit_alt(
1650 [](const auto& __alt) {
Marshall Clowe61ba952018-02-12 15:41:25 +00001651 using __alt_type = __uncvref_t<decltype(__alt)>;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001652 using __value_type = remove_const_t<
1653 typename __alt_type::__value_type>;
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001654 return hash<__value_type>{}(__alt.__value);
1655 },
1656 __v);
Eric Fiselier9a4e3502016-12-02 23:38:31 +00001657 return __hash_combine(__res, hash<size_t>{}(__v.index()));
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001658 }
1659};
1660
1661template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001662struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001663 using argument_type = monostate;
1664 using result_type = size_t;
1665
1666 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001667 result_type operator()(const argument_type&) const _NOEXCEPT {
Eric Fiselier6b1683c2016-12-04 21:37:37 +00001668 return 66740831; // return a fundamentally attractive random value.
1669 }
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001670};
1671
1672#endif // _LIBCPP_STD_VER > 14
1673
1674_LIBCPP_END_NAMESPACE_STD
1675
Eric Fiselier8625d332017-11-19 04:57:22 +00001676_LIBCPP_POP_MACROS
1677
Eric Fiselier94cdacc2016-12-02 23:00:05 +00001678#endif // _LIBCPP_VARIANT