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&);
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_same.h>
250# include <__type_traits/is_swappable.h>
251# include <__type_traits/is_trivially_assignable.h>
252# include <__type_traits/is_trivially_constructible.h>
253# include <__type_traits/is_trivially_destructible.h>
254# include <__type_traits/is_trivially_relocatable.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
908 _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor& __that) : __copy_constructor(
909 __valueless_t{}) { this->__generic_construct(*this, __that); } _LIBCPP_EAT_SEMICOLON);
910
911_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
912 _Trait::_Unavailable,
913 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor&) = delete);
914
915# undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
916
917template <class _Traits>
918class __assignment : public __copy_constructor<_Traits> {
919 using __base_type _LIBCPP_NODEBUG = __copy_constructor<_Traits>;
920
921public:
922 using __base_type::__base_type;
923 using __base_type::operator=;
924
925 template <size_t _Ip, class... _Args>
926 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto& __emplace(_Args&&... __args) {
927 this->__destroy();
928 std::__construct_at(std::addressof(this->__data), in_place_index<_Ip>, std::forward<_Args>(__args)...);
929 this->__index = _Ip;
930 return __access::__base::__get_alt<_Ip>(*this).__value;
931 }
932
933protected:
934 template <size_t _Ip, class _Tp, class _Arg>
935 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
936 if (this->index() == _Ip) {
937 __a.__value = std::forward<_Arg>(__arg);
938 } else if constexpr (is_nothrow_constructible_v<_Tp, _Arg> || !is_nothrow_move_constructible_v<_Tp>) {
939 this->__emplace<_Ip>(std::forward<_Arg>(__arg));
940 } else {
941 this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg)));
942 }
943 }
944
945 template <class _That>
946 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __generic_assign(_That&& __that) {
947 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
948 // do nothing.
949 } else if (__that.valueless_by_exception()) {
950 this->__destroy();
951 } else {
952 __visitation::__base::__visit_alt_at(
953 __that.index(),
954 [this](auto& __this_alt, auto&& __that_alt) {
955 this->__assign_alt(__this_alt, std::forward<decltype(__that_alt)>(__that_alt).__value);
956 },
957 *this,
958 std::forward<_That>(__that));
959 }
960 }
961};
962
963template <class _Traits, _Trait = _Traits::__move_assignable_trait>
964class __move_assignment;
965
966# define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, move_assignment_definition) \
967 template <class... _Types> \
968 class __move_assignment<__traits<_Types...>, move_assignable_trait> : public __assignment<__traits<_Types...>> { \
969 using __base_type _LIBCPP_NODEBUG = __assignment<__traits<_Types...>>; \
970 \
971 public: \
972 using __base_type::__base_type; \
973 using __base_type::operator=; \
974 \
975 _LIBCPP_HIDE_FROM_ABI __move_assignment(const __move_assignment&) = default; \
976 _LIBCPP_HIDE_FROM_ABI __move_assignment(__move_assignment&&) = default; \
977 _LIBCPP_HIDE_FROM_ABI ~__move_assignment() = default; \
978 _LIBCPP_HIDE_FROM_ABI __move_assignment& operator=(const __move_assignment&) = default; \
979 move_assignment_definition; \
980 }
981
982_LIBCPP_VARIANT_MOVE_ASSIGNMENT(_Trait::_TriviallyAvailable,
983 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment& operator=(
984 __move_assignment&& __that) = default);
985
986_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
987 _Trait::_Available,
988 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment&
989 operator=(__move_assignment&& __that) noexcept(
990 __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_move_assignable_v<_Types>)...>::value) {
991 this->__generic_assign(std::move(__that));
992 return *this;
993 } _LIBCPP_EAT_SEMICOLON);
994
995_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
996 _Trait::_Unavailable,
997 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment& operator=(__move_assignment&&) = delete);
998
999# undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
1000
1001template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
1002class __copy_assignment;
1003
1004# define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, copy_assignment_definition) \
1005 template <class... _Types> \
1006 class __copy_assignment<__traits<_Types...>, copy_assignable_trait> \
1007 : public __move_assignment<__traits<_Types...>> { \
1008 using __base_type _LIBCPP_NODEBUG = __move_assignment<__traits<_Types...>>; \
1009 \
1010 public: \
1011 using __base_type::__base_type; \
1012 using __base_type::operator=; \
1013 \
1014 _LIBCPP_HIDE_FROM_ABI __copy_assignment(const __copy_assignment&) = default; \
1015 _LIBCPP_HIDE_FROM_ABI __copy_assignment(__copy_assignment&&) = default; \
1016 _LIBCPP_HIDE_FROM_ABI ~__copy_assignment() = default; \
1017 _LIBCPP_HIDE_FROM_ABI __copy_assignment& operator=(__copy_assignment&&) = default; \
1018 copy_assignment_definition; \
1019 }
1020
1021_LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_TriviallyAvailable,
1022 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment& operator=(
1023 const __copy_assignment& __that) = default);
1024
1025_LIBCPP_VARIANT_COPY_ASSIGNMENT(
1026 _Trait::_Available,
1027 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment&
1028 operator=(const __copy_assignment& __that) {
1029 this->__generic_assign(__that);
1030 return *this;
1031 } _LIBCPP_EAT_SEMICOLON);
1032
1033_LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_Unavailable,
1034 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment& operator=(
1035 const __copy_assignment&) = delete);
1036
1037# undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1038
1039template <class... _Types>
1040class __impl : public __copy_assignment<__traits<_Types...>> {
1041 using __base_type _LIBCPP_NODEBUG = __copy_assignment<__traits<_Types...>>;
1042
1043public:
1044 using __base_type::__base_type; // get in_place_index_t constructor & friends
1045 _LIBCPP_HIDE_FROM_ABI __impl(__impl const&) = default;
1046 _LIBCPP_HIDE_FROM_ABI __impl(__impl&&) = default;
1047 _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl const&) = default;
1048 _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl&&) = default;
1049
1050 template <size_t _Ip, class _Arg>
1051 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign(_Arg&& __arg) {
1052 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this), std::forward<_Arg>(__arg));
1053 }
1054
1055 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __swap(__impl& __that) {
1056 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1057 // do nothing.
1058 } else if (this->index() == __that.index()) {
1059 __visitation::__base::__visit_alt_at(
1060 this->index(),
1061 [](auto& __this_alt, auto& __that_alt) {
1062 using std::swap;
1063 swap(__this_alt.__value, __that_alt.__value);
1064 },
1065 *this,
1066 __that);
1067 } else {
1068 __impl* __lhs = this;
1069 __impl* __rhs = std::addressof(__that);
1070 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1071 std::swap(__lhs, __rhs);
1072 }
1073 __impl __tmp(std::move(*__rhs));
1074# if _LIBCPP_HAS_EXCEPTIONS
1075 if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
1076 this->__generic_construct(*__rhs, std::move(*__lhs));
1077 } else {
1078 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1079 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1080 // into `__rhs` and provide the strong exception safety guarantee.
1081 try {
1082 this->__generic_construct(*__rhs, std::move(*__lhs));
1083 } catch (...) {
1084 if (__tmp.__move_nothrow()) {
1085 this->__generic_construct(*__rhs, std::move(__tmp));
1086 }
1087 throw;
1088 }
1089 }
1090# else
1091 // this isn't consolidated with the `if constexpr` branch above due to
1092 // `throw` being ill-formed with exceptions disabled even when discarded.
1093 this->__generic_construct(*__rhs, std::move(*__lhs));
1094# endif
1095 this->__generic_construct(*__lhs, std::move(__tmp));
1096 }
1097 }
1098
1099private:
1100 constexpr inline _LIBCPP_HIDE_FROM_ABI bool __move_nothrow() const {
1101 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1102 return this->valueless_by_exception() || __results[this->index()];
1103 }
1104};
1105
1106struct __no_narrowing_check {
1107 template <class _Dest, class _Source>
1108 using _Apply _LIBCPP_NODEBUG = __type_identity<_Dest>;
1109};
1110
1111struct __narrowing_check {
1112 template <class _Dest>
1113 static auto __test_impl(_Dest (&&)[1]) -> __type_identity<_Dest>;
1114 template <class _Dest, class _Source>
1115 using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({std::declval<_Source>()}));
1116};
1117
1118template <class _Dest, class _Source>
1119using __check_for_narrowing _LIBCPP_NODEBUG =
1120 typename _If< is_arithmetic<_Dest>::value, __narrowing_check, __no_narrowing_check >::template _Apply<_Dest,
1121 _Source>;
1122
1123template <class _Tp, size_t _Idx>
1124struct __overload {
1125 template <class _Up>
1126 auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
1127};
1128
1129template <class... _Bases>
1130struct __all_overloads : _Bases... {
1131 void operator()() const;
1132 using _Bases::operator()...;
1133};
1134
1135template <class _IdxSeq>
1136struct __make_overloads_imp;
1137
1138template <size_t... _Idx>
1139struct __make_overloads_imp<index_sequence<_Idx...> > {
1140 template <class... _Types>
1141 using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>;
1142};
1143
1144template <class... _Types>
1145using _MakeOverloads _LIBCPP_NODEBUG =
1146 typename __make_overloads_imp<make_index_sequence<sizeof...(_Types)>>::template _Apply<_Types...>;
1147
1148template <class _Tp, class... _Types>
1149using __best_match_t _LIBCPP_NODEBUG = typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
1150
1151} // namespace __variant_detail
1152
1153template <class _Visitor, class... _Vs, typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>>
1154_LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs);
1155
1156# if _LIBCPP_STD_VER >= 20
1157template <class _Rp,
1158 class _Visitor,
1159 class... _Vs,
1160 typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>>
1161_LIBCPP_HIDE_FROM_ABI constexpr _Rp visit(_Visitor&& __visitor, _Vs&&... __vs);
1162# endif
1163
1164template <class... _Types>
1165class _LIBCPP_DECLSPEC_EMPTY_BASES _LIBCPP_NO_SPECIALIZATIONS variant
1166 : private __sfinae_ctor_base< __all<is_copy_constructible_v<_Types>...>::value,
1167 __all<is_move_constructible_v<_Types>...>::value>,
1168 private __sfinae_assign_base<
1169 __all<(is_copy_constructible_v<_Types> && is_copy_assignable_v<_Types>)...>::value,
1170 __all<(is_move_constructible_v<_Types> && is_move_assignable_v<_Types>)...>::value> {
1171 static_assert(0 < sizeof...(_Types), "variant must consist of at least one alternative.");
1172
1173 static_assert(__all<!is_array_v<_Types>...>::value, "variant can not have an array type as an alternative.");
1174
1175 static_assert(__all<!is_reference_v<_Types>...>::value, "variant can not have a reference type as an alternative.");
1176
1177 static_assert(__all<!is_void_v<_Types>...>::value, "variant can not have a void type as an alternative.");
1178
1179 using __first_type _LIBCPP_NODEBUG = variant_alternative_t<0, variant>;
1180
1181public:
1182 using __trivially_relocatable _LIBCPP_NODEBUG =
1183 conditional_t<_And<__libcpp_is_trivially_relocatable<_Types>...>::value, variant, void>;
1184
1185 template <class _FirstType = __first_type, enable_if_t<is_default_constructible_v<_FirstType>, int> = 0>
1186 _LIBCPP_HIDE_FROM_ABI constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1187 : __impl_(in_place_index<0>) {}
1188
1189 _LIBCPP_HIDE_FROM_ABI constexpr variant(const variant&) = default;
1190 _LIBCPP_HIDE_FROM_ABI constexpr variant(variant&&) = default;
1191
1192 template < class _Arg,
1193 enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1194 enable_if_t<!__is_inplace_type<__remove_cvref_t<_Arg>>::value, int> = 0,
1195 enable_if_t<!__is_inplace_index<__remove_cvref_t<_Arg>>::value, int> = 0,
1196 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1197 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1198 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1199 _LIBCPP_HIDE_FROM_ABI constexpr variant(_Arg&& __arg) noexcept(is_nothrow_constructible_v<_Tp, _Arg>)
1200 : __impl_(in_place_index<_Ip>, std::forward<_Arg>(__arg)) {}
1201
1202 template <size_t _Ip,
1203 class... _Args,
1204 class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
1205 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1206 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1207 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_index_t<_Ip>, _Args&&... __args) noexcept(
1208 is_nothrow_constructible_v<_Tp, _Args...>)
1209 : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
1210
1211 template < size_t _Ip,
1212 class _Up,
1213 class... _Args,
1214 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1215 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1216 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1217 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
1218 in_place_index_t<_Ip>,
1219 initializer_list<_Up> __il,
1220 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1221 : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
1222
1223 template < class _Tp,
1224 class... _Args,
1225 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1226 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1227 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1228 is_nothrow_constructible_v<_Tp, _Args...>)
1229 : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
1230
1231 template < class _Tp,
1232 class _Up,
1233 class... _Args,
1234 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1235 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1236 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
1237 in_place_type_t<_Tp>,
1238 initializer_list<_Up> __il,
1239 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1240 : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
1241
1242 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~variant() = default;
1243
1244 _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(const variant&) = default;
1245 _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(variant&&) = default;
1246
1247 template < class _Arg,
1248 enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1249 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1250 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1251 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>, int> = 0>
1252 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 variant&
1253 operator=(_Arg&& __arg) noexcept(is_nothrow_assignable_v<_Tp&, _Arg> && is_nothrow_constructible_v<_Tp, _Arg>) {
1254 __impl_.template __assign<_Ip>(std::forward<_Arg>(__arg));
1255 return *this;
1256 }
1257
1258 template < size_t _Ip,
1259 class... _Args,
1260 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1261 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1262 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1263 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
1264 return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
1265 }
1266
1267 template < size_t _Ip,
1268 class _Up,
1269 class... _Args,
1270 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1271 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1272 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1273 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1274 return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
1275 }
1276
1277 template < class _Tp,
1278 class... _Args,
1279 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1280 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1281 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
1282 return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
1283 }
1284
1285 template < class _Tp,
1286 class _Up,
1287 class... _Args,
1288 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1289 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1290 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1291 return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
1292 }
1293
1294 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept {
1295 return __impl_.valueless_by_exception();
1296 }
1297
1298 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { return __impl_.index(); }
1299
1300 template <
1301 bool _Dummy = true,
1302 enable_if_t<_And<_And<bool_constant<_Dummy>, is_move_constructible<_Types>, is_swappable<_Types>>...>::value,
1303 int> = 0>
1304 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(variant& __that) noexcept(
1305 __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_swappable_v<_Types>)...>::value) {
1306 __impl_.__swap(__that.__impl_);
1307 }
1308
1309# if _LIBCPP_STD_VER >= 26
1310 // Helper class to implement [variant.visit]/10
1311 // Constraints: The call to visit does not use an explicit template-argument-list
1312 // that begins with a type template-argument.
1313 struct __variant_visit_barrier_tag {
1314 _LIBCPP_HIDE_FROM_ABI explicit __variant_visit_barrier_tag() = default;
1315 };
1316
1317 template <__variant_visit_barrier_tag = __variant_visit_barrier_tag{}, class _Self, class _Visitor>
1318 _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) visit(this _Self&& __self, _Visitor&& __visitor) {
1319 return std::visit(std::forward<_Visitor>(__visitor), std::__forward_as<_Self, variant>(__self));
1320 }
1321
1322 template <class _Rp, class _Self, class _Visitor>
1323 _LIBCPP_HIDE_FROM_ABI constexpr _Rp visit(this _Self&& __self, _Visitor&& __visitor) {
1324 return std::visit<_Rp>(std::forward<_Visitor>(__visitor), std::__forward_as<_Self, variant>(__self));
1325 }
1326# endif
1327
1328private:
1329 __variant_detail::__impl<_Types...> __impl_;
1330
1331 friend struct __variant_detail::__access::__variant;
1332 friend struct __variant_detail::__visitation::__variant;
1333};
1334
1335template <size_t _Ip, class... _Types>
1336_LIBCPP_HIDE_FROM_ABI constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1337 return __v.index() == _Ip;
1338}
1339
1340template <class _Tp, class... _Types>
1341[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1342 return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1343}
1344
1345template <size_t _Ip, class _Vp>
1346_LIBCPP_HIDE_FROM_ABI constexpr auto&& __generic_get(_Vp&& __v) {
1347 using __variant_detail::__access::__variant;
1348 if (!std::__holds_alternative<_Ip>(__v)) {
1349 if (__v.valueless_by_exception())
1350 std::__throw_bad_variant_access(msg: "std::get: variant is valueless");
1351 std::__throw_bad_variant_access(msg: "std::get: wrong alternative for variant");
1352 }
1353 return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
1354}
1355
1356template <size_t _Ip, class... _Types>
1357[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr variant_alternative_t<_Ip, variant<_Types...>>&
1358get(variant<_Types...>& __v) {
1359 static_assert(_Ip < sizeof...(_Types));
1360 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1361 return std::__generic_get<_Ip>(__v);
1362}
1363
1364template <size_t _Ip, class... _Types>
1365[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr variant_alternative_t<_Ip, variant<_Types...>>&&
1366get(variant<_Types...>&& __v) {
1367 static_assert(_Ip < sizeof...(_Types));
1368 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1369 return std::__generic_get<_Ip>(std::move(__v));
1370}
1371
1372template <size_t _Ip, class... _Types>
1373[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const variant_alternative_t<_Ip, variant<_Types...>>&
1374get(const variant<_Types...>& __v) {
1375 static_assert(_Ip < sizeof...(_Types));
1376 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1377 return std::__generic_get<_Ip>(__v);
1378}
1379
1380template <size_t _Ip, class... _Types>
1381[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const variant_alternative_t<_Ip, variant<_Types...>>&&
1382get(const variant<_Types...>&& __v) {
1383 static_assert(_Ip < sizeof...(_Types));
1384 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1385 return std::__generic_get<_Ip>(std::move(__v));
1386}
1387
1388template <class _Tp, class... _Types>
1389[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& get(variant<_Types...>& __v) {
1390 static_assert(!is_void_v<_Tp>);
1391 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1392}
1393
1394template <class _Tp, class... _Types>
1395[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& get(variant<_Types...>&& __v) {
1396 static_assert(!is_void_v<_Tp>);
1397 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
1398}
1399
1400template <class _Tp, class... _Types>
1401[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& get(const variant<_Types...>& __v) {
1402 static_assert(!is_void_v<_Tp>);
1403 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1404}
1405
1406template <class _Tp, class... _Types>
1407[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1408 static_assert(!is_void_v<_Tp>);
1409 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
1410}
1411
1412template <size_t _Ip, class _Vp>
1413_LIBCPP_HIDE_FROM_ABI constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1414 using __variant_detail::__access::__variant;
1415 return __v && std::__holds_alternative<_Ip>(*__v) ? std::addressof(__variant::__get_alt<_Ip>(*__v).__value) : nullptr;
1416}
1417
1418template <size_t _Ip, class... _Types>
1419[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1420get_if(variant<_Types...>* __v) noexcept {
1421 static_assert(_Ip < sizeof...(_Types));
1422 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1423 return std::__generic_get_if<_Ip>(__v);
1424}
1425
1426template <size_t _Ip, class... _Types>
1427[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1428get_if(const variant<_Types...>* __v) noexcept {
1429 static_assert(_Ip < sizeof...(_Types));
1430 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1431 return std::__generic_get_if<_Ip>(__v);
1432}
1433
1434template <class _Tp, class... _Types>
1435[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> get_if(variant<_Types...>* __v) noexcept {
1436 static_assert(!is_void_v<_Tp>);
1437 return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1438}
1439
1440template <class _Tp, class... _Types>
1441[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const _Tp> get_if(const variant<_Types...>* __v) noexcept {
1442 static_assert(!is_void_v<_Tp>);
1443 return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1444}
1445
1446template <class _Operator>
1447struct __convert_to_bool {
1448 template <class _T1, class _T2>
1449 _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_T1&& __t1, _T2&& __t2) const {
1450 static_assert(is_convertible<decltype(_Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2))), bool>::value,
1451 "the relational operator does not return a type which is implicitly convertible to bool");
1452 return _Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2));
1453 }
1454};
1455
1456template <class... _Types>
1457# if _LIBCPP_STD_VER >= 26
1458 requires(requires(const _Types& __t) {
1459 { __t == __t } -> __core_convertible_to<bool>;
1460 } && ...)
1461# endif
1462_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1463 using __variant_detail::__visitation::__variant;
1464 if (__lhs.index() != __rhs.index())
1465 return false;
1466 if (__lhs.valueless_by_exception())
1467 return true;
1468 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
1469}
1470
1471# if _LIBCPP_STD_VER >= 20
1472
1473template <class... _Types>
1474 requires(three_way_comparable<_Types> && ...)
1475_LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...>
1476operator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1477 using __variant_detail::__visitation::__variant;
1478 using __result_t = common_comparison_category_t<compare_three_way_result_t<_Types>...>;
1479 if (__lhs.valueless_by_exception() && __rhs.valueless_by_exception())
1480 return strong_ordering::equal;
1481 if (__lhs.valueless_by_exception())
1482 return strong_ordering::less;
1483 if (__rhs.valueless_by_exception())
1484 return strong_ordering::greater;
1485 if (auto __c = __lhs.index() <=> __rhs.index(); __c != 0)
1486 return __c;
1487 auto __three_way = []<class _Type>(const _Type& __v, const _Type& __w) -> __result_t { return __v <=> __w; };
1488 return __variant::__visit_value_at(__lhs.index(), __three_way, __lhs, __rhs);
1489}
1490
1491# endif // _LIBCPP_STD_VER >= 20
1492
1493template <class... _Types>
1494# if _LIBCPP_STD_VER >= 26
1495 requires(requires(const _Types& __t) {
1496 { __t != __t } -> __core_convertible_to<bool>;
1497 } && ...)
1498# endif
1499_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1500 using __variant_detail::__visitation::__variant;
1501 if (__lhs.index() != __rhs.index())
1502 return true;
1503 if (__lhs.valueless_by_exception())
1504 return false;
1505 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
1506}
1507
1508template <class... _Types>
1509# if _LIBCPP_STD_VER >= 26
1510 requires(requires(const _Types& __t) {
1511 { __t < __t } -> __core_convertible_to<bool>;
1512 } && ...)
1513# endif
1514_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1515 using __variant_detail::__visitation::__variant;
1516 if (__rhs.valueless_by_exception())
1517 return false;
1518 if (__lhs.valueless_by_exception())
1519 return true;
1520 if (__lhs.index() < __rhs.index())
1521 return true;
1522 if (__lhs.index() > __rhs.index())
1523 return false;
1524 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
1525}
1526
1527template <class... _Types>
1528# if _LIBCPP_STD_VER >= 26
1529 requires(requires(const _Types& __t) {
1530 { __t > __t } -> __core_convertible_to<bool>;
1531 } && ...)
1532# endif
1533_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1534 using __variant_detail::__visitation::__variant;
1535 if (__lhs.valueless_by_exception())
1536 return false;
1537 if (__rhs.valueless_by_exception())
1538 return true;
1539 if (__lhs.index() > __rhs.index())
1540 return true;
1541 if (__lhs.index() < __rhs.index())
1542 return false;
1543 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
1544}
1545
1546template <class... _Types>
1547# if _LIBCPP_STD_VER >= 26
1548 requires(requires(const _Types& __t) {
1549 { __t <= __t } -> __core_convertible_to<bool>;
1550 } && ...)
1551# endif
1552_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1553 using __variant_detail::__visitation::__variant;
1554 if (__lhs.valueless_by_exception())
1555 return true;
1556 if (__rhs.valueless_by_exception())
1557 return false;
1558 if (__lhs.index() < __rhs.index())
1559 return true;
1560 if (__lhs.index() > __rhs.index())
1561 return false;
1562 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
1563}
1564
1565template <class... _Types>
1566# if _LIBCPP_STD_VER >= 26
1567 requires(requires(const _Types& __t) {
1568 { __t >= __t } -> __core_convertible_to<bool>;
1569 } && ...)
1570# endif
1571_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1572 using __variant_detail::__visitation::__variant;
1573 if (__rhs.valueless_by_exception())
1574 return true;
1575 if (__lhs.valueless_by_exception())
1576 return false;
1577 if (__lhs.index() > __rhs.index())
1578 return true;
1579 if (__lhs.index() < __rhs.index())
1580 return false;
1581 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
1582}
1583
1584template <class... _Vs>
1585_LIBCPP_HIDE_FROM_ABI constexpr void __throw_if_valueless(_Vs&&... __vs) {
1586 const bool __valueless = (... || std::__as_variant(__vs).valueless_by_exception());
1587 if (__valueless) {
1588 std::__throw_bad_variant_access(msg: "std::visit: variant is valueless");
1589 }
1590}
1591
1592template < class _Visitor, class... _Vs, typename>
1593_LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
1594 using __variant_detail::__visitation::__variant;
1595 std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1596 // NOLINTNEXTLINE(bugprone-use-after-move) __throw_if_valueless doesn't actually forward the variants
1597 return __variant::__visit_value(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
1598}
1599
1600# if _LIBCPP_STD_VER >= 20
1601template < class _Rp, class _Visitor, class... _Vs, typename>
1602_LIBCPP_HIDE_FROM_ABI constexpr _Rp visit(_Visitor&& __visitor, _Vs&&... __vs) {
1603 using __variant_detail::__visitation::__variant;
1604 std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1605 return __variant::__visit_value<_Rp>(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
1606}
1607# endif
1608
1609template <class... _Types>
1610_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto
1611swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs)))
1612 -> decltype(__lhs.swap(__rhs)) {
1613 return __lhs.swap(__rhs);
1614}
1615
1616template <class... _Types>
1617struct hash< __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
1618# if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1619 using argument_type _LIBCPP_DEPRECATED_IN_CXX17 = variant<_Types...>;
1620 using result_type _LIBCPP_DEPRECATED_IN_CXX17 = size_t;
1621# endif
1622
1623 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const variant<_Types...>& __v) const {
1624 using __variant_detail::__visitation::__variant;
1625 size_t __res =
1626 __v.valueless_by_exception()
1627 ? 299792458 // Random value chosen by the universe upon creation
1628 : __variant::__visit_alt(
1629 [](const auto& __alt) {
1630 using __alt_type = __remove_cvref_t<decltype(__alt)>;
1631 using __value_type = remove_const_t< typename __alt_type::__value_type>;
1632 return hash<__value_type>{}(__alt.__value);
1633 },
1634 __v);
1635 return std::__hash_combine(lhs: __res, rhs: hash<size_t>{}(__v.index()));
1636 }
1637};
1638
1639// __unchecked_get is the same as std::get, except, it is UB to use it with the wrong
1640// type whereas std::get will throw or returning nullptr. This makes it faster than
1641// std::get.
1642template <size_t _Ip, class _Vp>
1643_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(_Vp&& __v) noexcept {
1644 using __variant_detail::__access::__variant;
1645 return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
1646}
1647
1648template <class _Tp, class... _Types>
1649_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept {
1650 return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1651}
1652
1653template <class _Tp, class... _Types>
1654_LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept {
1655 return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1656}
1657
1658_LIBCPP_END_NAMESPACE_STD
1659
1660# endif // _LIBCPP_STD_VER >= 17
1661
1662_LIBCPP_POP_MACROS
1663
1664# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 20
1665# include <cstddef>
1666# include <exception>
1667# include <tuple>
1668# include <type_traits>
1669# include <typeinfo>
1670# include <utility>
1671# endif
1672#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
1673
1674#endif // _LIBCPP_VARIANT
1675