1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// 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
7//
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);
25 constexpr variant(const variant&) noexcept(see below);
26 constexpr variant(variant&&) noexcept(see below);
27
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 constexpr ~variant(); // constexpr since c++20
46
47 // 20.7.2.3, assignment
48 constexpr variant& operator=(const variant&);
49 constexpr variant& operator=(variant&&) noexcept(see below);
50
51 template <class T>
52 constexpr variant& operator=(T&&) noexcept(see below); // constexpr since c++20
53
54 // 20.7.2.4, modifiers
55 template <class T, class... Args>
56 constexpr T& emplace(Args&&...); // constexpr since c++20
57
58 template <class T, class U, class... Args>
59 constexpr T& emplace(initializer_list<U>, Args&&...); // constexpr since c++20
60
61 template <size_t I, class... Args>
62 constexpr variant_alternative_t<I, variant>& emplace(Args&&...); // constexpr since c++20
63
64 template <size_t I, class U, class... Args>
65 constexpr variant_alternative_t<I, variant>&
66 emplace(initializer_list<U>, Args&&...); // constexpr since c++20
67
68 // 20.7.2.5, value status
69 constexpr bool valueless_by_exception() const noexcept;
70 constexpr size_t index() const noexcept;
71
72 // 20.7.2.6, swap
73 void swap(variant&) noexcept(see below);
74
75 // [variant.visit], visitation
76 template<class Self, class Visitor>
77 constexpr decltype(auto) visit(this Self&&, Visitor&&); // Since C++26
78 template<class R, class Self, class Visitor>
79 constexpr R visit(this Self&&, Visitor&&); // Since C++26
80 };
81
82 // 20.7.3, variant helper classes
83 template <class T> struct variant_size; // undefined
84
85 template <class T>
86 inline constexpr size_t variant_size_v = variant_size<T>::value;
87
88 template <class T> struct variant_size<const T>;
89 template <class T> struct variant_size<volatile T>;
90 template <class T> struct variant_size<const volatile T>;
91
92 template <class... Types>
93 struct variant_size<variant<Types...>>;
94
95 template <size_t I, class T> struct variant_alternative; // undefined
96
97 template <size_t I, class T>
98 using variant_alternative_t = typename variant_alternative<I, T>::type;
99
100 template <size_t I, class T> struct variant_alternative<I, const T>;
101 template <size_t I, class T> struct variant_alternative<I, volatile T>;
102 template <size_t I, class T> struct variant_alternative<I, const volatile T>;
103
104 template <size_t I, class... Types>
105 struct variant_alternative<I, variant<Types...>>;
106
107 inline constexpr size_t variant_npos = -1;
108
109 // 20.7.4, value access
110 template <class T, class... Types>
111 constexpr bool holds_alternative(const variant<Types...>&) noexcept;
112
113 template <size_t I, class... Types>
114 constexpr variant_alternative_t<I, variant<Types...>>&
115 get(variant<Types...>&);
116
117 template <size_t I, class... Types>
118 constexpr variant_alternative_t<I, variant<Types...>>&&
119 get(variant<Types...>&&);
120
121 template <size_t I, class... Types>
122 constexpr variant_alternative_t<I, variant<Types...>> const&
123 get(const variant<Types...>&);
124
125 template <size_t I, class... Types>
126 constexpr variant_alternative_t<I, variant<Types...>> const&&
127 get(const variant<Types...>&&);
128
129 template <class T, class... Types>
130 constexpr T& get(variant<Types...>&);
131
132 template <class T, class... Types>
133 constexpr T&& get(variant<Types...>&&);
134
135 template <class T, class... Types>
136 constexpr const T& get(const variant<Types...>&);
137
138 template <class T, class... Types>
139 constexpr const T&& get(const variant<Types...>&&);
140
141 template <size_t I, class... Types>
142 constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
143 get_if(variant<Types...>*) noexcept;
144
145 template <size_t I, class... Types>
146 constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
147 get_if(const variant<Types...>*) noexcept;
148
149 template <class T, class... Types>
150 constexpr add_pointer_t<T>
151 get_if(variant<Types...>*) noexcept;
152
153 template <class T, class... Types>
154 constexpr add_pointer_t<const T>
155 get_if(const variant<Types...>*) noexcept;
156
157 // 20.7.5, relational operators
158 template <class... Types>
159 constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
160
161 template <class... Types>
162 constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
163
164 template <class... Types>
165 constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
166
167 template <class... Types>
168 constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
169
170 template <class... Types>
171 constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
172
173 template <class... Types>
174 constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
175
176 template <class... Types> requires (three_way_comparable<Types> && ...)
177 constexpr common_comparison_category_t<compare_three_way_result_t<Types>...>
178 operator<=>(const variant<Types...>&, const variant<Types...>&); // since C++20
179
180 // 20.7.6, visitation
181 template <class Visitor, class... Variants>
182 constexpr see below visit(Visitor&&, Variants&&...);
183
184 template <class R, class Visitor, class... Variants>
185 constexpr R visit(Visitor&&, Variants&&...); // since C++20
186
187 // 20.7.7, class monostate
188 struct monostate;
189
190 // 20.7.8, monostate relational operators
191 constexpr bool operator==(monostate, monostate) noexcept;
192 constexpr bool operator!=(monostate, monostate) noexcept; // until C++20
193 constexpr bool operator<(monostate, monostate) noexcept; // until C++20
194 constexpr bool operator>(monostate, monostate) noexcept; // until C++20
195 constexpr bool operator<=(monostate, monostate) noexcept; // until C++20
196 constexpr bool operator>=(monostate, monostate) noexcept; // until C++20
197 constexpr strong_ordering operator<=>(monostate, monostate) noexcept; // since C++20
198
199 // 20.7.9, specialized algorithms
200 template <class... Types>
201 void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
202
203 // 20.7.10, class bad_variant_access
204 class bad_variant_access;
205
206 // 20.7.11, hash support
207 template <class T> struct hash;
208 template <class... Types> struct hash<variant<Types...>>;
209 template <> struct hash<monostate>;
210
211} // namespace std
212
213*/
214
215#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
216# include <__cxx03/__config>
217#else
218# include <__compare/common_comparison_category.h>
219# include <__compare/compare_three_way_result.h>
220# include <__compare/ordering.h>
221# include <__compare/three_way_comparable.h>
222# include <__config>
223# include <__exception/exception.h>
224# include <__functional/hash.h>
225# include <__functional/operations.h>
226# include <__functional/unary_function.h>
227# include <__fwd/variant.h>
228# include <__memory/addressof.h>
229# include <__memory/construct_at.h>
230# include <__tuple/find_index.h>
231# include <__tuple/sfinae_helpers.h>
232# include <__type_traits/add_cv_quals.h>
233# include <__type_traits/add_pointer.h>
234# include <__type_traits/common_type.h>
235# include <__type_traits/conditional.h>
236# include <__type_traits/conjunction.h>
237# include <__type_traits/decay.h>
238# include <__type_traits/enable_if.h>
239# include <__type_traits/invoke.h>
240# include <__type_traits/is_array.h>
241# include <__type_traits/is_assignable.h>
242# include <__type_traits/is_constructible.h>
243# include <__type_traits/is_convertible.h>
244# include <__type_traits/is_core_convertible.h>
245# include <__type_traits/is_destructible.h>
246# include <__type_traits/is_nothrow_assignable.h>
247# include <__type_traits/is_nothrow_constructible.h>
248# include <__type_traits/is_reference.h>
249# include <__type_traits/is_relocatable.h>
250# include <__type_traits/is_same.h>
251# include <__type_traits/is_swappable.h>
252# include <__type_traits/is_trivially_assignable.h>
253# include <__type_traits/is_trivially_constructible.h>
254# include <__type_traits/is_trivially_destructible.h>
255# include <__type_traits/is_void.h>
256# include <__type_traits/remove_const.h>
257# include <__type_traits/remove_cvref.h>
258# include <__type_traits/remove_reference.h>
259# include <__type_traits/type_identity.h>
260# include <__type_traits/void_t.h>
261# include <__utility/declval.h>
262# include <__utility/forward.h>
263# include <__utility/forward_like.h>
264# include <__utility/in_place.h>
265# include <__utility/integer_sequence.h>
266# include <__utility/move.h>
267# include <__utility/swap.h>
268# include <__variant/monostate.h>
269# include <__verbose_abort>
270# include <initializer_list>
271# include <limits>
272# include <version>
273
274// standard-mandated includes
275
276// [variant.syn]
277# include <compare>
278
279# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
280# pragma GCC system_header
281# endif
282
283_LIBCPP_PUSH_MACROS
284# include <__undef_macros>
285
286_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
287_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
288
289class _LIBCPP_EXPORTED_FROM_ABI bad_variant_access : public exception {
290public:
291 [[__nodiscard__]] const char* what() const _NOEXCEPT override;
292};
293
294class _LIBCPP_EXPORTED_FROM_ABI __bad_variant_access_with_msg : public bad_variant_access {
295public:
296 _LIBCPP_HIDE_FROM_ABI explicit __bad_variant_access_with_msg(const char* __msg) _NOEXCEPT : __msg_(__msg) {}
297# if _LIBCPP_AVAILABILITY_HAS_BAD_VARIANT_ACCESS_WITH_MSG_KEY_FUNCTION
298 [[__nodiscard__]] const char* what() const _NOEXCEPT override;
299# else
300 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI_VIRTUAL const char* what() const _NOEXCEPT override { return __msg_; }
301# endif
302
303private:
304 const char* __msg_;
305};
306
307_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
308_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
309
310# if _LIBCPP_STD_VER >= 17
311
312_LIBCPP_BEGIN_NAMESPACE_STD
313
314// Light N-dimensional array of function pointers. Used in place of std::array to avoid
315// adding a dependency.
316template <class _Tp, size_t _Size>
317struct __farray {
318 static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit");
319 _Tp __buf_[_Size] = {};
320
321 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator[](size_t __n) const noexcept { return __buf_[__n]; }
322};
323
324[[noreturn]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_variant_access(const char* __msg) {
325# if _LIBCPP_HAS_EXCEPTIONS
326 throw __bad_variant_access_with_msg(__msg);
327# else
328 _LIBCPP_VERBOSE_ABORT("bad_variant_access was thrown in -fno-exceptions mode: %s", __msg);
329# endif
330}
331
332// variant_size
333template <class _Tp>
334struct variant_size<const _Tp> : variant_size<_Tp> {};
335
336template <class _Tp>
337struct variant_size<volatile _Tp> : variant_size<_Tp> {};
338
339template <class _Tp>
340struct variant_size<const volatile _Tp> : variant_size<_Tp> {};
341
342template <class... _Types>
343struct variant_size<variant<_Types...>> : integral_constant<size_t, sizeof...(_Types)> {};
344
345// variant_alternative
346template <size_t _Ip, class _Tp>
347struct variant_alternative<_Ip, const _Tp> : add_const<variant_alternative_t<_Ip, _Tp>> {};
348
349template <size_t _Ip, class _Tp>
350struct variant_alternative<_Ip, volatile _Tp> : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
351
352template <size_t _Ip, class _Tp>
353struct variant_alternative<_Ip, const volatile _Tp> : add_cv<variant_alternative_t<_Ip, _Tp>> {};
354
355template <size_t _Ip, class... _Types>
356struct variant_alternative<_Ip, variant<_Types...>> {
357 static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
358 using type _LIBCPP_NODEBUG = __type_pack_element<_Ip, _Types...>;
359};
360
361template <size_t _NumAlternatives>
362_LIBCPP_HIDE_FROM_ABI constexpr auto __choose_index_type() {
363# ifdef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
364 if constexpr (_NumAlternatives < numeric_limits<unsigned char>::max())
365 return static_cast<unsigned char>(0);
366 else if constexpr (_NumAlternatives < numeric_limits<unsigned short>::max())
367 return static_cast<unsigned short>(0);
368 else
369# endif // _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
370 return static_cast<unsigned int>(0);
371}
372
373template <size_t _NumAlts>
374using __variant_index_t _LIBCPP_NODEBUG = decltype(std::__choose_index_type<_NumAlts>());
375
376template <class _IndexType>
377constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
378
379template <class... _Types>
380_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>& __as_variant(variant<_Types...>& __vs) noexcept {
381 return __vs;
382}
383
384template <class... _Types>
385_LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>& __as_variant(const variant<_Types...>& __vs) noexcept {
386 return __vs;
387}
388
389template <class... _Types>
390_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>&& __as_variant(variant<_Types...>&& __vs) noexcept {
391 return std::move(__vs);
392}
393
394template <class... _Types>
395_LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>&& __as_variant(const variant<_Types...>&& __vs) noexcept {
396 return std::move(__vs);
397}
398
399namespace __find_detail {
400
401template <class _Tp, class... _Types>
402_LIBCPP_HIDE_FROM_ABI constexpr size_t __find_index() {
403 constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
404 size_t __result = __not_found;
405 for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
406 if (__matches[__i]) {
407 if (__result != __not_found) {
408 return __ambiguous;
409 }
410 __result = __i;
411 }
412 }
413 return __result;
414}
415
416template <size_t _Index>
417struct __find_unambiguous_index_sfinae_impl : integral_constant<size_t, _Index> {};
418
419template <>
420struct __find_unambiguous_index_sfinae_impl<__not_found> {};
421
422template <>
423struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
424
425template <class _Tp, class... _Types>
426struct __find_unambiguous_index_sfinae
427 : __find_unambiguous_index_sfinae_impl<__find_detail::__find_index<_Tp, _Types...>()> {};
428
429} // namespace __find_detail
430
431namespace __variant_detail {
432
433struct __valueless_t {};
434
435enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
436
437template <typename _Tp, template <typename> class _IsTriviallyAvailable, template <typename> class _IsAvailable>
438constexpr _Trait __trait =
439 _IsTriviallyAvailable<_Tp>::value ? _Trait::_TriviallyAvailable
440 : _IsAvailable<_Tp>::value
441 ? _Trait::_Available
442 : _Trait::_Unavailable;
443
444_LIBCPP_HIDE_FROM_ABI constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
445 _Trait __result = _Trait::_TriviallyAvailable;
446 for (_Trait __t : __traits) {
447 if (static_cast<int>(__t) > static_cast<int>(__result)) {
448 __result = __t;
449 }
450 }
451 return __result;
452}
453
454template <typename... _Types>
455struct __traits {
456 static constexpr _Trait __copy_constructible_trait =
457 __variant_detail::__common_trait(traits: {__trait<_Types, is_trivially_copy_constructible, is_copy_constructible>...});
458
459 static constexpr _Trait __move_constructible_trait =
460 __variant_detail::__common_trait(traits: {__trait<_Types, is_trivially_move_constructible, is_move_constructible>...});
461
462 static constexpr _Trait __copy_assignable_trait = __variant_detail::__common_trait(
463 traits: {__copy_constructible_trait, __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
464
465 static constexpr _Trait __move_assignable_trait = __variant_detail::__common_trait(
466 traits: {__move_constructible_trait, __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
467
468 static constexpr _Trait __destructible_trait =
469 __variant_detail::__common_trait(traits: {__trait<_Types, is_trivially_destructible, is_destructible>...});
470};
471
472namespace __access {
473
474struct __union {
475 template <class _Vp>
476 _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
477 return std::forward<_Vp>(__v).__head;
478 }
479
480 template <class _Vp, size_t _Ip>
481 _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
482 return __get_alt(std::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
483 }
484};
485
486struct __base {
487 template <size_t _Ip, class _Vp>
488 _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) {
489 return __union::__get_alt(std::forward<_Vp>(__v).__data, in_place_index<_Ip>);
490 }
491};
492
493struct __variant {
494 template <size_t _Ip, class _Vp>
495 _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) {
496 return __base::__get_alt<_Ip>(std::forward<_Vp>(__v).__impl_);
497 }
498};
499
500} // namespace __access
501
502namespace __visitation {
503
504struct __base {
505 template <class _Visitor, class... _Vs>
506 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
507 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
508 constexpr auto __fdiagonal = __make_fdiagonal<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
509 return __fdiagonal[__index](std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...);
510 }
511
512 template <class _Visitor, class... _Vs>
513 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) {
514 constexpr auto __fmatrix = __make_fmatrix<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
515 return __at(__fmatrix, __vs.index()...)(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...);
516 }
517
518private:
519 template <class _Tp>
520 _LIBCPP_HIDE_FROM_ABI static constexpr const _Tp& __at(const _Tp& __elem) {
521 return __elem;
522 }
523
524 template <class _Tp, size_t _Np, typename... _Indices>
525 _LIBCPP_HIDE_FROM_ABI static constexpr auto&&
526 __at(const __farray<_Tp, _Np>& __elems, size_t __index, _Indices... __indices) {
527 return __at(__elems[__index], __indices...);
528 }
529
530 template <class _Fp, class... _Fs>
531 static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_visitor_return_type_check() {
532 static_assert(
533 __all<is_same_v<_Fp, _Fs>...>::value, "`std::visit` requires the visitor to have a single return type.");
534 }
535
536 template <class... _Fs>
537 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_farray(_Fs&&... __fs) {
538 __std_visit_visitor_return_type_check<__remove_cvref_t<_Fs>...>();
539 using __result = __farray<common_type_t<__remove_cvref_t<_Fs>...>, sizeof...(_Fs)>;
540 return __result{{std::forward<_Fs>(__fs)...}};
541 }
542
543 template <size_t... _Is>
544 struct __dispatcher {
545 template <class _Fp, class... _Vs>
546 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
547 return std::__invoke(static_cast<_Fp>(__f), __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
548 }
549 };
550
551 template <class _Fp, class... _Vs, size_t... _Is>
552 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_dispatch(index_sequence<_Is...>) {
553 return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
554 }
555
556 template <class, class _Tp>
557 using __expander _LIBCPP_NODEBUG = _Tp;
558
559 template <size_t _Ip, class _Fp, class... _Vs>
560 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl() {
561 return __make_dispatch<_Fp, _Vs...>(index_sequence<static_cast<__expander<_Vs, size_t>>(_Ip)...>{});
562 }
563
564 template <class _Fp, class... _Vs, size_t... _Is>
565 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
566 return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
567 }
568
569 template <class _Fp, class _Vp, class... _Vs>
570 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal() {
571 constexpr size_t __np = __remove_cvref_t<_Vp>::__size();
572 static_assert(__all<(__np == __remove_cvref_t<_Vs>::__size())...>::value);
573 return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<__np>{});
574 }
575
576 template <class _Fp, class... _Vs, size_t... _Is>
577 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
578 return __make_dispatch<_Fp, _Vs...>(__is);
579 }
580
581 template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
582 _LIBCPP_HIDE_FROM_ABI static constexpr auto
583 __make_fmatrix_impl(index_sequence<_Is...>, index_sequence<_Js...>, _Ls... __ls) {
584 return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(index_sequence<_Is..., _Js>{}, __ls...)...);
585 }
586
587 template <class _Fp, class... _Vs>
588 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix() {
589 return __make_fmatrix_impl<_Fp, _Vs...>(
590 index_sequence<>{}, make_index_sequence<__remove_cvref_t<_Vs>::__size()>{}...);
591 }
592};
593
594struct __variant {
595 template <class _Visitor, class... _Vs>
596 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
597 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
598 return __base::__visit_alt_at(__index, std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__impl_...);
599 }
600
601 template <class _Visitor, class... _Vs>
602 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) {
603 return __base::__visit_alt(
604 std::forward<_Visitor>(__visitor), std::__as_variant(std::forward<_Vs>(__vs)).__impl_...);
605 }
606
607 template <class _Visitor, class... _Vs>
608 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
609 __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
610 return __visit_alt_at(__index, __make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
611 }
612
613 template <class _Visitor, class... _Vs>
614 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_value(_Visitor&& __visitor, _Vs&&... __vs) {
615 return __visit_alt(__make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
616 }
617
618# if _LIBCPP_STD_VER >= 20
619 template <class _Rp, class _Visitor, class... _Vs>
620 _LIBCPP_HIDE_FROM_ABI static constexpr _Rp __visit_value(_Visitor&& __visitor, _Vs&&... __vs) {
621 return __visit_alt(__make_value_visitor<_Rp>(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
622 }
623# endif
624
625private:
626 template <class _Visitor, class... _Values>
627 static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_exhaustive_visitor_check() {
628 static_assert(is_invocable_v<_Visitor, _Values...>, "`std::visit` requires the visitor to be exhaustive.");
629 }
630
631 template <class _Visitor>
632 struct __value_visitor {
633 template <class... _Alts>
634 _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Alts&&... __alts) const
635 -> invoke_result_t<_Visitor&&, decltype((std::forward<_Alts>(__alts).__value))...> {
636 __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>();
637 return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
638 }
639 _Visitor&& __visitor;
640 };
641
642# if _LIBCPP_STD_VER >= 20
643 template <class _Rp, class _Visitor>
644 struct __value_visitor_return_type {
645 template <class... _Alts>
646 _LIBCPP_HIDE_FROM_ABI constexpr _Rp operator()(_Alts&&... __alts) const {
647 __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>();
648 if constexpr (is_void_v<_Rp>) {
649 std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
650 } else {
651 return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
652 }
653 }
654
655 _Visitor&& __visitor;
656 };
657# endif
658
659 template <class _Visitor>
660 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
661 return __value_visitor<_Visitor>{std::forward<_Visitor>(__visitor)};
662 }
663
664# if _LIBCPP_STD_VER >= 20
665 template <class _Rp, class _Visitor>
666 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
667 return __value_visitor_return_type<_Rp, _Visitor>{std::forward<_Visitor>(__visitor)};
668 }
669# endif
670};
671
672} // namespace __visitation
673
674// Adding semi-colons in macro expansions helps clang-format to do a better job.
675// This macro is used to avoid compilation errors due to "stray" semi-colons.
676# define _LIBCPP_EAT_SEMICOLON static_assert(true, "")
677
678template <size_t _Index, class _Tp>
679struct __alt {
680 using __value_type _LIBCPP_NODEBUG = _Tp;
681 static constexpr size_t __index = _Index;
682
683 template <class... _Args>
684 _LIBCPP_HIDE_FROM_ABI explicit constexpr __alt(in_place_t, _Args&&... __args)
685 : __value(std::forward<_Args>(__args)...) {}
686
687 __value_type __value;
688};
689
690template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
691union __union;
692
693template <_Trait _DestructibleTrait, size_t _Index>
694union __union<_DestructibleTrait, _Index> {};
695
696# define _LIBCPP_VARIANT_UNION(destructible_trait, destructor_definition) \
697 template <size_t _Index, class _Tp, class... _Types> \
698 union __union<destructible_trait, _Index, _Tp, _Types...> { \
699 public: \
700 _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
701 \
702 template <class... _Args> \
703 _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
704 : __head(in_place, std::forward<_Args>(__args)...) {} \
705 \
706 template <size_t _Ip, class... _Args> \
707 _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
708 : __tail(in_place_index<_Ip - 1>, std::forward<_Args>(__args)...) {} \
709 \
710 _LIBCPP_HIDE_FROM_ABI __union(const __union&) = default; \
711 _LIBCPP_HIDE_FROM_ABI __union(__union&&) = default; \
712 _LIBCPP_HIDE_FROM_ABI __union& operator=(const __union&) = default; \
713 _LIBCPP_HIDE_FROM_ABI __union& operator=(__union&&) = default; \
714 destructor_definition; \
715 \
716 private: \
717 char __dummy; \
718 __alt<_Index, _Tp> __head; \
719 __union<destructible_trait, _Index + 1, _Types...> __tail; \
720 \
721 friend struct __access::__union; \
722 }
723
724_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable,
725 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() = default);
726_LIBCPP_VARIANT_UNION(
727 _Trait::_Available, _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() {} _LIBCPP_EAT_SEMICOLON);
728_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() = delete);
729
730# undef _LIBCPP_VARIANT_UNION
731
732template <_Trait _DestructibleTrait, class... _Types>
733class __base {
734public:
735 using __index_t _LIBCPP_NODEBUG = __variant_index_t<sizeof...(_Types)>;
736
737 _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(__valueless_t __tag) noexcept
738 : __data(__tag), __index(__variant_npos<__index_t>) {}
739
740 template <size_t _Ip, class... _Args>
741 _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
742 : __data(in_place_index<_Ip>, std::forward<_Args>(__args)...), __index(_Ip) {}
743
744 _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept { return index() == variant_npos; }
745
746 _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept {
747 return __index == __variant_npos<__index_t> ? variant_npos : __index;
748 }
749
750protected:
751 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() & { return *this; }
752
753 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() && { return std::move(*this); }
754
755 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const& { return *this; }
756
757 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const&& { return std::move(*this); }
758
759 _LIBCPP_HIDE_FROM_ABI static constexpr size_t __size() { return sizeof...(_Types); }
760
761 __union<_DestructibleTrait, 0, _Types...> __data;
762 __index_t __index;
763
764 friend struct __access::__base;
765 friend struct __visitation::__base;
766};
767
768template <class _Traits, _Trait = _Traits::__destructible_trait>
769class __dtor;
770
771# define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor_definition, destroy) \
772 template <class... _Types> \
773 class __dtor<__traits<_Types...>, destructible_trait> : public __base<destructible_trait, _Types...> { \
774 using __base_type _LIBCPP_NODEBUG = __base<destructible_trait, _Types...>; \
775 using __index_t _LIBCPP_NODEBUG = typename __base_type::__index_t; \
776 \
777 public: \
778 using __base_type::__base_type; \
779 using __base_type::operator=; \
780 _LIBCPP_HIDE_FROM_ABI __dtor(const __dtor&) = default; \
781 _LIBCPP_HIDE_FROM_ABI __dtor(__dtor&&) = default; \
782 _LIBCPP_HIDE_FROM_ABI __dtor& operator=(const __dtor&) = default; \
783 _LIBCPP_HIDE_FROM_ABI __dtor& operator=(__dtor&&) = default; \
784 destructor_definition; \
785 \
786 protected: \
787 destroy; \
788 }
789
790_LIBCPP_VARIANT_DESTRUCTOR(
791 _Trait::_TriviallyAvailable,
792 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() = default,
793 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept {
794 this->__index = __variant_npos<__index_t>;
795 } _LIBCPP_EAT_SEMICOLON);
796
797_LIBCPP_VARIANT_DESTRUCTOR(
798 _Trait::_Available,
799 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() { __destroy(); } _LIBCPP_EAT_SEMICOLON,
800 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept {
801 if (!this->valueless_by_exception()) {
802 __visitation::__base::__visit_alt(
803 [](auto& __alt) noexcept {
804 using __alt_type = __remove_cvref_t<decltype(__alt)>;
805 __alt.~__alt_type();
806 },
807 *this);
808 }
809 this->__index = __variant_npos<__index_t>;
810 } _LIBCPP_EAT_SEMICOLON);
811
812_LIBCPP_VARIANT_DESTRUCTOR(_Trait::_Unavailable,
813 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() = delete,
814 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept = delete);
815
816# undef _LIBCPP_VARIANT_DESTRUCTOR
817
818template <class _Traits>
819class __ctor : public __dtor<_Traits> {
820 using __base_type _LIBCPP_NODEBUG = __dtor<_Traits>;
821
822public:
823 using __base_type::__base_type;
824 using __base_type::operator=;
825
826protected:
827 template <class _Rhs>
828 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
829 __lhs.__destroy();
830 if (!__rhs.valueless_by_exception()) {
831 auto __rhs_index = __rhs.index();
832 __visitation::__base::__visit_alt_at(
833 __rhs_index,
834 [&__lhs](auto&& __rhs_alt) {
835 std::__construct_at(std::addressof(__lhs.__data),
836 in_place_index<__decay_t<decltype(__rhs_alt)>::__index>,
837 std::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
838 },
839 std::forward<_Rhs>(__rhs));
840 __lhs.__index = __rhs_index;
841 }
842 }
843};
844
845template <class _Traits, _Trait = _Traits::__move_constructible_trait>
846class __move_constructor;
847
848# define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, move_constructor_definition) \
849 template <class... _Types> \
850 class __move_constructor<__traits<_Types...>, move_constructible_trait> : public __ctor<__traits<_Types...>> { \
851 using __base_type _LIBCPP_NODEBUG = __ctor<__traits<_Types...>>; \
852 \
853 public: \
854 using __base_type::__base_type; \
855 using __base_type::operator=; \
856 \
857 _LIBCPP_HIDE_FROM_ABI __move_constructor(const __move_constructor&) = default; \
858 _LIBCPP_HIDE_FROM_ABI ~__move_constructor() = default; \
859 _LIBCPP_HIDE_FROM_ABI __move_constructor& operator=(const __move_constructor&) = default; \
860 _LIBCPP_HIDE_FROM_ABI __move_constructor& operator=(__move_constructor&&) = default; \
861 move_constructor_definition; \
862 }
863
864_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
865 _Trait::_TriviallyAvailable,
866 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&& __that) = default);
867
868_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
869 _Trait::_Available,
870 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&& __that) noexcept(
871 __all<is_nothrow_move_constructible_v<_Types>...>::value) : __move_constructor(__valueless_t{}) {
872 this->__generic_construct(*this, std::move(__that));
873 } _LIBCPP_EAT_SEMICOLON);
874
875_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
876 _Trait::_Unavailable,
877 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&&) = delete);
878
879# undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
880
881template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
882class __copy_constructor;
883
884# define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, copy_constructor_definition) \
885 template <class... _Types> \
886 class __copy_constructor<__traits<_Types...>, copy_constructible_trait> \
887 : public __move_constructor<__traits<_Types...>> { \
888 using __base_type _LIBCPP_NODEBUG = __move_constructor<__traits<_Types...>>; \
889 \
890 public: \
891 using __base_type::__base_type; \
892 using __base_type::operator=; \
893 \
894 _LIBCPP_HIDE_FROM_ABI __copy_constructor(__copy_constructor&&) = default; \
895 _LIBCPP_HIDE_FROM_ABI ~__copy_constructor() = default; \
896 _LIBCPP_HIDE_FROM_ABI __copy_constructor& operator=(const __copy_constructor&) = default; \
897 _LIBCPP_HIDE_FROM_ABI __copy_constructor& operator=(__copy_constructor&&) = default; \
898 copy_constructor_definition; \
899 }
900
901_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
902 _Trait::_TriviallyAvailable,
903 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor& __that) = default);
904
905_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
906 _Trait::_Available,
907 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor& __that) noexcept(
908 __all<is_nothrow_copy_constructible_v<_Types>...>::value) : __copy_constructor(__valueless_t{}) {
909 this->__generic_construct(*this, __that);
910 } _LIBCPP_EAT_SEMICOLON);
911
912_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
913 _Trait::_Unavailable,
914 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor&) = delete);
915
916# undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
917
918template <class _Traits>
919class __assignment : public __copy_constructor<_Traits> {
920 using __base_type _LIBCPP_NODEBUG = __copy_constructor<_Traits>;
921
922public:
923 using __base_type::__base_type;
924 using __base_type::operator=;
925
926 template <size_t _Ip, class... _Args>
927 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto& __emplace(_Args&&... __args) {
928 this->__destroy();
929 std::__construct_at(std::addressof(this->__data), in_place_index<_Ip>, std::forward<_Args>(__args)...);
930 this->__index = _Ip;
931 return __access::__base::__get_alt<_Ip>(*this).__value;
932 }
933
934protected:
935 template <size_t _Ip, class _Tp, class _Arg>
936 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
937 if (this->index() == _Ip) {
938 __a.__value = std::forward<_Arg>(__arg);
939 } else if constexpr (is_nothrow_constructible_v<_Tp, _Arg> || !is_nothrow_move_constructible_v<_Tp>) {
940 this->__emplace<_Ip>(std::forward<_Arg>(__arg));
941 } else {
942 this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg)));
943 }
944 }
945
946 template <class _That>
947 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __generic_assign(_That&& __that) {
948 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
949 // do nothing.
950 } else if (__that.valueless_by_exception()) {
951 this->__destroy();
952 } else {
953 __visitation::__base::__visit_alt_at(
954 __that.index(),
955 [this](auto& __this_alt, auto&& __that_alt) {
956 this->__assign_alt(__this_alt, std::forward<decltype(__that_alt)>(__that_alt).__value);
957 },
958 *this,
959 std::forward<_That>(__that));
960 }
961 }
962};
963
964template <class _Traits, _Trait = _Traits::__move_assignable_trait>
965class __move_assignment;
966
967# define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, move_assignment_definition) \
968 template <class... _Types> \
969 class __move_assignment<__traits<_Types...>, move_assignable_trait> : public __assignment<__traits<_Types...>> { \
970 using __base_type _LIBCPP_NODEBUG = __assignment<__traits<_Types...>>; \
971 \
972 public: \
973 using __base_type::__base_type; \
974 using __base_type::operator=; \
975 \
976 _LIBCPP_HIDE_FROM_ABI __move_assignment(const __move_assignment&) = default; \
977 _LIBCPP_HIDE_FROM_ABI __move_assignment(__move_assignment&&) = default; \
978 _LIBCPP_HIDE_FROM_ABI ~__move_assignment() = default; \
979 _LIBCPP_HIDE_FROM_ABI __move_assignment& operator=(const __move_assignment&) = default; \
980 move_assignment_definition; \
981 }
982
983_LIBCPP_VARIANT_MOVE_ASSIGNMENT(_Trait::_TriviallyAvailable,
984 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment& operator=(
985 __move_assignment&& __that) = default);
986
987_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
988 _Trait::_Available,
989 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment&
990 operator=(__move_assignment&& __that) noexcept(
991 __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_move_assignable_v<_Types>)...>::value) {
992 this->__generic_assign(std::move(__that));
993 return *this;
994 } _LIBCPP_EAT_SEMICOLON);
995
996_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
997 _Trait::_Unavailable,
998 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment& operator=(__move_assignment&&) = delete);
999
1000# undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
1001
1002template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
1003class __copy_assignment;
1004
1005# define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, copy_assignment_definition) \
1006 template <class... _Types> \
1007 class __copy_assignment<__traits<_Types...>, copy_assignable_trait> \
1008 : public __move_assignment<__traits<_Types...>> { \
1009 using __base_type _LIBCPP_NODEBUG = __move_assignment<__traits<_Types...>>; \
1010 \
1011 public: \
1012 using __base_type::__base_type; \
1013 using __base_type::operator=; \
1014 \
1015 _LIBCPP_HIDE_FROM_ABI __copy_assignment(const __copy_assignment&) = default; \
1016 _LIBCPP_HIDE_FROM_ABI __copy_assignment(__copy_assignment&&) = default; \
1017 _LIBCPP_HIDE_FROM_ABI ~__copy_assignment() = default; \
1018 _LIBCPP_HIDE_FROM_ABI __copy_assignment& operator=(__copy_assignment&&) = default; \
1019 copy_assignment_definition; \
1020 }
1021
1022_LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_TriviallyAvailable,
1023 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment& operator=(
1024 const __copy_assignment& __that) = default);
1025
1026_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1027 _Trait::_Available,
1028 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment&
1029 operator=(const __copy_assignment& __that) {
1030 this->__generic_assign(__that);
1031 return *this;
1032 } _LIBCPP_EAT_SEMICOLON);
1033
1034_LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_Unavailable,
1035 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment& operator=(
1036 const __copy_assignment&) = delete);
1037
1038# undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1039
1040template <class... _Types>
1041class __impl : public __copy_assignment<__traits<_Types...>> {
1042 using __base_type _LIBCPP_NODEBUG = __copy_assignment<__traits<_Types...>>;
1043
1044public:
1045 using __base_type::__base_type; // get in_place_index_t constructor & friends
1046 _LIBCPP_HIDE_FROM_ABI __impl(__impl const&) = default;
1047 _LIBCPP_HIDE_FROM_ABI __impl(__impl&&) = default;
1048 _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl const&) = default;
1049 _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl&&) = default;
1050
1051 template <size_t _Ip, class _Arg>
1052 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign(_Arg&& __arg) {
1053 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this), std::forward<_Arg>(__arg));
1054 }
1055
1056 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __swap(__impl& __that) {
1057 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1058 // do nothing.
1059 } else if (this->index() == __that.index()) {
1060 __visitation::__base::__visit_alt_at(
1061 this->index(),
1062 [](auto& __this_alt, auto& __that_alt) {
1063 using std::swap;
1064 swap(__this_alt.__value, __that_alt.__value);
1065 },
1066 *this,
1067 __that);
1068 } else {
1069 __impl* __lhs = this;
1070 __impl* __rhs = std::addressof(__that);
1071 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1072 std::swap(__lhs, __rhs);
1073 }
1074 __impl __tmp(std::move(*__rhs));
1075# if _LIBCPP_HAS_EXCEPTIONS
1076 if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
1077 this->__generic_construct(*__rhs, std::move(*__lhs));
1078 } else {
1079 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1080 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1081 // into `__rhs` and provide the strong exception safety guarantee.
1082 try {
1083 this->__generic_construct(*__rhs, std::move(*__lhs));
1084 } catch (...) {
1085 if (__tmp.__move_nothrow()) {
1086 this->__generic_construct(*__rhs, std::move(__tmp));
1087 }
1088 throw;
1089 }
1090 }
1091# else
1092 // this isn't consolidated with the `if constexpr` branch above due to
1093 // `throw` being ill-formed with exceptions disabled even when discarded.
1094 this->__generic_construct(*__rhs, std::move(*__lhs));
1095# endif
1096 this->__generic_construct(*__lhs, std::move(__tmp));
1097 }
1098 }
1099
1100private:
1101 constexpr inline _LIBCPP_HIDE_FROM_ABI bool __move_nothrow() const {
1102 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1103 return this->valueless_by_exception() || __results[this->index()];
1104 }
1105};
1106
1107struct __no_narrowing_check {
1108 template <class _Dest, class _Source>
1109 using _Apply _LIBCPP_NODEBUG = __type_identity<_Dest>;
1110};
1111
1112struct __narrowing_check {
1113 template <class _Dest>
1114 static auto __test_impl(_Dest (&&)[1]) -> __type_identity<_Dest>;
1115 template <class _Dest, class _Source>
1116 using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({std::declval<_Source>()}));
1117};
1118
1119template <class _Dest, class _Source>
1120using __check_for_narrowing _LIBCPP_NODEBUG =
1121 typename _If< is_arithmetic<_Dest>::value, __narrowing_check, __no_narrowing_check >::template _Apply<_Dest,
1122 _Source>;
1123
1124template <class _Tp, size_t _Idx>
1125struct __overload {
1126 template <class _Up>
1127 auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
1128};
1129
1130template <class... _Bases>
1131struct __all_overloads : _Bases... {
1132 void operator()() const;
1133 using _Bases::operator()...;
1134};
1135
1136template <class _IdxSeq>
1137struct __make_overloads_imp;
1138
1139template <size_t... _Idx>
1140struct __make_overloads_imp<index_sequence<_Idx...> > {
1141 template <class... _Types>
1142 using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>;
1143};
1144
1145template <class... _Types>
1146using _MakeOverloads _LIBCPP_NODEBUG =
1147 typename __make_overloads_imp<make_index_sequence<sizeof...(_Types)>>::template _Apply<_Types...>;
1148
1149template <class _Tp, class... _Types>
1150using __best_match_t _LIBCPP_NODEBUG = typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
1151
1152} // namespace __variant_detail
1153
1154template <class _Visitor, class... _Vs, typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>>
1155_LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs);
1156
1157# if _LIBCPP_STD_VER >= 20
1158template <class _Rp,
1159 class _Visitor,
1160 class... _Vs,
1161 typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>>
1162_LIBCPP_HIDE_FROM_ABI constexpr _Rp visit(_Visitor&& __visitor, _Vs&&... __vs);
1163# endif
1164
1165template <class... _Types>
1166class _LIBCPP_DECLSPEC_EMPTY_BASES _LIBCPP_NO_SPECIALIZATIONS variant
1167 : private __sfinae_ctor_base< __all<is_copy_constructible_v<_Types>...>::value,
1168 __all<is_move_constructible_v<_Types>...>::value>,
1169 private __sfinae_assign_base<
1170 __all<(is_copy_constructible_v<_Types> && is_copy_assignable_v<_Types>)...>::value,
1171 __all<(is_move_constructible_v<_Types> && is_move_assignable_v<_Types>)...>::value> {
1172 static_assert(0 < sizeof...(_Types), "variant must consist of at least one alternative.");
1173
1174 static_assert(__all<!is_array_v<_Types>...>::value, "variant can not have an array type as an alternative.");
1175
1176 static_assert(__all<!is_reference_v<_Types>...>::value, "variant can not have a reference type as an alternative.");
1177
1178 static_assert(__all<!is_void_v<_Types>...>::value, "variant can not have a void type as an alternative.");
1179
1180 using __first_type _LIBCPP_NODEBUG = variant_alternative_t<0, variant>;
1181
1182public:
1183 using __trivially_relocatable _LIBCPP_NODEBUG =
1184 conditional_t<__all<__is_trivially_relocatable_v<_Types>...>::value, variant, void>;
1185
1186 template <class _FirstType = __first_type, enable_if_t<is_default_constructible_v<_FirstType>, int> = 0>
1187 _LIBCPP_HIDE_FROM_ABI constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1188 : __impl_(in_place_index<0>) {}
1189
1190 _LIBCPP_HIDE_FROM_ABI constexpr variant(const variant&) = default;
1191 _LIBCPP_HIDE_FROM_ABI constexpr variant(variant&&) = default;
1192
1193 template < class _Arg,
1194 enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1195 enable_if_t<!__is_inplace_type<__remove_cvref_t<_Arg>>::value, int> = 0,
1196 enable_if_t<!__is_inplace_index<__remove_cvref_t<_Arg>>::value, int> = 0,
1197 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1198 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1199 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1200 _LIBCPP_HIDE_FROM_ABI constexpr variant(_Arg&& __arg) noexcept(is_nothrow_constructible_v<_Tp, _Arg>)
1201 : __impl_(in_place_index<_Ip>, std::forward<_Arg>(__arg)) {}
1202
1203 template <size_t _Ip,
1204 class... _Args,
1205 class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
1206 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1207 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1208 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_index_t<_Ip>, _Args&&... __args) noexcept(
1209 is_nothrow_constructible_v<_Tp, _Args...>)
1210 : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
1211
1212 template < size_t _Ip,
1213 class _Up,
1214 class... _Args,
1215 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1216 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1217 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1218 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
1219 in_place_index_t<_Ip>,
1220 initializer_list<_Up> __il,
1221 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1222 : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
1223
1224 template < class _Tp,
1225 class... _Args,
1226 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1227 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1228 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1229 is_nothrow_constructible_v<_Tp, _Args...>)
1230 : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
1231
1232 template < class _Tp,
1233 class _Up,
1234 class... _Args,
1235 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1236 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1237 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
1238 in_place_type_t<_Tp>,
1239 initializer_list<_Up> __il,
1240 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1241 : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
1242
1243 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~variant() = default;
1244
1245 _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(const variant&) = default;
1246 _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(variant&&) = default;
1247
1248 template < class _Arg,
1249 enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1250 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1251 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1252 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>, int> = 0>
1253 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 variant&
1254 operator=(_Arg&& __arg) noexcept(is_nothrow_assignable_v<_Tp&, _Arg> && is_nothrow_constructible_v<_Tp, _Arg>) {
1255 __impl_.template __assign<_Ip>(std::forward<_Arg>(__arg));
1256 return *this;
1257 }
1258
1259 template < size_t _Ip,
1260 class... _Args,
1261 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1262 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1263 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1264 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
1265 return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
1266 }
1267
1268 template < size_t _Ip,
1269 class _Up,
1270 class... _Args,
1271 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1272 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1273 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1274 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1275 return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
1276 }
1277
1278 template < class _Tp,
1279 class... _Args,
1280 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1281 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1282 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
1283 return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
1284 }
1285
1286 template < class _Tp,
1287 class _Up,
1288 class... _Args,
1289 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1290 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1291 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1292 return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
1293 }
1294
1295 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept {
1296 return __impl_.valueless_by_exception();
1297 }
1298
1299 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { return __impl_.index(); }
1300
1301 template <
1302 bool _Dummy = true,
1303 enable_if_t<_And<_And<bool_constant<_Dummy>, is_move_constructible<_Types>, is_swappable<_Types>>...>::value,
1304 int> = 0>
1305 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(variant& __that) noexcept(
1306 __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_swappable_v<_Types>)...>::value) {
1307 __impl_.__swap(__that.__impl_);
1308 }
1309
1310# if _LIBCPP_STD_VER >= 26
1311 // Helper class to implement [variant.visit]/10
1312 // Constraints: The call to visit does not use an explicit template-argument-list
1313 // that begins with a type template-argument.
1314 struct __variant_visit_barrier_tag {
1315 _LIBCPP_HIDE_FROM_ABI explicit __variant_visit_barrier_tag() = default;
1316 };
1317
1318 template <__variant_visit_barrier_tag = __variant_visit_barrier_tag{}, class _Self, class _Visitor>
1319 _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) visit(this _Self&& __self, _Visitor&& __visitor) {
1320 return std::visit(std::forward<_Visitor>(__visitor), std::__forward_as<_Self, variant>(__self));
1321 }
1322
1323 template <class _Rp, class _Self, class _Visitor>
1324 _LIBCPP_HIDE_FROM_ABI constexpr _Rp visit(this _Self&& __self, _Visitor&& __visitor) {
1325 return std::visit<_Rp>(std::forward<_Visitor>(__visitor), std::__forward_as<_Self, variant>(__self));
1326 }
1327# endif
1328
1329private:
1330 __variant_detail::__impl<_Types...> __impl_;
1331
1332 friend struct __variant_detail::__access::__variant;
1333 friend struct __variant_detail::__visitation::__variant;
1334};
1335
1336template <class... _Types>
1337variant<_Types...> __upcast_to_variant(const volatile variant<_Types...>*);
1338void __upcast_to_variant(...);
1339
1340template <size_t _Ip, class... _Types>
1341_LIBCPP_HIDE_FROM_ABI constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1342 return __v.index() == _Ip;
1343}
1344
1345template <class _Tp, class... _Types>
1346[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1347 return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1348}
1349
1350template <size_t _Ip, class _Vp>
1351_LIBCPP_HIDE_FROM_ABI constexpr auto&& __generic_get(_Vp&& __v) {
1352 using __variant_detail::__access::__variant;
1353 if (!std::__holds_alternative<_Ip>(__v)) {
1354 if (__v.valueless_by_exception())
1355 std::__throw_bad_variant_access(msg: "std::get: variant is valueless");
1356 std::__throw_bad_variant_access(msg: "std::get: wrong alternative for variant");
1357 }
1358 return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
1359}
1360
1361template <size_t _Ip, class... _Types>
1362[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr variant_alternative_t<_Ip, variant<_Types...>>&
1363get(variant<_Types...>& __v) {
1364 static_assert(_Ip < sizeof...(_Types));
1365 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1366 return std::__generic_get<_Ip>(__v);
1367}
1368
1369template <size_t _Ip, class... _Types>
1370[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr variant_alternative_t<_Ip, variant<_Types...>>&&
1371get(variant<_Types...>&& __v) {
1372 static_assert(_Ip < sizeof...(_Types));
1373 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1374 return std::__generic_get<_Ip>(std::move(__v));
1375}
1376
1377template <size_t _Ip, class... _Types>
1378[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const variant_alternative_t<_Ip, variant<_Types...>>&
1379get(const variant<_Types...>& __v) {
1380 static_assert(_Ip < sizeof...(_Types));
1381 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1382 return std::__generic_get<_Ip>(__v);
1383}
1384
1385template <size_t _Ip, class... _Types>
1386[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const variant_alternative_t<_Ip, variant<_Types...>>&&
1387get(const variant<_Types...>&& __v) {
1388 static_assert(_Ip < sizeof...(_Types));
1389 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1390 return std::__generic_get<_Ip>(std::move(__v));
1391}
1392
1393template <class _Tp, class... _Types>
1394[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& get(variant<_Types...>& __v) {
1395 static_assert(!is_void_v<_Tp>);
1396 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1397}
1398
1399template <class _Tp, class... _Types>
1400[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& get(variant<_Types...>&& __v) {
1401 static_assert(!is_void_v<_Tp>);
1402 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
1403}
1404
1405template <class _Tp, class... _Types>
1406[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& get(const variant<_Types...>& __v) {
1407 static_assert(!is_void_v<_Tp>);
1408 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1409}
1410
1411template <class _Tp, class... _Types>
1412[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1413 static_assert(!is_void_v<_Tp>);
1414 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
1415}
1416
1417template <size_t _Ip, class _Vp>
1418_LIBCPP_HIDE_FROM_ABI constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1419 using __variant_detail::__access::__variant;
1420 return __v && std::__holds_alternative<_Ip>(*__v) ? std::addressof(__variant::__get_alt<_Ip>(*__v).__value) : nullptr;
1421}
1422
1423template <size_t _Ip, class... _Types>
1424[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1425get_if(variant<_Types...>* __v) noexcept {
1426 static_assert(_Ip < sizeof...(_Types));
1427 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1428 return std::__generic_get_if<_Ip>(__v);
1429}
1430
1431template <size_t _Ip, class... _Types>
1432[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1433get_if(const variant<_Types...>* __v) noexcept {
1434 static_assert(_Ip < sizeof...(_Types));
1435 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1436 return std::__generic_get_if<_Ip>(__v);
1437}
1438
1439template <class _Tp, class... _Types>
1440[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> get_if(variant<_Types...>* __v) noexcept {
1441 static_assert(!is_void_v<_Tp>);
1442 return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1443}
1444
1445template <class _Tp, class... _Types>
1446[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const _Tp> get_if(const variant<_Types...>* __v) noexcept {
1447 static_assert(!is_void_v<_Tp>);
1448 return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1449}
1450
1451template <class _Operator>
1452struct __convert_to_bool {
1453 template <class _T1, class _T2>
1454 _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_T1&& __t1, _T2&& __t2) const {
1455 static_assert(is_convertible<decltype(_Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2))), bool>::value,
1456 "the relational operator does not return a type which is implicitly convertible to bool");
1457 return _Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2));
1458 }
1459};
1460
1461template <class... _Types>
1462# if _LIBCPP_STD_VER >= 26
1463 requires(requires(const _Types& __t) {
1464 { __t == __t } -> __core_convertible_to<bool>;
1465 } && ...)
1466# endif
1467_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1468 using __variant_detail::__visitation::__variant;
1469 if (__lhs.index() != __rhs.index())
1470 return false;
1471 if (__lhs.valueless_by_exception())
1472 return true;
1473 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
1474}
1475
1476# if _LIBCPP_STD_VER >= 20
1477
1478template <class... _Types>
1479 requires(three_way_comparable<_Types> && ...)
1480_LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...>
1481operator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1482 using __variant_detail::__visitation::__variant;
1483 using __result_t = common_comparison_category_t<compare_three_way_result_t<_Types>...>;
1484 if (__lhs.valueless_by_exception() && __rhs.valueless_by_exception())
1485 return strong_ordering::equal;
1486 if (__lhs.valueless_by_exception())
1487 return strong_ordering::less;
1488 if (__rhs.valueless_by_exception())
1489 return strong_ordering::greater;
1490 if (auto __c = __lhs.index() <=> __rhs.index(); __c != 0)
1491 return __c;
1492 auto __three_way = []<class _Type>(const _Type& __v, const _Type& __w) -> __result_t { return __v <=> __w; };
1493 return __variant::__visit_value_at(__lhs.index(), __three_way, __lhs, __rhs);
1494}
1495
1496# endif // _LIBCPP_STD_VER >= 20
1497
1498template <class... _Types>
1499# if _LIBCPP_STD_VER >= 26
1500 requires(requires(const _Types& __t) {
1501 { __t != __t } -> __core_convertible_to<bool>;
1502 } && ...)
1503# endif
1504_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1505 using __variant_detail::__visitation::__variant;
1506 if (__lhs.index() != __rhs.index())
1507 return true;
1508 if (__lhs.valueless_by_exception())
1509 return false;
1510 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
1511}
1512
1513template <class... _Types>
1514# if _LIBCPP_STD_VER >= 26
1515 requires(requires(const _Types& __t) {
1516 { __t < __t } -> __core_convertible_to<bool>;
1517 } && ...)
1518# endif
1519_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1520 using __variant_detail::__visitation::__variant;
1521 if (__rhs.valueless_by_exception())
1522 return false;
1523 if (__lhs.valueless_by_exception())
1524 return true;
1525 if (__lhs.index() < __rhs.index())
1526 return true;
1527 if (__lhs.index() > __rhs.index())
1528 return false;
1529 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
1530}
1531
1532template <class... _Types>
1533# if _LIBCPP_STD_VER >= 26
1534 requires(requires(const _Types& __t) {
1535 { __t > __t } -> __core_convertible_to<bool>;
1536 } && ...)
1537# endif
1538_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1539 using __variant_detail::__visitation::__variant;
1540 if (__lhs.valueless_by_exception())
1541 return false;
1542 if (__rhs.valueless_by_exception())
1543 return true;
1544 if (__lhs.index() > __rhs.index())
1545 return true;
1546 if (__lhs.index() < __rhs.index())
1547 return false;
1548 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
1549}
1550
1551template <class... _Types>
1552# if _LIBCPP_STD_VER >= 26
1553 requires(requires(const _Types& __t) {
1554 { __t <= __t } -> __core_convertible_to<bool>;
1555 } && ...)
1556# endif
1557_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1558 using __variant_detail::__visitation::__variant;
1559 if (__lhs.valueless_by_exception())
1560 return true;
1561 if (__rhs.valueless_by_exception())
1562 return false;
1563 if (__lhs.index() < __rhs.index())
1564 return true;
1565 if (__lhs.index() > __rhs.index())
1566 return false;
1567 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
1568}
1569
1570template <class... _Types>
1571# if _LIBCPP_STD_VER >= 26
1572 requires(requires(const _Types& __t) {
1573 { __t >= __t } -> __core_convertible_to<bool>;
1574 } && ...)
1575# endif
1576_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1577 using __variant_detail::__visitation::__variant;
1578 if (__rhs.valueless_by_exception())
1579 return true;
1580 if (__lhs.valueless_by_exception())
1581 return false;
1582 if (__lhs.index() > __rhs.index())
1583 return true;
1584 if (__lhs.index() < __rhs.index())
1585 return false;
1586 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
1587}
1588
1589template <class... _Vs>
1590_LIBCPP_HIDE_FROM_ABI constexpr void __throw_if_valueless(_Vs&&... __vs) {
1591 const bool __valueless = (... || std::__as_variant(__vs).valueless_by_exception());
1592 if (__valueless) {
1593 std::__throw_bad_variant_access(msg: "std::visit: variant is valueless");
1594 }
1595}
1596
1597# define _LIBCPP_VARIANT_DISPATCH_COUNT 11 // Speed up compilation for the common cases
1598
1599template < class _Visitor, class... _Vs, typename>
1600_LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
1601 // Determine the std::variant base class on which we call index() below.
1602 // This is important for avoiding calling index() on a derived class.
1603 using __variant_type = decltype(std::__upcast_to_variant(std::declval<remove_reference_t<_Vs>*>()...));
1604 constexpr size_t __variant_size =
1605 conditional_t<is_void_v<__variant_type>,
1606 std::integral_constant<size_t, variant_npos>,
1607 variant_size<__variant_type>>::value;
1608 if constexpr (__variant_size <= _LIBCPP_VARIANT_DISPATCH_COUNT) {
1609 using __variant_detail::__access::__variant;
1610# define _LIBCPP_VARIANT_DISPATCH_INDEX(_I) \
1611 case _I: \
1612 if constexpr (_I < __variant_size) { \
1613 return std::__invoke( \
1614 std::forward<_Visitor>(__visitor), __variant::__get_alt<_I>(std::forward<_Vs>(__vs)...).__value); \
1615 } \
1616 [[__fallthrough__]]
1617 // Use __LINE__ to avoid hardcoding numbers that might become stale
1618 constexpr size_t __i0 = __LINE__ + 2; // New line required
1619 switch ((..., __vs.__variant_type::index())) { // New line required
1620 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1621 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1622 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1623 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1624 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1625 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1626 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1627 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1628 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1629 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1630 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1631 static_assert(__LINE__ - __i0 == _LIBCPP_VARIANT_DISPATCH_COUNT, "index count mismatch");
1632 default:
1633 std::__throw_bad_variant_access(msg: "std::visit: variant is valueless");
1634 }
1635# undef _LIBCPP_VARIANT_DISPATCH_INDEX
1636 } else {
1637 using __variant_detail::__visitation::__variant;
1638 std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1639 // NOLINTNEXTLINE(bugprone-use-after-move) __throw_if_valueless doesn't actually forward the variants
1640 return __variant::__visit_value(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
1641 }
1642}
1643
1644# if _LIBCPP_STD_VER >= 20
1645template < class _Rp, class _Visitor, class... _Vs, typename>
1646_LIBCPP_HIDE_FROM_ABI constexpr _Rp visit(_Visitor&& __visitor, _Vs&&... __vs) {
1647 // Determine the std::variant base class on which we call index() below.
1648 // This is important for avoiding calling index() on a derived class.
1649 using __variant_type = decltype(std::__upcast_to_variant(std::declval<remove_reference_t<_Vs>*>()...));
1650 constexpr size_t __variant_size =
1651 conditional_t<is_void_v<__variant_type>,
1652 std::integral_constant<size_t, variant_npos>,
1653 variant_size<__variant_type>>::value;
1654 if constexpr (__variant_size <= _LIBCPP_VARIANT_DISPATCH_COUNT) {
1655 using __variant_detail::__access::__variant;
1656# define _LIBCPP_VARIANT_DISPATCH_INDEX(_I) \
1657 case _I: \
1658 if constexpr (_I < __variant_size) { \
1659 if constexpr (std::is_void_v<_Rp>) { \
1660 return (void)std::__invoke( \
1661 std::forward<_Visitor>(__visitor), __variant::__get_alt<_I>(std::forward<_Vs>(__vs)...).__value); \
1662 } else { \
1663 return std::__invoke( \
1664 std::forward<_Visitor>(__visitor), __variant::__get_alt<_I>(std::forward<_Vs>(__vs)...).__value); \
1665 } \
1666 } \
1667 [[__fallthrough__]]
1668 // Use __LINE__ to avoid hardcoding numbers that might become stale
1669 constexpr size_t __i0 = __LINE__ + 2; // New line required
1670 switch ((..., __vs.__variant_type::index())) { // New line required
1671 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1672 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1673 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1674 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1675 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1676 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1677 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1678 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1679 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1680 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1681 _LIBCPP_VARIANT_DISPATCH_INDEX(__LINE__ - __i0); // New line required
1682 static_assert(__LINE__ - __i0 == _LIBCPP_VARIANT_DISPATCH_COUNT, "index count mismatch");
1683 default:
1684 std::__throw_bad_variant_access(msg: "std::visit: variant is valueless");
1685 }
1686# undef _LIBCPP_VARIANT_DISPATCH_INDEX
1687 } else {
1688 using __variant_detail::__visitation::__variant;
1689 std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1690 return __variant::__visit_value<_Rp>(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
1691 }
1692}
1693# endif
1694
1695# undef _LIBCPP_VARIANT_DISPATCH_COUNT
1696
1697template <class... _Types>
1698_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto
1699swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1700 -> decltype(__lhs.swap(__rhs)) {
1701 return __lhs.swap(__rhs);
1702}
1703
1704template <class... _Types>
1705struct hash< __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
1706# if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1707 using argument_type _LIBCPP_DEPRECATED_IN_CXX17 = variant<_Types...>;
1708 using result_type _LIBCPP_DEPRECATED_IN_CXX17 = size_t;
1709# endif
1710
1711 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const variant<_Types...>& __v) const {
1712 using __variant_detail::__visitation::__variant;
1713 size_t __res =
1714 __v.valueless_by_exception()
1715 ? 299792458 // Random value chosen by the universe upon creation
1716 : __variant::__visit_alt(
1717 [](const auto& __alt) {
1718 using __alt_type = __remove_cvref_t<decltype(__alt)>;
1719 using __value_type = remove_const_t< typename __alt_type::__value_type>;
1720 return hash<__value_type>{}(__alt.__value);
1721 },
1722 __v);
1723 return std::__hash_combine(lhs: __res, rhs: hash<size_t>{}(__v.index()));
1724 }
1725};
1726
1727// __unchecked_get is the same as std::get, except, it is UB to use it with the wrong
1728// type whereas std::get will throw or returning nullptr. This makes it faster than
1729// std::get.
1730template <size_t _Ip, class _Vp>
1731_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(_Vp&& __v) noexcept {
1732 using __variant_detail::__access::__variant;
1733 return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
1734}
1735
1736template <class _Tp, class... _Types>
1737_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept {
1738 return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1739}
1740
1741template <class _Tp, class... _Types>
1742_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept {
1743 return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1744}
1745
1746_LIBCPP_END_NAMESPACE_STD
1747
1748# endif // _LIBCPP_STD_VER >= 17
1749
1750_LIBCPP_POP_MACROS
1751
1752#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
1753
1754#endif // _LIBCPP_VARIANT
1755