2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_VARIANT
11 #define _LIBCPP_VARIANT
18 // 20.7.2, class template variant
19 template <class... Types>
23 // 20.7.2.1, constructors
24 constexpr variant() noexcept(see below);
25 constexpr variant(const variant&);
26 constexpr variant(variant&&) noexcept(see below);
28 template <class T> constexpr variant(T&&) noexcept(see below);
30 template <class T, class... Args>
31 constexpr explicit variant(in_place_type_t<T>, Args&&...);
33 template <class T, class U, class... Args>
34 constexpr explicit variant(
35 in_place_type_t<T>, initializer_list<U>, Args&&...);
37 template <size_t I, class... Args>
38 constexpr explicit variant(in_place_index_t<I>, Args&&...);
40 template <size_t I, class U, class... Args>
41 constexpr explicit variant(
42 in_place_index_t<I>, initializer_list<U>, Args&&...);
44 // 20.7.2.2, destructor
45 constexpr ~variant(); // constexpr since c++20
47 // 20.7.2.3, assignment
48 constexpr variant& operator=(const variant&);
49 constexpr variant& operator=(variant&&) noexcept(see below);
52 constexpr variant& operator=(T&&) noexcept(see below); // constexpr since c++20
54 // 20.7.2.4, modifiers
55 template <class T, class... Args>
56 constexpr T& emplace(Args&&...); // constexpr since c++20
58 template <class T, class U, class... Args>
59 constexpr T& emplace(initializer_list<U>, Args&&...); // constexpr since c++20
61 template <size_t I, class... Args>
62 constexpr variant_alternative_t<I, variant>& emplace(Args&&...); // constexpr since c++20
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
68 // 20.7.2.5, value status
69 constexpr bool valueless_by_exception() const noexcept;
70 constexpr size_t index() const noexcept;
73 void swap(variant&) noexcept(see below);
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
82 // 20.7.3, variant helper classes
83 template <class T> struct variant_size; // undefined
86 inline constexpr size_t variant_size_v = variant_size<T>::value;
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>;
92 template <class... Types>
93 struct variant_size<variant<Types...>>;
95 template <size_t I, class T> struct variant_alternative; // undefined
97 template <size_t I, class T>
98 using variant_alternative_t = typename variant_alternative<I, T>::type;
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>;
104 template <size_t I, class... Types>
105 struct variant_alternative<I, variant<Types...>>;
107 inline constexpr size_t variant_npos = -1;
109 // 20.7.4, value access
110 template <class T, class... Types>
111 constexpr bool holds_alternative(const variant<Types...>&) noexcept;
113 template <size_t I, class... Types>
114 constexpr variant_alternative_t<I, variant<Types...>>&
115 get(variant<Types...>&);
117 template <size_t I, class... Types>
118 constexpr variant_alternative_t<I, variant<Types...>>&&
119 get(variant<Types...>&&);
121 template <size_t I, class... Types>
122 constexpr variant_alternative_t<I, variant<Types...>> const&
123 get(const variant<Types...>&);
125 template <size_t I, class... Types>
126 constexpr variant_alternative_t<I, variant<Types...>> const&&
127 get(const variant<Types...>&&);
129 template <class T, class... Types>
130 constexpr T& get(variant<Types...>&);
132 template <class T, class... Types>
133 constexpr T&& get(variant<Types...>&&);
135 template <class T, class... Types>
136 constexpr const T& get(const variant<Types...>&);
138 template <class T, class... Types>
139 constexpr const T&& get(const variant<Types...>&&);
141 template <size_t I, class... Types>
142 constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
143 get_if(variant<Types...>*) noexcept;
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;
149 template <class T, class... Types>
150 constexpr add_pointer_t<T>
151 get_if(variant<Types...>*) noexcept;
153 template <class T, class... Types>
154 constexpr add_pointer_t<const T>
155 get_if(const variant<Types...>*) noexcept;
157 // 20.7.5, relational operators
158 template <class... Types>
159 constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
161 template <class... Types>
162 constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
164 template <class... Types>
165 constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
167 template <class... Types>
168 constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
170 template <class... Types>
171 constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
173 template <class... Types>
174 constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
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
180 // 20.7.6, visitation
181 template <class Visitor, class... Variants>
182 constexpr see below visit(Visitor&&, Variants&&...);
184 template <class R, class Visitor, class... Variants>
185 constexpr R visit(Visitor&&, Variants&&...); // since C++20
187 // 20.7.7, class monostate
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
199 // 20.7.9, specialized algorithms
200 template <class... Types>
201 void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
203 // 20.7.10, class bad_variant_access
204 class bad_variant_access;
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>;
215 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
216 # include <__cxx03/variant>
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>
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/dependent_type.h>
239 # include <__type_traits/enable_if.h>
240 # include <__type_traits/invoke.h>
241 # include <__type_traits/is_array.h>
242 # include <__type_traits/is_assignable.h>
243 # include <__type_traits/is_constructible.h>
244 # include <__type_traits/is_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>
274 // standard-mandated includes
279 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
280 # pragma GCC system_header
284 # include <__undef_macros>
286 namespace std { // explicitly not using versioning namespace
288 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
290 const char* what() const _NOEXCEPT override;
295 _LIBCPP_BEGIN_NAMESPACE_STD
297 # if _LIBCPP_STD_VER >= 17
299 // Light N-dimensional array of function pointers. Used in place of std::array to avoid
300 // adding a dependency.
301 template <class _Tp, size_t _Size>
303 static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit");
304 _Tp __buf_[_Size] = {};
306 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator[](size_t __n) const noexcept { return __buf_[__n]; }
309 [[noreturn]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS void
310 __throw_bad_variant_access() {
311 # if _LIBCPP_HAS_EXCEPTIONS
312 throw bad_variant_access();
314 _LIBCPP_VERBOSE_ABORT("bad_variant_access was thrown in -fno-exceptions mode");
320 struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
323 struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
326 struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp> : variant_size<_Tp> {};
328 template <class... _Types>
329 struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>> : integral_constant<size_t, sizeof...(_Types)> {};
331 // variant_alternative
332 template <size_t _Ip, class _Tp>
333 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp> : add_const<variant_alternative_t<_Ip, _Tp>> {};
335 template <size_t _Ip, class _Tp>
336 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp> : add_volatile<variant_alternative_t<_Ip, _Tp>> {};
338 template <size_t _Ip, class _Tp>
339 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp> : add_cv<variant_alternative_t<_Ip, _Tp>> {};
341 template <size_t _Ip, class... _Types>
342 struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
343 static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
344 using type = __type_pack_element<_Ip, _Types...>;
347 template <size_t _NumAlternatives>
348 _LIBCPP_HIDE_FROM_ABI constexpr auto __choose_index_type() {
349 # ifdef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
350 if constexpr (_NumAlternatives < numeric_limits<unsigned char>::max())
351 return static_cast<unsigned char>(0);
352 else if constexpr (_NumAlternatives < numeric_limits<unsigned short>::max())
353 return static_cast<unsigned short>(0);
355 # endif // _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
356 return static_cast<unsigned int>(0);
359 template <size_t _NumAlts>
360 using __variant_index_t = decltype(std::__choose_index_type<_NumAlts>());
362 template <class _IndexType>
363 constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
365 template <class... _Types>
366 _LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>& __as_variant(variant<_Types...>& __vs) noexcept {
370 template <class... _Types>
371 _LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>& __as_variant(const variant<_Types...>& __vs) noexcept {
375 template <class... _Types>
376 _LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>&& __as_variant(variant<_Types...>&& __vs) noexcept {
377 return std::move(__vs);
380 template <class... _Types>
381 _LIBCPP_HIDE_FROM_ABI constexpr const variant<_Types...>&& __as_variant(const variant<_Types...>&& __vs) noexcept {
382 return std::move(__vs);
385 namespace __find_detail {
387 template <class _Tp, class... _Types>
388 _LIBCPP_HIDE_FROM_ABI constexpr size_t __find_index() {
389 constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
390 size_t __result = __not_found;
391 for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
392 if (__matches[__i]) {
393 if (__result != __not_found) {
402 template <size_t _Index>
403 struct __find_unambiguous_index_sfinae_impl : integral_constant<size_t, _Index> {};
406 struct __find_unambiguous_index_sfinae_impl<__not_found> {};
409 struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
411 template <class _Tp, class... _Types>
412 struct __find_unambiguous_index_sfinae : __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
414 } // namespace __find_detail
416 namespace __variant_detail {
418 struct __valueless_t {};
420 enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
422 template <typename _Tp, template <typename> class _IsTriviallyAvailable, template <typename> class _IsAvailable>
423 constexpr _Trait __trait =
424 _IsTriviallyAvailable<_Tp>::value ? _Trait::_TriviallyAvailable
425 : _IsAvailable<_Tp>::value
427 : _Trait::_Unavailable;
429 _LIBCPP_HIDE_FROM_ABI constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
430 _Trait __result = _Trait::_TriviallyAvailable;
431 for (_Trait __t : __traits) {
432 if (static_cast<int>(__t) > static_cast<int>(__result)) {
439 template <typename... _Types>
441 static constexpr _Trait __copy_constructible_trait =
442 __variant_detail::__common_trait({__trait<_Types, is_trivially_copy_constructible, is_copy_constructible>...});
444 static constexpr _Trait __move_constructible_trait =
445 __variant_detail::__common_trait({__trait<_Types, is_trivially_move_constructible, is_move_constructible>...});
447 static constexpr _Trait __copy_assignable_trait = __variant_detail::__common_trait(
448 {__copy_constructible_trait, __trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
450 static constexpr _Trait __move_assignable_trait = __variant_detail::__common_trait(
451 {__move_constructible_trait, __trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
453 static constexpr _Trait __destructible_trait =
454 __variant_detail::__common_trait({__trait<_Types, is_trivially_destructible, is_destructible>...});
461 _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
462 return std::forward<_Vp>(__v).__head;
465 template <class _Vp, size_t _Ip>
466 _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
467 return __get_alt(std::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
472 template <size_t _Ip, class _Vp>
473 _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) {
474 return __union::__get_alt(std::forward<_Vp>(__v).__data, in_place_index<_Ip>);
479 template <size_t _Ip, class _Vp>
480 _LIBCPP_HIDE_FROM_ABI static constexpr auto&& __get_alt(_Vp&& __v) {
481 return __base::__get_alt<_Ip>(std::forward<_Vp>(__v).__impl_);
485 } // namespace __access
487 namespace __visitation {
490 template <class _Visitor, class... _Vs>
491 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
492 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
493 constexpr auto __fdiagonal = __make_fdiagonal<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
494 return __fdiagonal[__index](std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...);
497 template <class _Visitor, class... _Vs>
498 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) {
499 constexpr auto __fmatrix = __make_fmatrix<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
500 return __at(__fmatrix, __vs.index()...)(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__as_base()...);
505 _LIBCPP_HIDE_FROM_ABI static constexpr const _Tp& __at(const _Tp& __elem) {
509 template <class _Tp, size_t _Np, typename... _Indices>
510 _LIBCPP_HIDE_FROM_ABI static constexpr auto&&
511 __at(const __farray<_Tp, _Np>& __elems, size_t __index, _Indices... __indices) {
512 return __at(__elems[__index], __indices...);
515 template <class _Fp, class... _Fs>
516 static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_visitor_return_type_check() {
518 __all<is_same_v<_Fp, _Fs>...>::value, "`std::visit` requires the visitor to have a single return type.");
521 template <class... _Fs>
522 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_farray(_Fs&&... __fs) {
523 __std_visit_visitor_return_type_check<__remove_cvref_t<_Fs>...>();
524 using __result = __farray<common_type_t<__remove_cvref_t<_Fs>...>, sizeof...(_Fs)>;
525 return __result{{std::forward<_Fs>(__fs)...}};
528 template <size_t... _Is>
529 struct __dispatcher {
530 template <class _Fp, class... _Vs>
531 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
532 return std::__invoke(static_cast<_Fp>(__f), __access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
536 template <class _Fp, class... _Vs, size_t... _Is>
537 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_dispatch(index_sequence<_Is...>) {
538 return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
541 template <size_t _Ip, class _Fp, class... _Vs>
542 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl() {
543 return __make_dispatch<_Fp, _Vs...>(index_sequence<((void)__type_identity<_Vs>{}, _Ip)...>{});
546 template <class _Fp, class... _Vs, size_t... _Is>
547 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
548 return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
551 template <class _Fp, class _Vp, class... _Vs>
552 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fdiagonal() {
553 constexpr size_t __np = __remove_cvref_t<_Vp>::__size();
554 static_assert(__all<(__np == __remove_cvref_t<_Vs>::__size())...>::value);
555 return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<__np>{});
558 template <class _Fp, class... _Vs, size_t... _Is>
559 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
560 return __make_dispatch<_Fp, _Vs...>(__is);
563 template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
564 _LIBCPP_HIDE_FROM_ABI static constexpr auto
565 __make_fmatrix_impl(index_sequence<_Is...>, index_sequence<_Js...>, _Ls... __ls) {
566 return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(index_sequence<_Is..., _Js>{}, __ls...)...);
569 template <class _Fp, class... _Vs>
570 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_fmatrix() {
571 return __make_fmatrix_impl<_Fp, _Vs...>(
572 index_sequence<>{}, make_index_sequence<__remove_cvref_t<_Vs>::__size()>{}...);
577 template <class _Visitor, class... _Vs>
578 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
579 __visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
580 return __base::__visit_alt_at(__index, std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs).__impl_...);
583 template <class _Visitor, class... _Vs>
584 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor, _Vs&&... __vs) {
585 return __base::__visit_alt(
586 std::forward<_Visitor>(__visitor), std::__as_variant(std::forward<_Vs>(__vs)).__impl_...);
589 template <class _Visitor, class... _Vs>
590 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
591 __visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
592 return __visit_alt_at(__index, __make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
595 template <class _Visitor, class... _Vs>
596 _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __visit_value(_Visitor&& __visitor, _Vs&&... __vs) {
597 return __visit_alt(__make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
600 # if _LIBCPP_STD_VER >= 20
601 template <class _Rp, class _Visitor, class... _Vs>
602 _LIBCPP_HIDE_FROM_ABI static constexpr _Rp __visit_value(_Visitor&& __visitor, _Vs&&... __vs) {
603 return __visit_alt(__make_value_visitor<_Rp>(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
608 template <class _Visitor, class... _Values>
609 static _LIBCPP_HIDE_FROM_ABI constexpr void __std_visit_exhaustive_visitor_check() {
610 static_assert(is_invocable_v<_Visitor, _Values...>, "`std::visit` requires the visitor to be exhaustive.");
613 template <class _Visitor>
614 struct __value_visitor {
615 template <class... _Alts>
616 _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Alts&&... __alts) const {
617 __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>();
618 return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
620 _Visitor&& __visitor;
623 # if _LIBCPP_STD_VER >= 20
624 template <class _Rp, class _Visitor>
625 struct __value_visitor_return_type {
626 template <class... _Alts>
627 _LIBCPP_HIDE_FROM_ABI constexpr _Rp operator()(_Alts&&... __alts) const {
628 __std_visit_exhaustive_visitor_check< _Visitor, decltype((std::forward<_Alts>(__alts).__value))...>();
629 if constexpr (is_void_v<_Rp>) {
630 std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
632 return std::__invoke(std::forward<_Visitor>(__visitor), std::forward<_Alts>(__alts).__value...);
636 _Visitor&& __visitor;
640 template <class _Visitor>
641 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
642 return __value_visitor<_Visitor>{std::forward<_Visitor>(__visitor)};
645 # if _LIBCPP_STD_VER >= 20
646 template <class _Rp, class _Visitor>
647 _LIBCPP_HIDE_FROM_ABI static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
648 return __value_visitor_return_type<_Rp, _Visitor>{std::forward<_Visitor>(__visitor)};
653 } // namespace __visitation
655 // Adding semi-colons in macro expansions helps clang-format to do a better job.
656 // This macro is used to avoid compilation errors due to "stray" semi-colons.
657 # define _LIBCPP_EAT_SEMICOLON static_assert(true, "")
659 template <size_t _Index, class _Tp>
660 struct _LIBCPP_TEMPLATE_VIS __alt {
661 using __value_type = _Tp;
662 static constexpr size_t __index = _Index;
664 template <class... _Args>
665 _LIBCPP_HIDE_FROM_ABI explicit constexpr __alt(in_place_t, _Args&&... __args)
666 : __value(std::forward<_Args>(__args)...) {}
668 __value_type __value;
671 template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
672 union _LIBCPP_TEMPLATE_VIS __union;
674 template <_Trait _DestructibleTrait, size_t _Index>
675 union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
677 # define _LIBCPP_VARIANT_UNION(destructible_trait, destructor_definition) \
678 template <size_t _Index, class _Tp, class... _Types> \
679 union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, _Index, _Tp, _Types...> { \
681 _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
683 template <class... _Args> \
684 _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
685 : __head(in_place, std::forward<_Args>(__args)...) {} \
687 template <size_t _Ip, class... _Args> \
688 _LIBCPP_HIDE_FROM_ABI explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
689 : __tail(in_place_index<_Ip - 1>, std::forward<_Args>(__args)...) {} \
691 _LIBCPP_HIDE_FROM_ABI __union(const __union&) = default; \
692 _LIBCPP_HIDE_FROM_ABI __union(__union&&) = default; \
693 _LIBCPP_HIDE_FROM_ABI __union& operator=(const __union&) = default; \
694 _LIBCPP_HIDE_FROM_ABI __union& operator=(__union&&) = default; \
695 destructor_definition; \
699 __alt<_Index, _Tp> __head; \
700 __union<destructible_trait, _Index + 1, _Types...> __tail; \
702 friend struct __access::__union; \
705 _LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable,
706 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() = default);
707 _LIBCPP_VARIANT_UNION(
708 _Trait::_Available, _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() {} _LIBCPP_EAT_SEMICOLON);
709 _LIBCPP_VARIANT_UNION(_Trait::_Unavailable, _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__union() = delete);
711 # undef _LIBCPP_VARIANT_UNION
713 template <_Trait _DestructibleTrait, class... _Types>
714 class _LIBCPP_TEMPLATE_VIS __base {
716 using __index_t = __variant_index_t<sizeof...(_Types)>;
718 _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(__valueless_t __tag) noexcept
719 : __data(__tag), __index(__variant_npos<__index_t>) {}
721 template <size_t _Ip, class... _Args>
722 _LIBCPP_HIDE_FROM_ABI explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
723 : __data(in_place_index<_Ip>, std::forward<_Args>(__args)...), __index(_Ip) {}
725 _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept { return index() == variant_npos; }
727 _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept {
728 return __index == __variant_npos<__index_t> ? variant_npos : __index;
732 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() & { return *this; }
734 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() && { return std::move(*this); }
736 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const& { return *this; }
738 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __as_base() const&& { return std::move(*this); }
740 _LIBCPP_HIDE_FROM_ABI static constexpr size_t __size() { return sizeof...(_Types); }
742 __union<_DestructibleTrait, 0, _Types...> __data;
745 friend struct __access::__base;
746 friend struct __visitation::__base;
749 template <class _Traits, _Trait = _Traits::__destructible_trait>
750 class _LIBCPP_TEMPLATE_VIS __dtor;
752 # define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor_definition, destroy) \
753 template <class... _Types> \
754 class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, destructible_trait> \
755 : public __base<destructible_trait, _Types...> { \
756 using __base_type = __base<destructible_trait, _Types...>; \
757 using __index_t = typename __base_type::__index_t; \
760 using __base_type::__base_type; \
761 using __base_type::operator=; \
762 _LIBCPP_HIDE_FROM_ABI __dtor(const __dtor&) = default; \
763 _LIBCPP_HIDE_FROM_ABI __dtor(__dtor&&) = default; \
764 _LIBCPP_HIDE_FROM_ABI __dtor& operator=(const __dtor&) = default; \
765 _LIBCPP_HIDE_FROM_ABI __dtor& operator=(__dtor&&) = default; \
766 destructor_definition; \
772 _LIBCPP_VARIANT_DESTRUCTOR(
773 _Trait::_TriviallyAvailable,
774 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() = default,
775 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept {
776 this->__index = __variant_npos<__index_t>;
777 } _LIBCPP_EAT_SEMICOLON);
779 _LIBCPP_VARIANT_DESTRUCTOR(
781 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() { __destroy(); } _LIBCPP_EAT_SEMICOLON,
782 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept {
783 if (!this->valueless_by_exception()) {
784 __visitation::__base::__visit_alt(
785 [](auto& __alt) noexcept {
786 using __alt_type = __remove_cvref_t<decltype(__alt)>;
791 this->__index = __variant_npos<__index_t>;
792 } _LIBCPP_EAT_SEMICOLON);
794 _LIBCPP_VARIANT_DESTRUCTOR(_Trait::_Unavailable,
795 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() = delete,
796 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept = delete);
798 # undef _LIBCPP_VARIANT_DESTRUCTOR
800 template <class _Traits>
801 class _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
802 using __base_type = __dtor<_Traits>;
805 using __base_type::__base_type;
806 using __base_type::operator=;
809 template <class _Rhs>
810 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
812 if (!__rhs.valueless_by_exception()) {
813 auto __rhs_index = __rhs.index();
814 __visitation::__base::__visit_alt_at(
816 [&__lhs](auto&& __rhs_alt) {
817 std::__construct_at(std::addressof(__lhs.__data),
818 in_place_index<__decay_t<decltype(__rhs_alt)>::__index>,
819 std::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
821 std::forward<_Rhs>(__rhs));
822 __lhs.__index = __rhs_index;
827 template <class _Traits, _Trait = _Traits::__move_constructible_trait>
828 class _LIBCPP_TEMPLATE_VIS __move_constructor;
830 # define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, move_constructor_definition) \
831 template <class... _Types> \
832 class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, move_constructible_trait> \
833 : public __ctor<__traits<_Types...>> { \
834 using __base_type = __ctor<__traits<_Types...>>; \
837 using __base_type::__base_type; \
838 using __base_type::operator=; \
840 _LIBCPP_HIDE_FROM_ABI __move_constructor(const __move_constructor&) = default; \
841 _LIBCPP_HIDE_FROM_ABI ~__move_constructor() = default; \
842 _LIBCPP_HIDE_FROM_ABI __move_constructor& operator=(const __move_constructor&) = default; \
843 _LIBCPP_HIDE_FROM_ABI __move_constructor& operator=(__move_constructor&&) = default; \
844 move_constructor_definition; \
847 _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
848 _Trait::_TriviallyAvailable,
849 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&& __that) = default);
851 _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
853 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&& __that) noexcept(
854 __all<is_nothrow_move_constructible_v<_Types>...>::value)
855 : __move_constructor(__valueless_t{}) {
856 this->__generic_construct(*this, std::move(__that));
857 } _LIBCPP_EAT_SEMICOLON);
859 _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
860 _Trait::_Unavailable,
861 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_constructor(__move_constructor&&) = delete);
863 # undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
865 template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
866 class _LIBCPP_TEMPLATE_VIS __copy_constructor;
868 # define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, copy_constructor_definition) \
869 template <class... _Types> \
870 class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, copy_constructible_trait> \
871 : public __move_constructor<__traits<_Types...>> { \
872 using __base_type = __move_constructor<__traits<_Types...>>; \
875 using __base_type::__base_type; \
876 using __base_type::operator=; \
878 _LIBCPP_HIDE_FROM_ABI __copy_constructor(__copy_constructor&&) = default; \
879 _LIBCPP_HIDE_FROM_ABI ~__copy_constructor() = default; \
880 _LIBCPP_HIDE_FROM_ABI __copy_constructor& operator=(const __copy_constructor&) = default; \
881 _LIBCPP_HIDE_FROM_ABI __copy_constructor& operator=(__copy_constructor&&) = default; \
882 copy_constructor_definition; \
885 _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
886 _Trait::_TriviallyAvailable,
887 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor& __that) = default);
889 _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
891 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor& __that)
892 : __copy_constructor(__valueless_t{}) { this->__generic_construct(*this, __that); } _LIBCPP_EAT_SEMICOLON);
894 _LIBCPP_VARIANT_COPY_CONSTRUCTOR(
895 _Trait::_Unavailable,
896 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_constructor(const __copy_constructor&) = delete);
898 # undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
900 template <class _Traits>
901 class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
902 using __base_type = __copy_constructor<_Traits>;
905 using __base_type::__base_type;
906 using __base_type::operator=;
908 template <size_t _Ip, class... _Args>
909 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto& __emplace(_Args&&... __args) {
911 std::__construct_at(std::addressof(this->__data), in_place_index<_Ip>, std::forward<_Args>(__args)...);
913 return __access::__base::__get_alt<_Ip>(*this).__value;
917 template <size_t _Ip, class _Tp, class _Arg>
918 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
919 if (this->index() == _Ip) {
920 __a.__value = std::forward<_Arg>(__arg);
923 _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()(true_type) const {
924 __this->__emplace<_Ip>(std::forward<_Arg>(__arg));
926 _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()(false_type) const {
927 __this->__emplace<_Ip>(_Tp(std::forward<_Arg>(__arg)));
929 __assignment* __this;
931 } __impl{this, std::forward<_Arg>(__arg)};
932 __impl(bool_constant < is_nothrow_constructible_v<_Tp, _Arg> || !is_nothrow_move_constructible_v < _Tp >> {});
936 template <class _That>
937 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __generic_assign(_That&& __that) {
938 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
940 } else if (__that.valueless_by_exception()) {
943 __visitation::__base::__visit_alt_at(
945 [this](auto& __this_alt, auto&& __that_alt) {
946 this->__assign_alt(__this_alt, std::forward<decltype(__that_alt)>(__that_alt).__value);
949 std::forward<_That>(__that));
954 template <class _Traits, _Trait = _Traits::__move_assignable_trait>
955 class _LIBCPP_TEMPLATE_VIS __move_assignment;
957 # define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, move_assignment_definition) \
958 template <class... _Types> \
959 class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, move_assignable_trait> \
960 : public __assignment<__traits<_Types...>> { \
961 using __base_type = __assignment<__traits<_Types...>>; \
964 using __base_type::__base_type; \
965 using __base_type::operator=; \
967 _LIBCPP_HIDE_FROM_ABI __move_assignment(const __move_assignment&) = default; \
968 _LIBCPP_HIDE_FROM_ABI __move_assignment(__move_assignment&&) = default; \
969 _LIBCPP_HIDE_FROM_ABI ~__move_assignment() = default; \
970 _LIBCPP_HIDE_FROM_ABI __move_assignment& operator=(const __move_assignment&) = default; \
971 move_assignment_definition; \
974 _LIBCPP_VARIANT_MOVE_ASSIGNMENT(_Trait::_TriviallyAvailable,
975 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment& operator=(
976 __move_assignment&& __that) = default);
978 _LIBCPP_VARIANT_MOVE_ASSIGNMENT(
980 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment&
981 operator=(__move_assignment&& __that) noexcept(
982 __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_move_assignable_v<_Types>)...>::value) {
983 this->__generic_assign(std::move(__that));
985 } _LIBCPP_EAT_SEMICOLON);
987 _LIBCPP_VARIANT_MOVE_ASSIGNMENT(
988 _Trait::_Unavailable,
989 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __move_assignment& operator=(__move_assignment&&) = delete);
991 # undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
993 template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
994 class _LIBCPP_TEMPLATE_VIS __copy_assignment;
996 # define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, copy_assignment_definition) \
997 template <class... _Types> \
998 class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, copy_assignable_trait> \
999 : public __move_assignment<__traits<_Types...>> { \
1000 using __base_type = __move_assignment<__traits<_Types...>>; \
1003 using __base_type::__base_type; \
1004 using __base_type::operator=; \
1006 _LIBCPP_HIDE_FROM_ABI __copy_assignment(const __copy_assignment&) = default; \
1007 _LIBCPP_HIDE_FROM_ABI __copy_assignment(__copy_assignment&&) = default; \
1008 _LIBCPP_HIDE_FROM_ABI ~__copy_assignment() = default; \
1009 _LIBCPP_HIDE_FROM_ABI __copy_assignment& operator=(__copy_assignment&&) = default; \
1010 copy_assignment_definition; \
1013 _LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_TriviallyAvailable,
1014 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment& operator=(
1015 const __copy_assignment& __that) = default);
1017 _LIBCPP_VARIANT_COPY_ASSIGNMENT(
1019 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment&
1020 operator=(const __copy_assignment& __that) {
1021 this->__generic_assign(__that);
1023 } _LIBCPP_EAT_SEMICOLON);
1025 _LIBCPP_VARIANT_COPY_ASSIGNMENT(_Trait::_Unavailable,
1026 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __copy_assignment& operator=(
1027 const __copy_assignment&) = delete);
1029 # undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
1031 template <class... _Types>
1032 class _LIBCPP_TEMPLATE_VIS __impl : public __copy_assignment<__traits<_Types...>> {
1033 using __base_type = __copy_assignment<__traits<_Types...>>;
1036 using __base_type::__base_type; // get in_place_index_t constructor & friends
1037 _LIBCPP_HIDE_FROM_ABI __impl(__impl const&) = default;
1038 _LIBCPP_HIDE_FROM_ABI __impl(__impl&&) = default;
1039 _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl const&) = default;
1040 _LIBCPP_HIDE_FROM_ABI __impl& operator=(__impl&&) = default;
1042 template <size_t _Ip, class _Arg>
1043 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign(_Arg&& __arg) {
1044 this->__assign_alt(__access::__base::__get_alt<_Ip>(*this), std::forward<_Arg>(__arg));
1047 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __swap(__impl& __that) {
1048 if (this->valueless_by_exception() && __that.valueless_by_exception()) {
1050 } else if (this->index() == __that.index()) {
1051 __visitation::__base::__visit_alt_at(
1053 [](auto& __this_alt, auto& __that_alt) {
1055 swap(__this_alt.__value, __that_alt.__value);
1060 __impl* __lhs = this;
1061 __impl* __rhs = std::addressof(__that);
1062 if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
1063 std::swap(__lhs, __rhs);
1065 __impl __tmp(std::move(*__rhs));
1066 # if _LIBCPP_HAS_EXCEPTIONS
1067 if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
1068 this->__generic_construct(*__rhs, std::move(*__lhs));
1070 // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
1071 // and `__tmp` is nothrow move constructible then we move `__tmp` back
1072 // into `__rhs` and provide the strong exception safety guarantee.
1074 this->__generic_construct(*__rhs, std::move(*__lhs));
1076 if (__tmp.__move_nothrow()) {
1077 this->__generic_construct(*__rhs, std::move(__tmp));
1083 // this isn't consolidated with the `if constexpr` branch above due to
1084 // `throw` being ill-formed with exceptions disabled even when discarded.
1085 this->__generic_construct(*__rhs, std::move(*__lhs));
1087 this->__generic_construct(*__lhs, std::move(__tmp));
1092 constexpr inline _LIBCPP_HIDE_FROM_ABI bool __move_nothrow() const {
1093 constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
1094 return this->valueless_by_exception() || __results[this->index()];
1098 struct __no_narrowing_check {
1099 template <class _Dest, class _Source>
1100 using _Apply = __type_identity<_Dest>;
1103 struct __narrowing_check {
1104 template <class _Dest>
1105 static auto __test_impl(_Dest (&&)[1]) -> __type_identity<_Dest>;
1106 template <class _Dest, class _Source>
1107 using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({std::declval<_Source>()}));
1110 template <class _Dest, class _Source>
1111 using __check_for_narrowing _LIBCPP_NODEBUG =
1112 typename _If< is_arithmetic<_Dest>::value, __narrowing_check, __no_narrowing_check >::template _Apply<_Dest,
1115 template <class _Tp, size_t _Idx>
1117 template <class _Up>
1118 auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
1121 template <class... _Bases>
1122 struct __all_overloads : _Bases... {
1123 void operator()() const;
1124 using _Bases::operator()...;
1127 template <class _IdxSeq>
1128 struct __make_overloads_imp;
1130 template <size_t... _Idx>
1131 struct __make_overloads_imp<__tuple_indices<_Idx...> > {
1132 template <class... _Types>
1133 using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>;
1136 template <class... _Types>
1137 using _MakeOverloads _LIBCPP_NODEBUG =
1138 typename __make_overloads_imp< __make_indices_imp<sizeof...(_Types), 0> >::template _Apply<_Types...>;
1140 template <class _Tp, class... _Types>
1141 using __best_match_t = typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
1143 } // namespace __variant_detail
1145 template <class _Visitor, class... _Vs, typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>>
1146 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr decltype(auto)
1147 visit(_Visitor&& __visitor, _Vs&&... __vs);
1149 # if _LIBCPP_STD_VER >= 20
1150 template <class _Rp,
1153 typename = void_t<decltype(std::__as_variant(std::declval<_Vs>()))...>>
1154 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp
1155 visit(_Visitor&& __visitor, _Vs&&... __vs);
1158 template <class... _Types>
1159 class _LIBCPP_TEMPLATE_VIS _LIBCPP_DECLSPEC_EMPTY_BASES variant
1160 : private __sfinae_ctor_base< __all<is_copy_constructible_v<_Types>...>::value,
1161 __all<is_move_constructible_v<_Types>...>::value>,
1162 private __sfinae_assign_base<
1163 __all<(is_copy_constructible_v<_Types> && is_copy_assignable_v<_Types>)...>::value,
1164 __all<(is_move_constructible_v<_Types> && is_move_assignable_v<_Types>)...>::value> {
1165 static_assert(0 < sizeof...(_Types), "variant must consist of at least one alternative.");
1167 static_assert(__all<!is_array_v<_Types>...>::value, "variant can not have an array type as an alternative.");
1169 static_assert(__all<!is_reference_v<_Types>...>::value, "variant can not have a reference type as an alternative.");
1171 static_assert(__all<!is_void_v<_Types>...>::value, "variant can not have a void type as an alternative.");
1173 using __first_type = variant_alternative_t<0, variant>;
1176 using __trivially_relocatable =
1177 conditional_t<_And<__libcpp_is_trivially_relocatable<_Types>...>::value, variant, void>;
1179 template <bool _Dummy = true,
1180 enable_if_t<__dependent_type<is_default_constructible<__first_type>, _Dummy>::value, int> = 0>
1181 _LIBCPP_HIDE_FROM_ABI constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
1182 : __impl_(in_place_index<0>) {}
1184 _LIBCPP_HIDE_FROM_ABI constexpr variant(const variant&) = default;
1185 _LIBCPP_HIDE_FROM_ABI constexpr variant(variant&&) = default;
1187 template < class _Arg,
1188 enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1189 enable_if_t<!__is_inplace_type<__remove_cvref_t<_Arg>>::value, int> = 0,
1190 enable_if_t<!__is_inplace_index<__remove_cvref_t<_Arg>>::value, int> = 0,
1191 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1192 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1193 enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
1194 _LIBCPP_HIDE_FROM_ABI constexpr variant(_Arg&& __arg) noexcept(is_nothrow_constructible_v<_Tp, _Arg>)
1195 : __impl_(in_place_index<_Ip>, std::forward<_Arg>(__arg)) {}
1197 template <size_t _Ip,
1199 class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
1200 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1201 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1202 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_index_t<_Ip>, _Args&&... __args) noexcept(
1203 is_nothrow_constructible_v<_Tp, _Args...>)
1204 : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
1206 template < size_t _Ip,
1209 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1210 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1211 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1212 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
1213 in_place_index_t<_Ip>,
1214 initializer_list<_Up> __il,
1215 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1216 : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
1218 template < class _Tp,
1220 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1221 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1222 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
1223 is_nothrow_constructible_v<_Tp, _Args...>)
1224 : __impl_(in_place_index<_Ip>, std::forward<_Args>(__args)...) {}
1226 template < class _Tp,
1229 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1230 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1231 _LIBCPP_HIDE_FROM_ABI explicit constexpr variant(
1232 in_place_type_t<_Tp>,
1233 initializer_list<_Up> __il,
1234 _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
1235 : __impl_(in_place_index<_Ip>, __il, std::forward<_Args>(__args)...) {}
1237 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~variant() = default;
1239 _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(const variant&) = default;
1240 _LIBCPP_HIDE_FROM_ABI constexpr variant& operator=(variant&&) = default;
1242 template < class _Arg,
1243 enable_if_t<!is_same_v<__remove_cvref_t<_Arg>, variant>, int> = 0,
1244 class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
1245 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1246 enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>, int> = 0>
1247 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 variant&
1248 operator=(_Arg&& __arg) noexcept(is_nothrow_assignable_v<_Tp&, _Arg> && is_nothrow_constructible_v<_Tp, _Arg>) {
1249 __impl_.template __assign<_Ip>(std::forward<_Arg>(__arg));
1253 template < size_t _Ip,
1255 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1256 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1257 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1258 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
1259 return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
1262 template < size_t _Ip,
1265 enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
1266 class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
1267 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1268 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1269 return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
1272 template < class _Tp,
1274 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1275 enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
1276 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
1277 return __impl_.template __emplace<_Ip>(std::forward<_Args>(__args)...);
1280 template < class _Tp,
1283 size_t _Ip = __find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
1284 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
1285 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
1286 return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
1289 _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept {
1290 return __impl_.valueless_by_exception();
1293 _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { return __impl_.index(); }
1295 template < bool _Dummy = true,
1296 enable_if_t< __all<(__dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1297 __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
1299 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(variant& __that) noexcept(
1300 __all<(is_nothrow_move_constructible_v<_Types> && is_nothrow_swappable_v<_Types>)...>::value) {
1301 __impl_.__swap(__that.__impl_);
1304 # if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER
1305 // Helper class to implement [variant.visit]/10
1306 // Constraints: The call to visit does not use an explicit template-argument-list
1307 // that begins with a type template-argument.
1308 struct __variant_visit_barrier_tag {
1309 _LIBCPP_HIDE_FROM_ABI explicit __variant_visit_barrier_tag() = default;
1312 template <__variant_visit_barrier_tag = __variant_visit_barrier_tag{}, class _Self, class _Visitor>
1313 _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) visit(this _Self&& __self, _Visitor&& __visitor) {
1314 return std::visit(std::forward<_Visitor>(__visitor), std::__forward_as<_Self, variant>(__self));
1317 template <class _Rp, class _Self, class _Visitor>
1318 _LIBCPP_HIDE_FROM_ABI constexpr _Rp visit(this _Self&& __self, _Visitor&& __visitor) {
1319 return std::visit<_Rp>(std::forward<_Visitor>(__visitor), std::__forward_as<_Self, variant>(__self));
1324 __variant_detail::__impl<_Types...> __impl_;
1326 friend struct __variant_detail::__access::__variant;
1327 friend struct __variant_detail::__visitation::__variant;
1330 template <size_t _Ip, class... _Types>
1331 _LIBCPP_HIDE_FROM_ABI constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
1332 return __v.index() == _Ip;
1335 template <class _Tp, class... _Types>
1336 _LIBCPP_HIDE_FROM_ABI constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1337 return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1340 template <size_t _Ip, class _Vp>
1341 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr auto&& __generic_get(_Vp&& __v) {
1342 using __variant_detail::__access::__variant;
1343 if (!std::__holds_alternative<_Ip>(__v)) {
1344 __throw_bad_variant_access();
1346 return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
1349 template <size_t _Ip, class... _Types>
1350 _LIBCPP_HIDE_FROM_ABI
1351 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&
1352 get(variant<_Types...>& __v) {
1353 static_assert(_Ip < sizeof...(_Types));
1354 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1355 return std::__generic_get<_Ip>(__v);
1358 template <size_t _Ip, class... _Types>
1359 _LIBCPP_HIDE_FROM_ABI
1360 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&&
1361 get(variant<_Types...>&& __v) {
1362 static_assert(_Ip < sizeof...(_Types));
1363 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1364 return std::__generic_get<_Ip>(std::move(__v));
1367 template <size_t _Ip, class... _Types>
1368 _LIBCPP_HIDE_FROM_ABI
1369 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&
1370 get(const variant<_Types...>& __v) {
1371 static_assert(_Ip < sizeof...(_Types));
1372 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1373 return std::__generic_get<_Ip>(__v);
1376 template <size_t _Ip, class... _Types>
1377 _LIBCPP_HIDE_FROM_ABI
1378 _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&&
1379 get(const variant<_Types...>&& __v) {
1380 static_assert(_Ip < sizeof...(_Types));
1381 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1382 return std::__generic_get<_Ip>(std::move(__v));
1385 template <class _Tp, class... _Types>
1386 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp& get(variant<_Types...>& __v) {
1387 static_assert(!is_void_v<_Tp>);
1388 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1391 template <class _Tp, class... _Types>
1392 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp&& get(variant<_Types...>&& __v) {
1393 static_assert(!is_void_v<_Tp>);
1394 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
1397 template <class _Tp, class... _Types>
1398 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp&
1399 get(const variant<_Types...>& __v) {
1400 static_assert(!is_void_v<_Tp>);
1401 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1404 template <class _Tp, class... _Types>
1405 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp&&
1406 get(const variant<_Types...>&& __v) {
1407 static_assert(!is_void_v<_Tp>);
1408 return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
1411 template <size_t _Ip, class _Vp>
1412 _LIBCPP_HIDE_FROM_ABI constexpr auto* __generic_get_if(_Vp* __v) noexcept {
1413 using __variant_detail::__access::__variant;
1414 return __v && std::__holds_alternative<_Ip>(*__v) ? std::addressof(__variant::__get_alt<_Ip>(*__v).__value) : nullptr;
1417 template <size_t _Ip, class... _Types>
1418 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1419 get_if(variant<_Types...>* __v) noexcept {
1420 static_assert(_Ip < sizeof...(_Types));
1421 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1422 return std::__generic_get_if<_Ip>(__v);
1425 template <size_t _Ip, class... _Types>
1426 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1427 get_if(const variant<_Types...>* __v) noexcept {
1428 static_assert(_Ip < sizeof...(_Types));
1429 static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
1430 return std::__generic_get_if<_Ip>(__v);
1433 template <class _Tp, class... _Types>
1434 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> get_if(variant<_Types...>* __v) noexcept {
1435 static_assert(!is_void_v<_Tp>);
1436 return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1439 template <class _Tp, class... _Types>
1440 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const _Tp> get_if(const variant<_Types...>* __v) noexcept {
1441 static_assert(!is_void_v<_Tp>);
1442 return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1445 template <class _Operator>
1446 struct __convert_to_bool {
1447 template <class _T1, class _T2>
1448 _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_T1&& __t1, _T2&& __t2) const {
1449 static_assert(is_convertible<decltype(_Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2))), bool>::value,
1450 "the relational operator does not return a type which is implicitly convertible to bool");
1451 return _Operator{}(std::forward<_T1>(__t1), std::forward<_T2>(__t2));
1455 template <class... _Types>
1456 _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1457 using __variant_detail::__visitation::__variant;
1458 if (__lhs.index() != __rhs.index())
1460 if (__lhs.valueless_by_exception())
1462 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
1465 # if _LIBCPP_STD_VER >= 20
1467 template <class... _Types>
1468 requires(three_way_comparable<_Types> && ...)
1469 _LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...>
1470 operator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1471 using __variant_detail::__visitation::__variant;
1472 using __result_t = common_comparison_category_t<compare_three_way_result_t<_Types>...>;
1473 if (__lhs.valueless_by_exception() && __rhs.valueless_by_exception())
1474 return strong_ordering::equal;
1475 if (__lhs.valueless_by_exception())
1476 return strong_ordering::less;
1477 if (__rhs.valueless_by_exception())
1478 return strong_ordering::greater;
1479 if (auto __c = __lhs.index() <=> __rhs.index(); __c != 0)
1481 auto __three_way = []<class _Type>(const _Type& __v, const _Type& __w) -> __result_t { return __v <=> __w; };
1482 return __variant::__visit_value_at(__lhs.index(), __three_way, __lhs, __rhs);
1485 # endif // _LIBCPP_STD_VER >= 20
1487 template <class... _Types>
1488 _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1489 using __variant_detail::__visitation::__variant;
1490 if (__lhs.index() != __rhs.index())
1492 if (__lhs.valueless_by_exception())
1494 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
1497 template <class... _Types>
1498 _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1499 using __variant_detail::__visitation::__variant;
1500 if (__rhs.valueless_by_exception())
1502 if (__lhs.valueless_by_exception())
1504 if (__lhs.index() < __rhs.index())
1506 if (__lhs.index() > __rhs.index())
1508 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
1511 template <class... _Types>
1512 _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1513 using __variant_detail::__visitation::__variant;
1514 if (__lhs.valueless_by_exception())
1516 if (__rhs.valueless_by_exception())
1518 if (__lhs.index() > __rhs.index())
1520 if (__lhs.index() < __rhs.index())
1522 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
1525 template <class... _Types>
1526 _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1527 using __variant_detail::__visitation::__variant;
1528 if (__lhs.valueless_by_exception())
1530 if (__rhs.valueless_by_exception())
1532 if (__lhs.index() < __rhs.index())
1534 if (__lhs.index() > __rhs.index())
1536 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
1539 template <class... _Types>
1540 _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
1541 using __variant_detail::__visitation::__variant;
1542 if (__rhs.valueless_by_exception())
1544 if (__lhs.valueless_by_exception())
1546 if (__lhs.index() > __rhs.index())
1548 if (__lhs.index() < __rhs.index())
1550 return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
1553 template <class... _Vs>
1554 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr void __throw_if_valueless(_Vs&&... __vs) {
1555 const bool __valueless = (... || std::__as_variant(__vs).valueless_by_exception());
1557 __throw_bad_variant_access();
1561 template < class _Visitor, class... _Vs, typename>
1562 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr decltype(auto)
1563 visit(_Visitor&& __visitor, _Vs&&... __vs) {
1564 using __variant_detail::__visitation::__variant;
1565 std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1566 return __variant::__visit_value(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
1569 # if _LIBCPP_STD_VER >= 20
1570 template < class _Rp, class _Visitor, class... _Vs, typename>
1571 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp
1572 visit(_Visitor&& __visitor, _Vs&&... __vs) {
1573 using __variant_detail::__visitation::__variant;
1574 std::__throw_if_valueless(std::forward<_Vs>(__vs)...);
1575 return __variant::__visit_value<_Rp>(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
1579 template <class... _Types>
1580 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto
1581 swap(variant<_Types...>& __lhs,
1582 variant<_Types...>& __rhs) noexcept(noexcept(__lhs.swap(__rhs))) -> decltype(__lhs.swap(__rhs)) {
1583 return __lhs.swap(__rhs);
1586 template <class... _Types>
1587 struct _LIBCPP_TEMPLATE_VIS hash< __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
1588 using argument_type = variant<_Types...>;
1589 using result_type = size_t;
1591 _LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type& __v) const {
1592 using __variant_detail::__visitation::__variant;
1594 __v.valueless_by_exception()
1595 ? 299792458 // Random value chosen by the universe upon creation
1596 : __variant::__visit_alt(
1597 [](const auto& __alt) {
1598 using __alt_type = __remove_cvref_t<decltype(__alt)>;
1599 using __value_type = remove_const_t< typename __alt_type::__value_type>;
1600 return hash<__value_type>{}(__alt.__value);
1603 return std::__hash_combine(__res, hash<size_t>{}(__v.index()));
1607 // __unchecked_get is the same as std::get, except, it is UB to use it with the wrong
1608 // type whereas std::get will throw or returning nullptr. This makes it faster than
1610 template <size_t _Ip, class _Vp>
1611 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(_Vp&& __v) noexcept {
1612 using __variant_detail::__access::__variant;
1613 return __variant::__get_alt<_Ip>(std::forward<_Vp>(__v)).__value;
1616 template <class _Tp, class... _Types>
1617 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept {
1618 return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1621 template <class _Tp, class... _Types>
1622 _LIBCPP_HIDE_FROM_ABI constexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept {
1623 return std::__unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
1626 # endif // _LIBCPP_STD_VER >= 17
1628 _LIBCPP_END_NAMESPACE_STD
1632 # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1634 # include <exception>
1636 # include <type_traits>
1637 # include <typeinfo>
1640 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
1642 #endif // _LIBCPP_VARIANT