1 // <variant> -*- C++ -*-
3 // Copyright (C) 2016-2026 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 * This is the `<variant>` C++ Library header.
29 #ifndef _GLIBCXX_VARIANT
30 #define _GLIBCXX_VARIANT 1
32 #ifdef _GLIBCXX_SYSHDR
33 #pragma GCC system_header
36 #define __glibcxx_want_freestanding_variant
37 #define __glibcxx_want_variant
38 #define __glibcxx_want_constrained_equality
39 #define __glibcxx_want_constexpr_exceptions
40 #include <bits/version.h>
42 #ifdef __cpp_lib_variant // C++ >= 17
43 #include <initializer_list>
44 #include <type_traits>
45 #include <bits/enable_special_members.h>
46 #include <bits/exception_defines.h>
47 #include <bits/functional_hash.h>
48 #include <bits/invoke.h>
49 #include <bits/monostate.h>
50 #include <bits/parse_numbers.h> // _Select_int
51 #include <bits/stl_iterator_base_funcs.h>
52 #include <bits/stl_construct.h>
53 #include <bits/utility.h> // in_place_index_t
54 #if __cplusplus >= 202002L
59 // C++ < 20 || __cpp_concepts < 202002L || __cpp_constexpr < 201811L
60 #if __cpp_lib_variant < 202106L
61 # include <ext/aligned_buffer.h> // Use __aligned_membuf for storage.
64 namespace std _GLIBCXX_VISIBILITY(default)
66 _GLIBCXX_BEGIN_NAMESPACE_VERSION
68 template<typename... _Types> class tuple;
69 template<typename... _Types> class variant;
71 template<typename _Variant>
74 template<typename _Variant>
75 struct variant_size<const _Variant> : variant_size<_Variant> {};
77 template<typename _Variant>
78 struct variant_size<volatile _Variant> : variant_size<_Variant> {};
80 template<typename _Variant>
81 struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
83 template<typename... _Types>
84 struct variant_size<variant<_Types...>>
85 : std::integral_constant<size_t, sizeof...(_Types)> {};
87 template<typename _Variant>
88 inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
90 template<typename... _Types>
91 inline constexpr size_t
92 variant_size_v<variant<_Types...>> = sizeof...(_Types);
94 template<typename... _Types>
95 inline constexpr size_t
96 variant_size_v<const variant<_Types...>> = sizeof...(_Types);
98 template<size_t _Np, typename _Variant>
99 struct variant_alternative;
101 template<size_t _Np, typename... _Types>
102 struct variant_alternative<_Np, variant<_Types...>>
104 static_assert(_Np < sizeof...(_Types));
106 using type = typename _Nth_type<_Np, _Types...>::type;
109 template<size_t _Np, typename _Variant>
110 using variant_alternative_t =
111 typename variant_alternative<_Np, _Variant>::type;
113 template<size_t _Np, typename _Variant>
114 struct variant_alternative<_Np, const _Variant>
115 { using type = const variant_alternative_t<_Np, _Variant>; };
117 template<size_t _Np, typename _Variant>
118 struct variant_alternative<_Np, volatile _Variant>
119 { using type = volatile variant_alternative_t<_Np, _Variant>; };
121 template<size_t _Np, typename _Variant>
122 struct variant_alternative<_Np, const volatile _Variant>
123 { using type = const volatile variant_alternative_t<_Np, _Variant>; };
125 inline constexpr size_t variant_npos = -1;
127 template<size_t _Np, typename... _Types>
128 constexpr variant_alternative_t<_Np, variant<_Types...>>&
129 get(variant<_Types...>&);
131 template<size_t _Np, typename... _Types>
132 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
133 get(variant<_Types...>&&);
135 template<size_t _Np, typename... _Types>
136 constexpr variant_alternative_t<_Np, variant<_Types...>> const&
137 get(const variant<_Types...>&);
139 template<size_t _Np, typename... _Types>
140 constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
141 get(const variant<_Types...>&&);
143 template<typename _Result_type, typename _Visitor, typename... _Variants>
144 constexpr decltype(auto)
145 __do_visit(_Visitor&& __visitor, _Variants&&... __variants);
147 template <typename... _Types, typename _Tp>
150 __variant_cast(_Tp&& __rhs)
152 if constexpr (is_lvalue_reference_v<_Tp>)
154 if constexpr (is_const_v<remove_reference_t<_Tp>>)
155 return static_cast<const variant<_Types...>&>(__rhs);
157 return static_cast<variant<_Types...>&>(__rhs);
160 return static_cast<variant<_Types...>&&>(__rhs);
167 // used for raw visitation
168 struct __variant_cookie {};
169 // used for raw visitation with indices passed in
170 struct __variant_idx_cookie { using type = __variant_idx_cookie; };
171 // Used to enable deduction (and same-type checking) for std::visit:
172 template<typename _Tp> struct __deduce_visit_result { using type = _Tp; };
174 // Visit variants that might be valueless.
175 template<typename _Visitor, typename... _Variants>
177 __raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
179 std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
180 std::forward<_Variants>(__variants)...);
183 // Visit variants that might be valueless, passing indices to the visitor.
184 template<typename _Visitor, typename... _Variants>
186 __raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
188 std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
189 std::forward<_Variants>(__variants)...);
192 // The __as function templates implement the exposition-only "as-variant"
194 template<typename... _Types>
195 constexpr std::variant<_Types...>&
196 __as(std::variant<_Types...>& __v) noexcept
199 template<typename... _Types>
200 constexpr const std::variant<_Types...>&
201 __as(const std::variant<_Types...>& __v) noexcept
204 template<typename... _Types>
205 constexpr std::variant<_Types...>&&
206 __as(std::variant<_Types...>&& __v) noexcept
207 { return std::move(__v); }
209 template<typename... _Types>
210 constexpr const std::variant<_Types...>&&
211 __as(const std::variant<_Types...>&& __v) noexcept
212 { return std::move(__v); }
214 #if (__cpp_lib_variant < 202106L) && defined(_GLIBCXX_USE_VARIANT_CXX17_OLD_ABI)
215 template<typename _Type, bool = std::is_trivially_destructible_v<_Type>>
216 struct _Uninitialized;
218 template<typename _Type, bool /* unused */ = true>
219 struct _Uninitialized;
222 // The primary template is used for trivially destructible types in C++17,
223 // and for all types in C++20.
224 template<typename _Type, bool>
225 struct _Uninitialized
227 template<typename... _Args>
229 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
230 : _M_storage(std::forward<_Args>(__args)...)
233 #if (__cpp_lib_variant < 202106L) && defined(_GLIBCXX_USE_VARIANT_CXX17_OLD_ABI)
234 constexpr const _Type& _M_get() const & noexcept
235 { return _M_storage; }
237 constexpr _Type& _M_get() & noexcept
238 { return _M_storage; }
240 constexpr const _Type&& _M_get() const && noexcept
241 { return std::move(_M_storage); }
243 constexpr _Type&& _M_get() && noexcept
244 { return std::move(_M_storage); }
250 #if (__cpp_lib_variant < 202106L) && defined(_GLIBCXX_USE_VARIANT_CXX17_OLD_ABI)
251 // This partial specialization is used for non-trivially destructible types
253 template<typename _Type>
254 struct _Uninitialized<_Type, false>
256 template<typename... _Args>
258 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
260 ::new ((void*)std::addressof(_M_storage))
261 _Type(std::forward<_Args>(__args)...);
264 const _Type& _M_get() const & noexcept
265 { return *_M_storage._M_ptr(); }
267 _Type& _M_get() & noexcept
268 { return *_M_storage._M_ptr(); }
270 const _Type&& _M_get() const && noexcept
271 { return std::move(*_M_storage._M_ptr()); }
273 _Type&& _M_get() && noexcept
274 { return std::move(*_M_storage._M_ptr()); }
276 __gnu_cxx::__aligned_membuf<_Type> _M_storage;
279 template<size_t _Np, typename _Union>
280 constexpr decltype(auto)
281 __get_n(_Union&& __u) noexcept
283 if constexpr (_Np == 0)
284 return std::forward<_Union>(__u)._M_first._M_get();
285 else if constexpr (_Np == 1)
286 return std::forward<_Union>(__u)._M_rest._M_first._M_get();
287 else if constexpr (_Np == 2)
288 return std::forward<_Union>(__u)._M_rest._M_rest._M_first._M_get();
290 return __variant::__get_n<_Np - 3>(
291 std::forward<_Union>(__u)._M_rest._M_rest._M_rest);
293 #else // !_GLIBCXX_USE_VARIANT_CXX17_OLD_ABI
294 template<size_t _Np, typename _Union>
296 __get_n(_Union&& __u) noexcept
298 if constexpr (_Np == 0)
299 return std::forward<_Union>(__u)._M_first._M_storage;
300 else if constexpr (_Np == 1)
301 return std::forward<_Union>(__u)._M_rest._M_first._M_storage;
302 else if constexpr (_Np == 2)
303 return std::forward<_Union>(__u)._M_rest._M_rest._M_first._M_storage;
305 return __variant::__get_n<_Np - 3>(
306 std::forward<_Union>(__u)._M_rest._M_rest._M_rest);
308 #endif // !_GLIBCXX_USE_VARIANT_CXX17_OLD_ABI
310 // Returns the typed storage for __v.
311 template<size_t _Np, typename _Variant>
312 constexpr decltype(auto)
313 __get(_Variant&& __v) noexcept
314 { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
316 // Gets the _Uninitialized to construct into for __u.
317 template<size_t _Np, typename _Union>
318 constexpr decltype(auto)
319 __construct_n(_Union& __u) noexcept
321 if constexpr (_Np == 0)
322 return &__u._M_first;
323 else if constexpr (_Np == 1)
325 std::_Construct(&__u._M_rest);
326 return &__u._M_rest._M_first;
328 else if constexpr (_Np == 2)
330 std::_Construct(&__u._M_rest);
331 std::_Construct(&__u._M_rest._M_rest);
332 return &__u._M_rest._M_rest._M_first;
336 std::_Construct(&__u._M_rest);
337 std::_Construct(&__u._M_rest._M_rest);
338 std::_Construct(&__u._M_rest._M_rest._M_rest);
339 return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest);
343 template<typename... _Types>
346 static constexpr bool _S_default_ctor =
347 is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
348 static constexpr bool _S_copy_ctor =
349 (is_copy_constructible_v<_Types> && ...);
350 static constexpr bool _S_move_ctor =
351 (is_move_constructible_v<_Types> && ...);
352 static constexpr bool _S_copy_assign =
354 && (is_copy_assignable_v<_Types> && ...);
355 static constexpr bool _S_move_assign =
357 && (is_move_assignable_v<_Types> && ...);
359 static constexpr bool _S_trivial_dtor =
360 (is_trivially_destructible_v<_Types> && ...);
361 static constexpr bool _S_trivial_copy_ctor =
362 (is_trivially_copy_constructible_v<_Types> && ...);
363 static constexpr bool _S_trivial_move_ctor =
364 (is_trivially_move_constructible_v<_Types> && ...);
365 static constexpr bool _S_trivial_copy_assign =
366 _S_trivial_dtor && _S_trivial_copy_ctor
367 && (is_trivially_copy_assignable_v<_Types> && ...);
368 static constexpr bool _S_trivial_move_assign =
369 _S_trivial_dtor && _S_trivial_move_ctor
370 && (is_trivially_move_assignable_v<_Types> && ...);
372 // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
373 // are always nothrow.
374 static constexpr bool _S_nothrow_default_ctor =
375 is_nothrow_default_constructible_v<
376 typename _Nth_type<0, _Types...>::type>;
377 static constexpr bool _S_nothrow_copy_ctor = false;
378 static constexpr bool _S_nothrow_move_ctor =
379 (is_nothrow_move_constructible_v<_Types> && ...);
380 static constexpr bool _S_nothrow_copy_assign = false;
381 static constexpr bool _S_nothrow_move_assign =
383 && (is_nothrow_move_assignable_v<_Types> && ...);
386 // Defines members and ctors.
387 template<bool __trivially_destructible, typename... _Types>
388 union _Variadic_union
390 _Variadic_union() = default;
392 template<size_t _Np, typename... _Args>
393 _Variadic_union(in_place_index_t<_Np>, _Args&&...) = delete;
396 template<typename _First, typename... _Rest>
397 union _Variadic_union<true, _First, _Rest...>
399 constexpr _Variadic_union() : _M_rest() { }
401 template<typename... _Args>
403 _Variadic_union(in_place_index_t<0>, _Args&&... __args)
404 : _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
407 template<size_t _Np, typename... _Args>
409 _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
410 : _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
413 _Uninitialized<_First> _M_first;
414 _Variadic_union<true, _Rest...> _M_rest;
417 template<typename _First, typename... _Rest>
418 union _Variadic_union<false, _First, _Rest...>
420 constexpr _Variadic_union() : _M_rest() { }
422 template<typename... _Args>
424 _Variadic_union(in_place_index_t<0>, _Args&&... __args)
425 : _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
428 template<size_t _Np, typename... _Args>
430 _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
431 : _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
434 _Variadic_union(const _Variadic_union&) = default;
435 _Variadic_union(_Variadic_union&&) = default;
436 _Variadic_union& operator=(const _Variadic_union&) = default;
437 _Variadic_union& operator=(_Variadic_union&&) = default;
439 // If any alternative type is not trivially destructible then we need a
440 // user-provided destructor that does nothing. The active alternative
441 // will be destroyed by _Variant_storage::_M_reset() instead of here.
442 _GLIBCXX20_CONSTEXPR ~_Variadic_union()
445 _Uninitialized<_First> _M_first;
446 _Variadic_union<(is_trivially_destructible_v<_Rest> && ...), _Rest...> _M_rest;
449 // _Never_valueless_alt is true for variant alternatives that can
450 // always be placed in a variant without it becoming valueless.
452 // For suitably-small, trivially copyable types we can create temporaries
453 // on the stack and then memcpy them into place.
454 template<typename _Tp>
455 struct _Never_valueless_alt
456 : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
459 // Specialize _Never_valueless_alt for other types which have a
460 // non-throwing and cheap move construction and move assignment operator,
461 // so that emplacing the type will provide the strong exception-safety
462 // guarantee, by creating and moving a temporary.
463 // Whether _Never_valueless_alt<T> is true or not affects the ABI of a
464 // variant using that alternative, so we can't change the value later!
466 // True if every alternative in _Types... can be emplaced in a variant
467 // without it becoming valueless. If this is true, variant<_Types...>
468 // can never be valueless, which enables some minor optimizations.
469 template <typename... _Types>
470 constexpr bool __never_valueless()
472 return _Traits<_Types...>::_S_move_assign
473 && (_Never_valueless_alt<_Types>::value && ...);
476 // Defines index and the dtor, possibly trivial.
477 template<bool __trivially_destructible, typename... _Types>
478 struct _Variant_storage;
480 template <typename... _Types>
481 using __select_index =
482 typename __select_int::_Select_int_base<sizeof...(_Types),
484 unsigned short>::type::value_type;
486 template<typename... _Types>
487 struct _Variant_storage<false, _Types...>
491 : _M_index(static_cast<__index_type>(variant_npos))
494 template<size_t _Np, typename... _Args>
496 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
497 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
504 if (!_M_valid()) [[__unlikely__]]
507 std::__do_visit<void>([](auto&& __this_mem) mutable
509 std::_Destroy(std::__addressof(__this_mem));
510 }, __variant_cast<_Types...>(*this));
512 _M_index = static_cast<__index_type>(variant_npos);
520 _M_valid() const noexcept
522 if constexpr (__variant::__never_valueless<_Types...>())
524 return this->_M_index != __index_type(variant_npos);
527 _Variadic_union<false, _Types...> _M_u;
528 using __index_type = __select_index<_Types...>;
529 __index_type _M_index;
532 template<typename... _Types>
533 struct _Variant_storage<true, _Types...>
537 : _M_index(static_cast<__index_type>(variant_npos))
540 template<size_t _Np, typename... _Args>
542 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
543 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
549 { _M_index = static_cast<__index_type>(variant_npos); }
552 _M_valid() const noexcept
554 if constexpr (__variant::__never_valueless<_Types...>())
556 // It would be nice if we could just return true for -fno-exceptions.
557 // It's possible (but inadvisable) that a std::variant could become
558 // valueless in a translation unit compiled with -fexceptions and then
559 // be passed to functions compiled with -fno-exceptions. We would need
560 // some #ifdef _GLIBCXX_NO_EXCEPTIONS_GLOBALLY property to elide all
561 // checks for valueless_by_exception().
562 return this->_M_index != static_cast<__index_type>(variant_npos);
565 _Variadic_union<true, _Types...> _M_u;
566 using __index_type = __select_index<_Types...>;
567 __index_type _M_index;
570 // Implementation of v.emplace<N>(args...).
571 template<size_t _Np, bool _Triv, typename... _Types, typename... _Args>
574 __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
577 auto* __addr = __variant::__construct_n<_Np>(__v._M_u);
578 std::_Construct(__addr, in_place_index<0>,
579 std::forward<_Args>(__args)...);
580 // Construction didn't throw, so can set the new index now:
584 template<typename... _Types>
585 using _Variant_storage_alias =
586 _Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
588 // The following are (Copy|Move) (ctor|assign) layers for forwarding
589 // triviality and handling non-trivial SMF behaviors.
591 template<bool, typename... _Types>
592 struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
594 using _Base = _Variant_storage_alias<_Types...>;
598 _Copy_ctor_base(const _Copy_ctor_base& __rhs)
599 noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
601 __variant::__raw_idx_visit(
602 [this](auto&& __rhs_mem, auto __rhs_index) mutable
604 constexpr size_t __j = __rhs_index;
605 if constexpr (__j != variant_npos)
606 std::_Construct(std::__addressof(this->_M_u),
607 in_place_index<__j>, __rhs_mem);
608 }, __variant_cast<_Types...>(__rhs));
609 this->_M_index = __rhs._M_index;
612 _Copy_ctor_base(_Copy_ctor_base&&) = default;
613 _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
614 _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
617 template<typename... _Types>
618 struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
620 using _Base = _Variant_storage_alias<_Types...>;
624 template<typename... _Types>
625 using _Copy_ctor_alias =
626 _Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
628 template<bool, typename... _Types>
629 struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
631 using _Base = _Copy_ctor_alias<_Types...>;
635 _Move_ctor_base(_Move_ctor_base&& __rhs)
636 noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
638 __variant::__raw_idx_visit(
639 [this](auto&& __rhs_mem, auto __rhs_index) mutable
641 constexpr size_t __j = __rhs_index;
642 if constexpr (__j != variant_npos)
643 std::_Construct(std::__addressof(this->_M_u),
645 std::forward<decltype(__rhs_mem)>(__rhs_mem));
646 }, __variant_cast<_Types...>(std::move(__rhs)));
647 this->_M_index = __rhs._M_index;
650 _Move_ctor_base(const _Move_ctor_base&) = default;
651 _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
652 _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
655 template<typename... _Types>
656 struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
658 using _Base = _Copy_ctor_alias<_Types...>;
662 template<typename... _Types>
663 using _Move_ctor_alias =
664 _Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
666 template<bool, typename... _Types>
667 struct _Copy_assign_base : _Move_ctor_alias<_Types...>
669 using _Base = _Move_ctor_alias<_Types...>;
674 operator=(const _Copy_assign_base& __rhs)
675 noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
677 __variant::__raw_idx_visit(
678 [this](auto&& __rhs_mem, auto __rhs_index) mutable
680 constexpr size_t __j = __rhs_index;
681 if constexpr (__j == variant_npos)
682 this->_M_reset(); // Make *this valueless.
683 else if (this->_M_index == __j)
684 __variant::__get<__j>(*this) = __rhs_mem;
687 using _Tj = typename _Nth_type<__j, _Types...>::type;
688 if constexpr (is_nothrow_copy_constructible_v<_Tj>
689 || !is_nothrow_move_constructible_v<_Tj>)
690 __variant::__emplace<__j>(*this, __rhs_mem);
693 using _Variant = variant<_Types...>;
694 _Variant& __self = __variant_cast<_Types...>(*this);
695 __self = _Variant(in_place_index<__j>, __rhs_mem);
698 }, __variant_cast<_Types...>(__rhs));
702 _Copy_assign_base(const _Copy_assign_base&) = default;
703 _Copy_assign_base(_Copy_assign_base&&) = default;
704 _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
707 template<typename... _Types>
708 struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
710 using _Base = _Move_ctor_alias<_Types...>;
714 template<typename... _Types>
715 using _Copy_assign_alias =
716 _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
718 template<bool, typename... _Types>
719 struct _Move_assign_base : _Copy_assign_alias<_Types...>
721 using _Base = _Copy_assign_alias<_Types...>;
726 operator=(_Move_assign_base&& __rhs)
727 noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
729 __variant::__raw_idx_visit(
730 [this](auto&& __rhs_mem, auto __rhs_index) mutable
732 constexpr size_t __j = __rhs_index;
733 if constexpr (__j != variant_npos)
735 if (this->_M_index == __j)
736 __variant::__get<__j>(*this) = std::move(__rhs_mem);
739 using _Tj = typename _Nth_type<__j, _Types...>::type;
740 if constexpr (is_nothrow_move_constructible_v<_Tj>)
741 __variant::__emplace<__j>(*this, std::move(__rhs_mem));
744 using _Variant = variant<_Types...>;
745 _Variant& __self = __variant_cast<_Types...>(*this);
746 __self.template emplace<__j>(std::move(__rhs_mem));
752 }, __variant_cast<_Types...>(__rhs));
756 _Move_assign_base(const _Move_assign_base&) = default;
757 _Move_assign_base(_Move_assign_base&&) = default;
758 _Move_assign_base& operator=(const _Move_assign_base&) = default;
761 template<typename... _Types>
762 struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
764 using _Base = _Copy_assign_alias<_Types...>;
768 template<typename... _Types>
769 using _Move_assign_alias =
770 _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
772 template<typename... _Types>
773 struct _Variant_base : _Move_assign_alias<_Types...>
775 using _Base = _Move_assign_alias<_Types...>;
778 _Variant_base() noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
779 : _Variant_base(in_place_index<0>) { }
781 template<size_t _Np, typename... _Args>
783 _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
784 : _Base(__i, std::forward<_Args>(__args)...)
787 _Variant_base(const _Variant_base&) = default;
788 _Variant_base(_Variant_base&&) = default;
789 _Variant_base& operator=(const _Variant_base&) = default;
790 _Variant_base& operator=(_Variant_base&&) = default;
793 template<typename _Tp, typename... _Types>
794 inline constexpr bool __exactly_once
795 = std::__find_uniq_type_in_pack<_Tp, _Types...>() < sizeof...(_Types);
797 // Helper used to check for valid conversions that don't involve narrowing.
798 template<typename _Ti> struct _Arr { _Ti _M_x[1]; };
800 // "Build an imaginary function FUN(Ti) for each alternative type Ti"
801 template<size_t _Ind, typename _Tp, typename _Ti, typename = void>
804 // This function means 'using _Build_FUN<I, T, Ti>::_S_fun;' is valid,
805 // but only static functions will be considered in the call below.
806 void _S_fun() = delete;
809 // "... for which Ti x[] = {std::forward<T>(t)}; is well-formed."
810 template<size_t _Ind, typename _Tp, typename _Ti>
811 struct _Build_FUN<_Ind, _Tp, _Ti,
812 void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
814 // This is the FUN function for type _Ti, with index _Ind
815 static integral_constant<size_t, _Ind> _S_fun(_Ti);
818 template<typename _Tp, typename _Variant,
819 typename = make_index_sequence<variant_size_v<_Variant>>>
822 template<typename _Tp, typename... _Ti, size_t... _Ind>
823 struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
824 : _Build_FUN<_Ind, _Tp, _Ti>...
826 using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
829 // The index j of the overload FUN(Tj) selected by overload resolution
830 // for FUN(std::forward<_Tp>(t))
831 template<typename _Tp, typename _Variant>
833 = decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
835 // The index selected for FUN(std::forward<T>(t)), or variant_npos if none.
836 template<typename _Tp, typename _Variant, typename = void>
837 inline constexpr size_t
838 __accepted_index = variant_npos;
840 template<typename _Tp, typename _Variant>
841 inline constexpr size_t
842 __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
843 = _FUN_type<_Tp, _Variant>::value;
845 template<typename _Maybe_variant_cookie, typename _Variant,
846 typename = __remove_cvref_t<_Variant>>
847 inline constexpr bool
848 __extra_visit_slot_needed = false;
850 template<typename _Var, typename... _Types>
851 inline constexpr bool
852 __extra_visit_slot_needed<__variant_cookie, _Var, variant<_Types...>>
853 = !__variant::__never_valueless<_Types...>();
855 template<typename _Var, typename... _Types>
856 inline constexpr bool
857 __extra_visit_slot_needed<__variant_idx_cookie, _Var, variant<_Types...>>
858 = !__variant::__never_valueless<_Types...>();
860 // Used for storing a multi-dimensional vtable.
861 template<typename _Tp, size_t... _Dimensions>
864 // Partial specialization with rank zero, stores a single _Tp element.
865 template<typename _Tp>
866 struct _Multi_array<_Tp>
869 struct __untag_result
871 { using element_type = _Tp; };
873 #pragma GCC diagnostic push
874 #pragma GCC diagnostic ignored "-Wignored-qualifiers"
875 template <typename... _Args>
876 struct __untag_result<const void(*)(_Args...)>
878 { using element_type = void(*)(_Args...); };
879 #pragma GCC diagnostic pop
881 template <typename... _Args>
882 struct __untag_result<__variant_cookie(*)(_Args...)>
884 { using element_type = void(*)(_Args...); };
886 template <typename... _Args>
887 struct __untag_result<__variant_idx_cookie(*)(_Args...)>
889 { using element_type = void(*)(_Args...); };
891 template <typename _Res, typename... _Args>
892 struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
894 { using element_type = _Res(*)(_Args...); };
896 using __result_is_deduced = __untag_result<_Tp>;
898 constexpr const typename __untag_result<_Tp>::element_type&
902 typename __untag_result<_Tp>::element_type _M_data;
905 // Partial specialization with rank >= 1.
906 template<typename _Ret,
908 typename... _Variants,
909 size_t __first, size_t... __rest>
910 struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
912 static constexpr size_t __index =
913 sizeof...(_Variants) - sizeof...(__rest) - 1;
915 using _Variant = typename _Nth_type<__index, _Variants...>::type;
917 static constexpr int __do_cookie =
918 __extra_visit_slot_needed<_Ret, _Variant> ? 1 : 0;
920 using _Tp = _Ret(*)(_Visitor, _Variants...);
922 template<typename... _Args>
923 constexpr decltype(auto)
924 _M_access(size_t __first_index, _Args... __rest_indices) const
926 return _M_arr[__first_index + __do_cookie]
927 ._M_access(__rest_indices...);
930 _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
933 // Creates a multi-dimensional vtable recursively.
936 // visit([](auto, auto){},
937 // variant<int, char>(), // typedef'ed as V1
938 // variant<float, double, long double>()) // typedef'ed as V2
939 // will trigger instantiations of:
940 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
941 // tuple<V1&&, V2&&>, std::index_sequence<>>
942 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
943 // tuple<V1&&, V2&&>, std::index_sequence<0>>
944 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
945 // tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
946 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
947 // tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
948 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
949 // tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
950 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
951 // tuple<V1&&, V2&&>, std::index_sequence<1>>
952 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
953 // tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
954 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
955 // tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
956 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
957 // tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
958 // The returned multi-dimensional vtable can be fast accessed by the visitor
959 // using index calculation.
960 template<typename _Array_type, typename _Index_seq>
961 struct __gen_vtable_impl;
963 // Defines the _S_apply() member that returns a _Multi_array populated
964 // with function pointers that perform the visitation expressions e(m)
965 // for each valid pack of indexes into the variant types _Variants.
967 // This partial specialization builds up the index sequences by recursively
968 // calling _S_apply() on the next specialization of __gen_vtable_impl.
969 // The base case of the recursion defines the actual function pointers.
970 template<typename _Result_type, typename _Visitor, size_t... __dimensions,
971 typename... _Variants, size_t... __indices>
972 struct __gen_vtable_impl<
973 _Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
974 std::index_sequence<__indices...>>
977 remove_reference_t<typename _Nth_type<sizeof...(__indices),
978 _Variants...>::type>;
980 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
983 static constexpr _Array_type
986 _Array_type __vtable{};
988 __vtable, make_index_sequence<variant_size_v<_Next>>());
992 template<size_t... __var_indices>
993 static constexpr void
994 _S_apply_all_alts(_Array_type& __vtable,
995 std::index_sequence<__var_indices...>)
997 if constexpr (__extra_visit_slot_needed<_Result_type, _Next>)
998 (_S_apply_single_alt<true, __var_indices>(
999 __vtable._M_arr[__var_indices + 1],
1000 &(__vtable._M_arr[0])), ...);
1002 (_S_apply_single_alt<false, __var_indices>(
1003 __vtable._M_arr[__var_indices]), ...);
1006 template<bool __do_cookie, size_t __index, typename _Tp>
1007 static constexpr void
1008 _S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
1010 if constexpr (__do_cookie)
1012 __element = __gen_vtable_impl<
1014 std::index_sequence<__indices..., __index>>::_S_apply();
1015 *__cookie_element = __gen_vtable_impl<
1017 std::index_sequence<__indices..., variant_npos>>::_S_apply();
1021 auto __tmp_element = __gen_vtable_impl<
1022 remove_reference_t<decltype(__element)>,
1023 std::index_sequence<__indices..., __index>>::_S_apply();
1024 static_assert(is_same_v<_Tp, decltype(__tmp_element)>,
1025 "std::visit requires the visitor to have the same "
1026 "return type for all alternatives of a variant");
1027 __element = __tmp_element;
1032 // This partial specialization is the base case for the recursion.
1033 // It populates a _Multi_array element with the address of a function
1034 // that invokes the visitor with the alternatives specified by __indices.
1035 template<typename _Result_type, typename _Visitor, typename... _Variants,
1036 size_t... __indices>
1037 struct __gen_vtable_impl<
1038 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
1039 std::index_sequence<__indices...>>
1042 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
1044 template<size_t __index, typename _Variant>
1045 static constexpr decltype(auto)
1046 __element_by_index_or_cookie(_Variant&& __var) noexcept
1048 if constexpr (__index != variant_npos)
1049 return __variant::__get<__index>(std::forward<_Variant>(__var));
1051 return __variant_cookie{};
1054 static constexpr decltype(auto)
1055 __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
1057 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
1058 // For raw visitation using indices, pass the indices to the visitor
1059 // and discard the return value:
1060 std::__invoke(std::forward<_Visitor>(__visitor),
1061 __element_by_index_or_cookie<__indices>(
1062 std::forward<_Variants>(__vars))...,
1063 integral_constant<size_t, __indices>()...);
1064 else if constexpr (is_same_v<_Result_type, __variant_cookie>)
1065 // For raw visitation without indices, and discard the return value:
1066 std::__invoke(std::forward<_Visitor>(__visitor),
1067 __element_by_index_or_cookie<__indices>(
1068 std::forward<_Variants>(__vars))...);
1069 else if constexpr (_Array_type::__result_is_deduced::value)
1070 // For the usual std::visit case deduce the return value:
1071 return std::__invoke(std::forward<_Visitor>(__visitor),
1072 __element_by_index_or_cookie<__indices>(
1073 std::forward<_Variants>(__vars))...);
1074 else // for std::visit<R> use INVOKE<R>
1075 return std::__invoke_r<_Result_type>(
1076 std::forward<_Visitor>(__visitor),
1077 __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
1080 static constexpr auto
1083 if constexpr (_Array_type::__result_is_deduced::value)
1085 constexpr bool __visit_ret_type_mismatch =
1086 !is_same_v<typename _Result_type::type,
1087 decltype(__visit_invoke(std::declval<_Visitor>(),
1088 std::declval<_Variants>()...))>;
1089 if constexpr (__visit_ret_type_mismatch)
1091 struct __cannot_match {};
1092 return __cannot_match{};
1095 return _Array_type{&__visit_invoke};
1098 return _Array_type{&__visit_invoke};
1102 template<typename _Result_type, typename _Visitor, typename... _Variants>
1106 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
1107 variant_size_v<remove_reference_t<_Variants>>...>;
1109 static constexpr _Array_type _S_vtable
1110 = __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
1113 #if ! _GLIBCXX_INLINE_VERSION
1114 template<size_t _Nm, typename _Tp>
1115 struct _Base_dedup : public _Tp { };
1117 template<typename _Variant, typename __indices>
1118 struct _Variant_hash_base;
1120 template<typename... _Types, size_t... __indices>
1121 struct _Variant_hash_base<variant<_Types...>,
1122 std::index_sequence<__indices...>>
1123 : _Base_dedup<__indices, __hash_empty_base<remove_const_t<_Types>>>...
1127 // Equivalent to decltype(get<_Np>(as-variant(declval<_Variant>())))
1128 template<size_t _Np, typename _Variant,
1129 typename _AsV = decltype(__variant::__as(std::declval<_Variant>())),
1130 typename _Tp = variant_alternative_t<_Np, remove_reference_t<_AsV>>>
1132 = __conditional_t<is_lvalue_reference_v<_Variant>, _Tp&, _Tp&&>;
1134 // Return type of std::visit.
1135 template<typename _Visitor, typename... _Variants>
1136 using __visit_result_t
1137 = invoke_result_t<_Visitor, __get_t<0, _Variants>...>;
1139 template<typename _Tp, typename... _Types>
1140 constexpr inline bool __same_types = (is_same_v<_Tp, _Types> && ...);
1142 template <typename _Visitor, typename _Variant, size_t... _Idxs>
1143 constexpr bool __check_visitor_results(std::index_sequence<_Idxs...>)
1145 return __same_types<
1146 invoke_result_t<_Visitor, __get_t<_Idxs, _Variant>>...
1150 } // namespace __variant
1151 } // namespace __detail
1153 template<typename _Tp, typename... _Types>
1155 holds_alternative(const variant<_Types...>& __v) noexcept
1157 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1158 "T must occur exactly once in alternatives");
1159 return __v.index() == std::__find_uniq_type_in_pack<_Tp, _Types...>();
1162 template<typename _Tp, typename... _Types>
1164 get(variant<_Types...>& __v)
1166 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1167 "T must occur exactly once in alternatives");
1168 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1169 return std::get<__n>(__v);
1172 template<typename _Tp, typename... _Types>
1174 get(variant<_Types...>&& __v)
1176 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1177 "T must occur exactly once in alternatives");
1178 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1179 return std::get<__n>(std::move(__v));
1182 template<typename _Tp, typename... _Types>
1183 constexpr const _Tp&
1184 get(const variant<_Types...>& __v)
1186 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1187 "T must occur exactly once in alternatives");
1188 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1189 return std::get<__n>(__v);
1192 template<typename _Tp, typename... _Types>
1193 constexpr const _Tp&&
1194 get(const variant<_Types...>&& __v)
1196 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1197 "T must occur exactly once in alternatives");
1198 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1199 return std::get<__n>(std::move(__v));
1202 template<size_t _Np, typename... _Types>
1203 constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
1204 get_if(variant<_Types...>* __ptr) noexcept
1206 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1207 static_assert(_Np < sizeof...(_Types),
1208 "The index must be in [0, number of alternatives)");
1209 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1210 if (__ptr && __ptr->index() == _Np)
1211 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1215 template<size_t _Np, typename... _Types>
1217 add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
1218 get_if(const variant<_Types...>* __ptr) noexcept
1220 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1221 static_assert(_Np < sizeof...(_Types),
1222 "The index must be in [0, number of alternatives)");
1223 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1224 if (__ptr && __ptr->index() == _Np)
1225 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1229 template<typename _Tp, typename... _Types>
1230 constexpr add_pointer_t<_Tp>
1231 get_if(variant<_Types...>* __ptr) noexcept
1233 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1234 "T must occur exactly once in alternatives");
1235 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1236 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1237 return std::get_if<__n>(__ptr);
1240 template<typename _Tp, typename... _Types>
1241 constexpr add_pointer_t<const _Tp>
1242 get_if(const variant<_Types...>* __ptr) noexcept
1244 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1245 "T must occur exactly once in alternatives");
1246 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1247 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1248 return std::get_if<__n>(__ptr);
1251 namespace __detail::__variant
1253 template<typename _Ret, typename _Vp, typename _Op>
1255 __compare(_Ret __ret, const _Vp& __lhs, const _Vp& __rhs, _Op __op)
1257 __variant::__raw_idx_visit(
1258 [&__ret, &__lhs, __op] (auto&& __rhs_mem, auto __rhs_index) mutable
1260 if constexpr (__rhs_index != variant_npos)
1262 if (__lhs.index() == __rhs_index.value)
1264 auto& __this_mem = std::get<__rhs_index>(__lhs);
1265 __ret = __op(__this_mem, __rhs_mem);
1269 __ret = __op(__lhs.index() + 1, __rhs_index + 1);
1273 } // namespace __detail::__variant
1275 template<typename... _Types>
1276 #if __cpp_lib_concepts
1277 requires ((requires (const _Types& __t) {
1278 { __t == __t } -> convertible_to<bool>; }) && ...)
1281 operator== [[nodiscard]] (const variant<_Types...>& __lhs,
1282 const variant<_Types...>& __rhs)
1284 namespace __variant = __detail::__variant;
1285 return __variant::__compare(true, __lhs, __rhs,
1286 [](auto&& __l, auto&& __r) -> bool {
1291 template<typename... _Types>
1292 #if __cpp_lib_concepts
1293 requires ((requires (const _Types& __t) {
1294 { __t != __t } -> convertible_to<bool>; }) && ...)
1297 operator!= [[nodiscard]] (const variant<_Types...>& __lhs,
1298 const variant<_Types...>& __rhs)
1300 namespace __variant = __detail::__variant;
1301 return __variant::__compare(true, __lhs, __rhs,
1302 [](auto&& __l, auto&& __r) -> bool {
1307 template<typename... _Types>
1308 #if __cpp_lib_concepts
1309 requires ((requires (const _Types& __t) {
1310 { __t < __t } -> convertible_to<bool>; }) && ...)
1313 operator< [[nodiscard]] (const variant<_Types...>& __lhs,
1314 const variant<_Types...>& __rhs)
1316 namespace __variant = __detail::__variant;
1317 return __variant::__compare(true, __lhs, __rhs,
1318 [](auto&& __l, auto&& __r) -> bool {
1323 template<typename... _Types>
1324 #if __cpp_lib_concepts
1325 requires ((requires (const _Types& __t) {
1326 { __t <= __t } -> convertible_to<bool>; }) && ...)
1329 operator<= [[nodiscard]] (const variant<_Types...>& __lhs,
1330 const variant<_Types...>& __rhs)
1332 namespace __variant = __detail::__variant;
1333 return __variant::__compare(true, __lhs, __rhs,
1334 [](auto&& __l, auto&& __r) -> bool {
1339 template<typename... _Types>
1340 #if __cpp_lib_concepts
1341 requires ((requires (const _Types& __t) {
1342 { __t > __t } -> convertible_to<bool>; }) && ...)
1345 operator> [[nodiscard]] (const variant<_Types...>& __lhs,
1346 const variant<_Types...>& __rhs)
1348 namespace __variant = __detail::__variant;
1349 return __variant::__compare(true, __lhs, __rhs,
1350 [](auto&& __l, auto&& __r) -> bool {
1355 template<typename... _Types>
1356 #if __cpp_lib_concepts
1357 requires ((requires (const _Types& __t) {
1358 { __t >= __t } -> convertible_to<bool>; }) && ...)
1361 operator>= [[nodiscard]] (const variant<_Types...>& __lhs,
1362 const variant<_Types...>& __rhs)
1364 namespace __variant = __detail::__variant;
1365 return __variant::__compare(true, __lhs, __rhs,
1366 [](auto&& __l, auto&& __r) -> bool {
1371 #ifdef __cpp_lib_three_way_comparison
1372 template<typename... _Types>
1373 requires (three_way_comparable<_Types> && ...)
1375 common_comparison_category_t<compare_three_way_result_t<_Types>...>
1376 operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
1378 common_comparison_category_t<compare_three_way_result_t<_Types>...> __ret
1379 = strong_ordering::equal;
1380 namespace __variant = __detail::__variant;
1381 return __variant::__compare(__ret, __v, __w,
1382 [](auto&& __l, auto&& __r) {
1388 template<typename _Visitor, typename... _Variants>
1389 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1390 visit(_Visitor&&, _Variants&&...);
1392 #if __cplusplus > 201703L
1393 template<typename _Res, typename _Visitor, typename... _Variants>
1395 visit(_Visitor&&, _Variants&&...);
1398 template<typename... _Types>
1399 _GLIBCXX20_CONSTEXPR
1400 inline enable_if_t<(is_move_constructible_v<_Types> && ...)
1401 && (is_swappable_v<_Types> && ...)>
1402 swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1403 noexcept(noexcept(__lhs.swap(__rhs)))
1404 { __lhs.swap(__rhs); }
1406 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1407 // 2766. Swapping non-swappable types
1408 template<typename... _Types>
1409 enable_if_t<!((is_move_constructible_v<_Types> && ...)
1410 && (is_swappable_v<_Types> && ...))>
1411 swap(variant<_Types...>&, variant<_Types...>&) = delete;
1413 #if __cpp_lib_constexpr_exceptions >= 202502L
1414 #define _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS constexpr
1416 #define _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS
1418 [[noreturn]] _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS void
1419 __throw_bad_variant_access(unsigned);
1421 class bad_variant_access : public exception
1424 _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS bad_variant_access() noexcept { }
1426 _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS const char* what()
1427 const noexcept override
1428 { return _M_reason; }
1431 // Must only be called with a string literal
1432 _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS
1433 bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
1435 // Must point to a string with static storage duration:
1436 const char* _M_reason = "bad variant access";
1438 friend _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS void
1439 __throw_bad_variant_access([[maybe_unused]] unsigned __n)
1441 [[maybe_unused]] static constexpr const char* __reasons[] = {
1442 "std::get: wrong index for variant",
1443 "std::get: variant is valueless",
1444 "std::visit: variant is valueless",
1445 "std::visit<R>: variant is valueless",
1447 _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__reasons[__n % 4u]));
1449 #undef _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS
1452 template<typename... _Types>
1454 : private __detail::__variant::_Variant_base<_Types...>,
1455 private _Enable_copy_move<
1456 __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1457 __detail::__variant::_Traits<_Types...>::_S_copy_assign,
1458 __detail::__variant::_Traits<_Types...>::_S_move_ctor,
1459 __detail::__variant::_Traits<_Types...>::_S_move_assign,
1463 template <typename... _UTypes, typename _Tp>
1464 friend _GLIBCXX20_CONSTEXPR decltype(auto)
1465 __variant_cast(_Tp&&);
1467 static_assert(sizeof...(_Types) > 0,
1468 "variant must have at least one alternative");
1469 static_assert(((std::is_object_v<_Types> && !is_array_v<_Types>) && ...),
1470 "variant alternatives must be non-array object types");
1472 using _Base = __detail::__variant::_Variant_base<_Types...>;
1474 template<typename _Tp>
1475 static constexpr bool __not_self
1476 = !is_same_v<__remove_cvref_t<_Tp>, variant>;
1478 template<typename _Tp>
1479 static constexpr bool
1480 __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1482 template<typename _Tp>
1483 static constexpr size_t __accepted_index
1484 = __detail::__variant::__accepted_index<_Tp, variant>;
1486 template<size_t _Np, typename = enable_if_t<(_Np < sizeof...(_Types))>>
1487 using __to_type = typename _Nth_type<_Np, _Types...>::type;
1489 template<typename _Tp, typename = enable_if_t<__not_self<_Tp>>>
1490 using __accepted_type = __to_type<__accepted_index<_Tp>>;
1492 template<typename _Tp>
1493 static constexpr size_t __index_of
1494 = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1496 using _Traits = __detail::__variant::_Traits<_Types...>;
1498 template<typename _Tp>
1499 static constexpr bool __not_in_place_tag
1500 = !__is_in_place_type_v<__remove_cvref_t<_Tp>>
1501 && !__is_in_place_index_v<__remove_cvref_t<_Tp>>;
1505 variant() requires is_default_constructible_v<__to_type<0>> = default;
1507 template<typename _Tp0 = __to_type<0>,
1508 typename = enable_if_t<is_default_constructible_v<_Tp0>>>
1510 variant() noexcept(is_nothrow_default_constructible_v<__to_type<0>>)
1514 variant(const variant& __rhs) = default;
1515 variant(variant&&) = default;
1516 variant& operator=(const variant&) = default;
1517 variant& operator=(variant&&) = default;
1518 _GLIBCXX20_CONSTEXPR ~variant() = default;
1520 template<typename _Tp,
1521 typename = enable_if_t<sizeof...(_Types) != 0>,
1522 typename = enable_if_t<__not_in_place_tag<_Tp>>,
1523 typename _Tj = __accepted_type<_Tp&&>,
1524 typename = enable_if_t<__exactly_once<_Tj>
1525 && is_constructible_v<_Tj, _Tp>>>
1528 noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
1529 : variant(in_place_index<__accepted_index<_Tp>>,
1530 std::forward<_Tp>(__t))
1533 template<typename _Tp, typename... _Args,
1534 typename = enable_if_t<__exactly_once<_Tp>
1535 && is_constructible_v<_Tp, _Args...>>>
1537 variant(in_place_type_t<_Tp>, _Args&&... __args)
1538 : variant(in_place_index<__index_of<_Tp>>,
1539 std::forward<_Args>(__args)...)
1542 template<typename _Tp, typename _Up, typename... _Args,
1543 typename = enable_if_t<__exactly_once<_Tp>
1544 && is_constructible_v<_Tp,
1545 initializer_list<_Up>&, _Args...>>>
1547 variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1549 : variant(in_place_index<__index_of<_Tp>>, __il,
1550 std::forward<_Args>(__args)...)
1553 template<size_t _Np, typename... _Args,
1554 typename _Tp = __to_type<_Np>,
1555 typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
1557 variant(in_place_index_t<_Np>, _Args&&... __args)
1558 : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...)
1561 template<size_t _Np, typename _Up, typename... _Args,
1562 typename _Tp = __to_type<_Np>,
1563 typename = enable_if_t<is_constructible_v<_Tp,
1564 initializer_list<_Up>&,
1567 variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1569 : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...)
1572 template<typename _Tp>
1573 _GLIBCXX20_CONSTEXPR
1574 enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1575 && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
1576 && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
1578 operator=(_Tp&& __rhs)
1579 noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
1580 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
1582 constexpr auto __index = __accepted_index<_Tp>;
1583 if (index() == __index)
1584 std::get<__index>(*this) = std::forward<_Tp>(__rhs);
1587 using _Tj = __accepted_type<_Tp&&>;
1588 if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
1589 || !is_nothrow_move_constructible_v<_Tj>)
1590 this->emplace<__index>(std::forward<_Tp>(__rhs));
1592 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1593 // 3585. converting assignment with immovable alternative
1594 this->emplace<__index>(_Tj(std::forward<_Tp>(__rhs)));
1599 template<typename _Tp, typename... _Args>
1600 _GLIBCXX20_CONSTEXPR
1601 enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1603 emplace(_Args&&... __args)
1605 constexpr size_t __index = __index_of<_Tp>;
1606 return this->emplace<__index>(std::forward<_Args>(__args)...);
1609 template<typename _Tp, typename _Up, typename... _Args>
1610 _GLIBCXX20_CONSTEXPR
1611 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1612 && __exactly_once<_Tp>,
1614 emplace(initializer_list<_Up> __il, _Args&&... __args)
1616 constexpr size_t __index = __index_of<_Tp>;
1617 return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
1620 template<size_t _Np, typename... _Args>
1621 _GLIBCXX20_CONSTEXPR
1622 enable_if_t<is_constructible_v<__to_type<_Np>, _Args...>,
1624 emplace(_Args&&... __args)
1626 namespace __variant = std::__detail::__variant;
1627 using type = typename _Nth_type<_Np, _Types...>::type;
1628 // Provide the strong exception-safety guarantee when possible,
1629 // to avoid becoming valueless.
1630 if constexpr (is_nothrow_constructible_v<type, _Args...>)
1632 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1634 else if constexpr (is_scalar_v<type>)
1636 // This might invoke a potentially-throwing conversion operator:
1637 const type __tmp(std::forward<_Args>(__args)...);
1638 // But this won't throw:
1639 __variant::__emplace<_Np>(*this, __tmp);
1641 else if constexpr (__variant::_Never_valueless_alt<type>()
1642 && _Traits::_S_move_assign)
1644 // This construction might throw:
1645 variant __tmp(in_place_index<_Np>,
1646 std::forward<_Args>(__args)...);
1647 // But _Never_valueless_alt<type> means this won't:
1648 *this = std::move(__tmp);
1652 // This case only provides the basic exception-safety guarantee,
1653 // i.e. the variant can become valueless.
1654 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1656 return std::get<_Np>(*this);
1659 template<size_t _Np, typename _Up, typename... _Args>
1660 _GLIBCXX20_CONSTEXPR
1661 enable_if_t<is_constructible_v<__to_type<_Np>,
1662 initializer_list<_Up>&, _Args...>,
1664 emplace(initializer_list<_Up> __il, _Args&&... __args)
1666 namespace __variant = std::__detail::__variant;
1667 using type = typename _Nth_type<_Np, _Types...>::type;
1668 // Provide the strong exception-safety guarantee when possible,
1669 // to avoid becoming valueless.
1670 if constexpr (is_nothrow_constructible_v<type,
1671 initializer_list<_Up>&,
1674 __variant::__emplace<_Np>(*this, __il,
1675 std::forward<_Args>(__args)...);
1677 else if constexpr (__variant::_Never_valueless_alt<type>()
1678 && _Traits::_S_move_assign)
1680 // This construction might throw:
1681 variant __tmp(in_place_index<_Np>, __il,
1682 std::forward<_Args>(__args)...);
1683 // But _Never_valueless_alt<type> means this won't:
1684 *this = std::move(__tmp);
1688 // This case only provides the basic exception-safety guarantee,
1689 // i.e. the variant can become valueless.
1690 __variant::__emplace<_Np>(*this, __il,
1691 std::forward<_Args>(__args)...);
1693 return std::get<_Np>(*this);
1696 template<size_t _Np, typename... _Args>
1697 enable_if_t<!(_Np < sizeof...(_Types))> emplace(_Args&&...) = delete;
1699 template<typename _Tp, typename... _Args>
1700 enable_if_t<!__exactly_once<_Tp>> emplace(_Args&&...) = delete;
1702 constexpr bool valueless_by_exception() const noexcept
1703 { return !this->_M_valid(); }
1705 constexpr size_t index() const noexcept
1707 using __index_type = typename _Base::__index_type;
1708 if constexpr (__detail::__variant::__never_valueless<_Types...>())
1709 return this->_M_index;
1710 else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
1711 return make_signed_t<__index_type>(this->_M_index);
1713 return size_t(__index_type(this->_M_index + 1)) - 1;
1716 _GLIBCXX20_CONSTEXPR
1718 swap(variant& __rhs)
1719 noexcept((__is_nothrow_swappable<_Types>::value && ...)
1720 && is_nothrow_move_constructible_v<variant>)
1722 static_assert((is_move_constructible_v<_Types> && ...));
1724 // Handle this here to simplify the visitation.
1725 if (__rhs.valueless_by_exception()) [[__unlikely__]]
1727 if (!this->valueless_by_exception()) [[__likely__]]
1732 namespace __variant = __detail::__variant;
1734 __variant::__raw_idx_visit(
1735 [this, &__rhs](auto&& __rhs_mem, auto __rhs_index) mutable
1737 constexpr size_t __j = __rhs_index;
1738 if constexpr (__j != variant_npos)
1740 if (this->index() == __j)
1743 swap(std::get<__j>(*this), __rhs_mem);
1747 auto __tmp(std::move(__rhs_mem));
1749 if constexpr (_Traits::_S_trivial_move_assign)
1750 __rhs = std::move(*this);
1752 __variant::__raw_idx_visit(
1753 [&__rhs](auto&& __this_mem, auto __this_index) mutable
1755 constexpr size_t __k = __this_index;
1756 if constexpr (__k != variant_npos)
1757 __variant::__emplace<__k>(__rhs,
1758 std::move(__this_mem));
1761 __variant::__emplace<__j>(*this, std::move(__tmp));
1767 #if __cpp_lib_variant >= 202306L // >= C++26
1768 // [variant.visit], visitation
1770 /** Simple visitation for a single variant
1772 * To visit a single variant you can use `var.visit(visitor)`
1773 * instead of `std::visit(visitor, var)`.
1777 template<int = 0, typename _Self, typename _Visitor>
1778 constexpr decltype(auto)
1779 visit(this _Self&& __self, _Visitor&& __vis)
1781 using _CVar = __conditional_t<is_const_v<remove_reference_t<_Self>>,
1782 const variant, variant>;
1783 using _Var = __conditional_t<is_rvalue_reference_v<_Self&&>,
1785 return std::visit(std::forward<_Visitor>(__vis), (_Var)__self);
1788 /** Simple visitation for a single variant, with explicit return type
1790 * To visit a single variant you can use `var.visit<R>(visitor)`
1791 * instead of `std::visit<R>(visitor, var)`.
1795 template<typename _Res, typename _Self, typename _Visitor>
1797 visit(this _Self&& __self, _Visitor&& __vis)
1799 using _CVar = __conditional_t<is_const_v<remove_reference_t<_Self>>,
1800 const variant, variant>;
1801 using _Var = __conditional_t<is_rvalue_reference_v<_Self&&>,
1803 return std::visit<_Res>(std::forward<_Visitor>(__vis), (_Var)__self);
1808 template<size_t _Np, typename _Vp>
1809 friend constexpr decltype(auto)
1810 __detail::__variant::__get(_Vp&& __v) noexcept;
1813 template<size_t _Np, typename... _Types>
1814 constexpr variant_alternative_t<_Np, variant<_Types...>>&
1815 get(variant<_Types...>& __v)
1817 static_assert(_Np < sizeof...(_Types),
1818 "The index must be in [0, number of alternatives)");
1819 if (__v.index() != _Np)
1820 __throw_bad_variant_access(__v.valueless_by_exception());
1821 return __detail::__variant::__get<_Np>(__v);
1824 template<size_t _Np, typename... _Types>
1825 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1826 get(variant<_Types...>&& __v)
1828 static_assert(_Np < sizeof...(_Types),
1829 "The index must be in [0, number of alternatives)");
1830 if (__v.index() != _Np)
1831 __throw_bad_variant_access(__v.valueless_by_exception());
1832 return __detail::__variant::__get<_Np>(std::move(__v));
1835 template<size_t _Np, typename... _Types>
1836 constexpr const variant_alternative_t<_Np, variant<_Types...>>&
1837 get(const variant<_Types...>& __v)
1839 static_assert(_Np < sizeof...(_Types),
1840 "The index must be in [0, number of alternatives)");
1841 if (__v.index() != _Np)
1842 __throw_bad_variant_access(__v.valueless_by_exception());
1843 return __detail::__variant::__get<_Np>(__v);
1846 template<size_t _Np, typename... _Types>
1847 constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
1848 get(const variant<_Types...>&& __v)
1850 static_assert(_Np < sizeof...(_Types),
1851 "The index must be in [0, number of alternatives)");
1852 if (__v.index() != _Np)
1853 __throw_bad_variant_access(__v.valueless_by_exception());
1854 return __detail::__variant::__get<_Np>(std::move(__v));
1857 /// @cond undocumented
1858 template<typename _Result_type, typename _Visitor, typename... _Variants>
1859 constexpr decltype(auto)
1860 __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
1862 // Get the silly case of visiting no variants out of the way first.
1863 if constexpr (sizeof...(_Variants) == 0)
1865 if constexpr (is_void_v<_Result_type>)
1866 return (void) std::forward<_Visitor>(__visitor)();
1868 return std::forward<_Visitor>(__visitor)();
1872 constexpr size_t __max = 11; // "These go to eleven."
1874 // The type of the first variant in the pack.
1875 using _V0 = typename _Nth_type<0, _Variants...>::type;
1876 // The number of alternatives in that first variant.
1877 constexpr auto __n = variant_size_v<remove_reference_t<_V0>>;
1879 if constexpr (sizeof...(_Variants) > 1 || __n > __max)
1881 // Use a jump table for the general case.
1882 constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1883 _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1885 auto __func_ptr = __vtable._M_access(__variants.index()...);
1886 return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1887 std::forward<_Variants>(__variants)...);
1889 else // We have a single variant with a small number of alternatives.
1891 // A name for the first variant in the pack.
1893 = [](_V0& __v, ...) -> _V0& { return __v; }(__variants...);
1895 using __detail::__variant::_Multi_array;
1896 using __detail::__variant::__gen_vtable_impl;
1897 using _Ma = _Multi_array<_Result_type (*)(_Visitor&&, _V0&&)>;
1899 #ifdef _GLIBCXX_DEBUG
1900 # define _GLIBCXX_VISIT_UNREACHABLE __builtin_trap
1902 # define _GLIBCXX_VISIT_UNREACHABLE __builtin_unreachable
1905 #define _GLIBCXX_VISIT_CASE(N) \
1908 if constexpr (N < __n) \
1910 return __gen_vtable_impl<_Ma, index_sequence<N>>:: \
1911 __visit_invoke(std::forward<_Visitor>(__visitor), \
1912 std::forward<_V0>(__v0)); \
1914 else _GLIBCXX_VISIT_UNREACHABLE(); \
1917 switch (__v0.index())
1919 _GLIBCXX_VISIT_CASE(0)
1920 _GLIBCXX_VISIT_CASE(1)
1921 _GLIBCXX_VISIT_CASE(2)
1922 _GLIBCXX_VISIT_CASE(3)
1923 _GLIBCXX_VISIT_CASE(4)
1924 _GLIBCXX_VISIT_CASE(5)
1925 _GLIBCXX_VISIT_CASE(6)
1926 _GLIBCXX_VISIT_CASE(7)
1927 _GLIBCXX_VISIT_CASE(8)
1928 _GLIBCXX_VISIT_CASE(9)
1929 _GLIBCXX_VISIT_CASE(10)
1931 using __detail::__variant::__variant_idx_cookie;
1932 using __detail::__variant::__variant_cookie;
1933 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>
1934 || is_same_v<_Result_type, __variant_cookie>)
1936 using _Npos = index_sequence<variant_npos>;
1937 return __gen_vtable_impl<_Ma, _Npos>::
1938 __visit_invoke(std::forward<_Visitor>(__visitor),
1939 std::forward<_V0>(__v0));
1942 _GLIBCXX_VISIT_UNREACHABLE();
1944 _GLIBCXX_VISIT_UNREACHABLE();
1946 #undef _GLIBCXX_VISIT_CASE
1947 #undef _GLIBCXX_VISIT_UNREACHABLE
1953 template<typename _Visitor, typename... _Variants>
1954 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1955 visit(_Visitor&& __visitor, _Variants&&... __variants)
1957 namespace __variant = std::__detail::__variant;
1959 if ((__variant::__as(__variants).valueless_by_exception() || ...))
1960 __throw_bad_variant_access(2);
1963 = __detail::__variant::__visit_result_t<_Visitor, _Variants...>;
1965 using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
1967 if constexpr (sizeof...(_Variants) == 1)
1969 using _Vp = decltype(__variant::__as(std::declval<_Variants>()...));
1971 constexpr bool __visit_rettypes_match = __detail::__variant::
1972 __check_visitor_results<_Visitor, _Vp>(
1973 make_index_sequence<variant_size_v<remove_reference_t<_Vp>>>());
1974 if constexpr (!__visit_rettypes_match)
1976 static_assert(__visit_rettypes_match,
1977 "std::visit requires the visitor to have the same "
1978 "return type for all alternatives of a variant");
1982 return std::__do_visit<_Tag>(
1983 std::forward<_Visitor>(__visitor),
1984 static_cast<_Vp>(__variants)...);
1987 return std::__do_visit<_Tag>(
1988 std::forward<_Visitor>(__visitor),
1989 __variant::__as(std::forward<_Variants>(__variants))...);
1992 #if __cplusplus > 201703L
1993 template<typename _Res, typename _Visitor, typename... _Variants>
1995 visit(_Visitor&& __visitor, _Variants&&... __variants)
1997 namespace __variant = std::__detail::__variant;
1999 if ((__variant::__as(__variants).valueless_by_exception() || ...))
2000 __throw_bad_variant_access(3);
2002 return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
2003 __variant::__as(std::forward<_Variants>(__variants))...);
2007 /// @cond undocumented
2008 template<typename... _Types>
2009 struct __variant_hash
2011 #if __cplusplus < 202002L
2012 using result_type [[__deprecated__]] = size_t;
2013 using argument_type [[__deprecated__]] = variant<_Types...>;
2017 operator()(const variant<_Types...>& __t) const
2018 noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
2021 __detail::__variant::__raw_visit(
2022 [&__t, &__ret](auto&& __t_mem) mutable
2024 using _Type = __remove_cvref_t<decltype(__t_mem)>;
2025 if constexpr (!is_same_v<_Type,
2026 __detail::__variant::__variant_cookie>)
2027 __ret = std::hash<size_t>{}(__t.index())
2028 + std::hash<_Type>{}(__t_mem);
2030 __ret = std::hash<size_t>{}(__t.index());
2037 template<typename... _Types>
2038 struct hash<variant<_Types...>>
2039 : __conditional_t<(__is_hash_enabled_for<remove_const_t<_Types>> && ...),
2040 __variant_hash<_Types...>,
2041 __hash_not_enabled<variant<_Types...>>>
2042 #if ! _GLIBCXX_INLINE_VERSION
2043 , __detail::__variant::_Variant_hash_base<variant<_Types...>,
2044 index_sequence_for<_Types...>>
2048 template<typename... _Types>
2049 struct __is_fast_hash<hash<variant<_Types...>>>
2050 : bool_constant<(__is_fast_hash<_Types>::value && ...)>
2053 _GLIBCXX_END_NAMESPACE_VERSION
2056 #endif // __cpp_lib_variant
2057 #endif // _GLIBCXX_VARIANT