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