1 // <variant> -*- C++ -*-
3 // Copyright (C) 2016-2022 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 #pragma GCC system_header
34 #if __cplusplus >= 201703L
36 #include <initializer_list>
37 #include <type_traits>
38 #include <bits/enable_special_members.h>
39 #include <bits/exception_defines.h>
40 #include <bits/functional_hash.h>
41 #include <bits/invoke.h>
42 #include <bits/parse_numbers.h>
43 #include <bits/stl_iterator_base_types.h>
44 #include <bits/stl_iterator_base_funcs.h>
45 #include <bits/stl_construct.h>
46 #include <bits/utility.h> // in_place_index_t
47 #if __cplusplus >= 202002L
51 #if __cpp_concepts >= 202002L && __cpp_constexpr >= 201811L
52 // P2231R1 constexpr needs constexpr unions and constrained destructors.
53 # define __cpp_lib_variant 202106L
55 # include <ext/aligned_buffer.h> // Use __aligned_membuf instead of union.
56 # define __cpp_lib_variant 202102L
59 namespace std _GLIBCXX_VISIBILITY(default)
61 _GLIBCXX_BEGIN_NAMESPACE_VERSION
63 template<typename... _Types> class tuple;
64 template<typename... _Types> class variant;
65 template <typename> struct hash;
67 template<typename _Variant>
70 template<typename _Variant>
71 struct variant_size<const _Variant> : variant_size<_Variant> {};
73 template<typename _Variant>
74 struct variant_size<volatile _Variant> : variant_size<_Variant> {};
76 template<typename _Variant>
77 struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
79 template<typename... _Types>
80 struct variant_size<variant<_Types...>>
81 : std::integral_constant<size_t, sizeof...(_Types)> {};
83 template<typename _Variant>
84 inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
86 template<typename... _Types>
87 inline constexpr size_t
88 variant_size_v<variant<_Types...>> = sizeof...(_Types);
90 template<typename... _Types>
91 inline constexpr size_t
92 variant_size_v<const variant<_Types...>> = sizeof...(_Types);
94 template<size_t _Np, typename _Variant>
95 struct variant_alternative;
97 template<size_t _Np, typename... _Types>
98 struct variant_alternative<_Np, variant<_Types...>>
100 static_assert(_Np < sizeof...(_Types));
102 using type = typename _Nth_type<_Np, _Types...>::type;
105 template<size_t _Np, typename _Variant>
106 using variant_alternative_t =
107 typename variant_alternative<_Np, _Variant>::type;
109 template<size_t _Np, typename _Variant>
110 struct variant_alternative<_Np, const _Variant>
111 { using type = add_const_t<variant_alternative_t<_Np, _Variant>>; };
113 template<size_t _Np, typename _Variant>
114 struct variant_alternative<_Np, volatile _Variant>
115 { using type = add_volatile_t<variant_alternative_t<_Np, _Variant>>; };
117 template<size_t _Np, typename _Variant>
118 struct variant_alternative<_Np, const volatile _Variant>
119 { using type = add_cv_t<variant_alternative_t<_Np, _Variant>>; };
121 inline constexpr size_t variant_npos = -1;
123 template<size_t _Np, typename... _Types>
124 constexpr variant_alternative_t<_Np, variant<_Types...>>&
125 get(variant<_Types...>&);
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...>> const&
133 get(const 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<typename _Result_type, typename _Visitor, typename... _Variants>
140 constexpr decltype(auto)
141 __do_visit(_Visitor&& __visitor, _Variants&&... __variants);
143 template <typename... _Types, typename _Tp>
146 __variant_cast(_Tp&& __rhs)
148 if constexpr (is_lvalue_reference_v<_Tp>)
150 if constexpr (is_const_v<remove_reference_t<_Tp>>)
151 return static_cast<const variant<_Types...>&>(__rhs);
153 return static_cast<variant<_Types...>&>(__rhs);
156 return static_cast<variant<_Types...>&&>(__rhs);
163 // used for raw visitation
164 struct __variant_cookie {};
165 // used for raw visitation with indices passed in
166 struct __variant_idx_cookie { using type = __variant_idx_cookie; };
167 // Used to enable deduction (and same-type checking) for std::visit:
168 template<typename _Tp> struct __deduce_visit_result { using type = _Tp; };
170 // Visit variants that might be valueless.
171 template<typename _Visitor, typename... _Variants>
173 __raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
175 std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
176 std::forward<_Variants>(__variants)...);
179 // Visit variants that might be valueless, passing indices to the visitor.
180 template<typename _Visitor, typename... _Variants>
182 __raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
184 std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
185 std::forward<_Variants>(__variants)...);
188 // The __as function templates implement the exposition-only "as-variant"
190 template<typename... _Types>
191 constexpr std::variant<_Types...>&
192 __as(std::variant<_Types...>& __v) noexcept
195 template<typename... _Types>
196 constexpr const std::variant<_Types...>&
197 __as(const std::variant<_Types...>& __v) noexcept
200 template<typename... _Types>
201 constexpr std::variant<_Types...>&&
202 __as(std::variant<_Types...>&& __v) noexcept
203 { return std::move(__v); }
205 template<typename... _Types>
206 constexpr const std::variant<_Types...>&&
207 __as(const std::variant<_Types...>&& __v) noexcept
208 { return std::move(__v); }
211 // _Uninitialized<T> is guaranteed to be a trivially destructible type,
214 // _Uninitialized<T> is trivially destructible iff T is, so _Variant_union
215 // needs a constrained non-trivial destructor.
216 template<typename _Type, bool = std::is_trivially_destructible_v<_Type>>
217 struct _Uninitialized;
219 template<typename _Type>
220 struct _Uninitialized<_Type, true>
222 template<typename... _Args>
224 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
225 : _M_storage(std::forward<_Args>(__args)...)
228 constexpr const _Type& _M_get() const & noexcept
229 { return _M_storage; }
231 constexpr _Type& _M_get() & noexcept
232 { return _M_storage; }
234 constexpr const _Type&& _M_get() const && noexcept
235 { return std::move(_M_storage); }
237 constexpr _Type&& _M_get() && noexcept
238 { return std::move(_M_storage); }
243 template<typename _Type>
244 struct _Uninitialized<_Type, false>
246 #if __cpp_lib_variant >= 202106L
247 template<typename... _Args>
249 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
250 : _M_storage(std::forward<_Args>(__args)...)
253 constexpr ~_Uninitialized() { }
255 _Uninitialized(const _Uninitialized&) = default;
256 _Uninitialized(_Uninitialized&&) = default;
257 _Uninitialized& operator=(const _Uninitialized&) = default;
258 _Uninitialized& operator=(_Uninitialized&&) = default;
260 constexpr const _Type& _M_get() const & noexcept
261 { return _M_storage; }
263 constexpr _Type& _M_get() & noexcept
264 { return _M_storage; }
266 constexpr const _Type&& _M_get() const && noexcept
267 { return std::move(_M_storage); }
269 constexpr _Type&& _M_get() && noexcept
270 { return std::move(_M_storage); }
272 struct _Empty_byte { };
275 _Empty_byte _M_empty;
279 template<typename... _Args>
281 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
283 ::new ((void*)std::addressof(_M_storage))
284 _Type(std::forward<_Args>(__args)...);
287 const _Type& _M_get() const & noexcept
288 { return *_M_storage._M_ptr(); }
290 _Type& _M_get() & noexcept
291 { return *_M_storage._M_ptr(); }
293 const _Type&& _M_get() const && noexcept
294 { return std::move(*_M_storage._M_ptr()); }
296 _Type&& _M_get() && noexcept
297 { return std::move(*_M_storage._M_ptr()); }
299 __gnu_cxx::__aligned_membuf<_Type> _M_storage;
303 template<size_t _Np, typename _Union>
304 constexpr decltype(auto)
305 __get_n(_Union&& __u) noexcept
307 if constexpr (_Np == 0)
308 return std::forward<_Union>(__u)._M_first._M_get();
309 else if constexpr (_Np == 1)
310 return std::forward<_Union>(__u)._M_rest._M_first._M_get();
311 else if constexpr (_Np == 2)
312 return std::forward<_Union>(__u)._M_rest._M_rest._M_first._M_get();
314 return __variant::__get_n<_Np - 3>(
315 std::forward<_Union>(__u)._M_rest._M_rest._M_rest);
318 // Returns the typed storage for __v.
319 template<size_t _Np, typename _Variant>
320 constexpr decltype(auto)
321 __get(_Variant&& __v) noexcept
322 { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
324 template<typename... _Types>
327 static constexpr bool _S_default_ctor =
328 is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
329 static constexpr bool _S_copy_ctor =
330 (is_copy_constructible_v<_Types> && ...);
331 static constexpr bool _S_move_ctor =
332 (is_move_constructible_v<_Types> && ...);
333 static constexpr bool _S_copy_assign =
335 && (is_copy_assignable_v<_Types> && ...);
336 static constexpr bool _S_move_assign =
338 && (is_move_assignable_v<_Types> && ...);
340 static constexpr bool _S_trivial_dtor =
341 (is_trivially_destructible_v<_Types> && ...);
342 static constexpr bool _S_trivial_copy_ctor =
343 (is_trivially_copy_constructible_v<_Types> && ...);
344 static constexpr bool _S_trivial_move_ctor =
345 (is_trivially_move_constructible_v<_Types> && ...);
346 static constexpr bool _S_trivial_copy_assign =
347 _S_trivial_dtor && _S_trivial_copy_ctor
348 && (is_trivially_copy_assignable_v<_Types> && ...);
349 static constexpr bool _S_trivial_move_assign =
350 _S_trivial_dtor && _S_trivial_move_ctor
351 && (is_trivially_move_assignable_v<_Types> && ...);
353 // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
354 // are always nothrow.
355 static constexpr bool _S_nothrow_default_ctor =
356 is_nothrow_default_constructible_v<
357 typename _Nth_type<0, _Types...>::type>;
358 static constexpr bool _S_nothrow_copy_ctor = false;
359 static constexpr bool _S_nothrow_move_ctor =
360 (is_nothrow_move_constructible_v<_Types> && ...);
361 static constexpr bool _S_nothrow_copy_assign = false;
362 static constexpr bool _S_nothrow_move_assign =
364 && (is_nothrow_move_assignable_v<_Types> && ...);
367 // Defines members and ctors.
368 template<typename... _Types>
369 union _Variadic_union
371 _Variadic_union() = default;
373 template<size_t _Np, typename... _Args>
374 _Variadic_union(in_place_index_t<_Np>, _Args&&...) = delete;
377 template<typename _First, typename... _Rest>
378 union _Variadic_union<_First, _Rest...>
380 constexpr _Variadic_union() : _M_rest() { }
382 template<typename... _Args>
384 _Variadic_union(in_place_index_t<0>, _Args&&... __args)
385 : _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
388 template<size_t _Np, typename... _Args>
390 _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
391 : _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
394 #if __cpp_lib_variant >= 202106L
395 _Variadic_union(const _Variadic_union&) = default;
396 _Variadic_union(_Variadic_union&&) = default;
397 _Variadic_union& operator=(const _Variadic_union&) = default;
398 _Variadic_union& operator=(_Variadic_union&&) = default;
400 ~_Variadic_union() = default;
402 constexpr ~_Variadic_union()
403 requires (!__has_trivial_destructor(_First))
404 || (!__has_trivial_destructor(_Variadic_union<_Rest...>))
408 _Uninitialized<_First> _M_first;
409 _Variadic_union<_Rest...> _M_rest;
412 // _Never_valueless_alt is true for variant alternatives that can
413 // always be placed in a variant without it becoming valueless.
415 // For suitably-small, trivially copyable types we can create temporaries
416 // on the stack and then memcpy them into place.
417 template<typename _Tp>
418 struct _Never_valueless_alt
419 : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
422 // Specialize _Never_valueless_alt for other types which have a
423 // non-throwing and cheap move construction and move assignment operator,
424 // so that emplacing the type will provide the strong exception-safety
425 // guarantee, by creating and moving a temporary.
426 // Whether _Never_valueless_alt<T> is true or not affects the ABI of a
427 // variant using that alternative, so we can't change the value later!
429 // True if every alternative in _Types... can be emplaced in a variant
430 // without it becoming valueless. If this is true, variant<_Types...>
431 // can never be valueless, which enables some minor optimizations.
432 template <typename... _Types>
433 constexpr bool __never_valueless()
435 return _Traits<_Types...>::_S_move_assign
436 && (_Never_valueless_alt<_Types>::value && ...);
439 // Defines index and the dtor, possibly trivial.
440 template<bool __trivially_destructible, typename... _Types>
441 struct _Variant_storage;
443 template <typename... _Types>
444 using __select_index =
445 typename __select_int::_Select_int_base<sizeof...(_Types),
447 unsigned short>::type::value_type;
449 template<typename... _Types>
450 struct _Variant_storage<false, _Types...>
454 : _M_index(static_cast<__index_type>(variant_npos))
457 template<size_t _Np, typename... _Args>
459 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
460 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
467 if (!_M_valid()) [[unlikely]]
470 std::__do_visit<void>([](auto&& __this_mem) mutable
472 std::_Destroy(std::__addressof(__this_mem));
473 }, __variant_cast<_Types...>(*this));
475 _M_index = static_cast<__index_type>(variant_npos);
483 _M_valid() const noexcept
485 if constexpr (__variant::__never_valueless<_Types...>())
487 return this->_M_index != __index_type(variant_npos);
490 _Variadic_union<_Types...> _M_u;
491 using __index_type = __select_index<_Types...>;
492 __index_type _M_index;
495 template<typename... _Types>
496 struct _Variant_storage<true, _Types...>
500 : _M_index(static_cast<__index_type>(variant_npos))
503 template<size_t _Np, typename... _Args>
505 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
506 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
512 { _M_index = static_cast<__index_type>(variant_npos); }
515 _M_valid() const noexcept
517 if constexpr (__variant::__never_valueless<_Types...>())
519 // It would be nice if we could just return true for -fno-exceptions.
520 // It's possible (but inadvisable) that a std::variant could become
521 // valueless in a translation unit compiled with -fexceptions and then
522 // be passed to functions compiled with -fno-exceptions. We would need
523 // some #ifdef _GLIBCXX_NO_EXCEPTIONS_GLOBALLY property to elide all
524 // checks for valueless_by_exception().
525 return this->_M_index != static_cast<__index_type>(variant_npos);
528 _Variadic_union<_Types...> _M_u;
529 using __index_type = __select_index<_Types...>;
530 __index_type _M_index;
533 // Implementation of v.emplace<N>(args...).
534 template<size_t _Np, bool _Triv, typename... _Types, typename... _Args>
537 __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
540 auto* __addr = std::__addressof(__variant::__get_n<_Np>(__v._M_u));
541 std::_Construct(__addr, std::forward<_Args>(__args)...);
542 // Construction didn't throw, so can set the new index now:
546 template<typename... _Types>
547 using _Variant_storage_alias =
548 _Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
550 // The following are (Copy|Move) (ctor|assign) layers for forwarding
551 // triviality and handling non-trivial SMF behaviors.
553 template<bool, typename... _Types>
554 struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
556 using _Base = _Variant_storage_alias<_Types...>;
560 _Copy_ctor_base(const _Copy_ctor_base& __rhs)
561 noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
563 __variant::__raw_idx_visit(
564 [this](auto&& __rhs_mem, auto __rhs_index) mutable
566 constexpr size_t __j = __rhs_index;
567 if constexpr (__j != variant_npos)
568 std::_Construct(std::__addressof(this->_M_u),
569 in_place_index<__j>, __rhs_mem);
570 }, __variant_cast<_Types...>(__rhs));
571 this->_M_index = __rhs._M_index;
574 _Copy_ctor_base(_Copy_ctor_base&&) = default;
575 _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
576 _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
579 template<typename... _Types>
580 struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
582 using _Base = _Variant_storage_alias<_Types...>;
586 template<typename... _Types>
587 using _Copy_ctor_alias =
588 _Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
590 template<bool, typename... _Types>
591 struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
593 using _Base = _Copy_ctor_alias<_Types...>;
597 _Move_ctor_base(_Move_ctor_base&& __rhs)
598 noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
600 __variant::__raw_idx_visit(
601 [this](auto&& __rhs_mem, auto __rhs_index) mutable
603 constexpr size_t __j = __rhs_index;
604 if constexpr (__j != variant_npos)
605 std::_Construct(std::__addressof(this->_M_u),
607 std::forward<decltype(__rhs_mem)>(__rhs_mem));
608 }, __variant_cast<_Types...>(std::move(__rhs)));
609 this->_M_index = __rhs._M_index;
612 _Move_ctor_base(const _Move_ctor_base&) = default;
613 _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
614 _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
617 template<typename... _Types>
618 struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
620 using _Base = _Copy_ctor_alias<_Types...>;
624 template<typename... _Types>
625 using _Move_ctor_alias =
626 _Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
628 template<bool, typename... _Types>
629 struct _Copy_assign_base : _Move_ctor_alias<_Types...>
631 using _Base = _Move_ctor_alias<_Types...>;
636 operator=(const _Copy_assign_base& __rhs)
637 noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
639 __variant::__raw_idx_visit(
640 [this](auto&& __rhs_mem, auto __rhs_index) mutable
642 constexpr size_t __j = __rhs_index;
643 if constexpr (__j == variant_npos)
644 this->_M_reset(); // Make *this valueless.
645 else if (this->_M_index == __j)
646 __variant::__get<__j>(*this) = __rhs_mem;
649 using _Tj = typename _Nth_type<__j, _Types...>::type;
650 if constexpr (is_nothrow_copy_constructible_v<_Tj>
651 || !is_nothrow_move_constructible_v<_Tj>)
652 __variant::__emplace<__j>(*this, __rhs_mem);
655 using _Variant = variant<_Types...>;
656 _Variant& __self = __variant_cast<_Types...>(*this);
657 __self = _Variant(in_place_index<__j>, __rhs_mem);
660 }, __variant_cast<_Types...>(__rhs));
664 _Copy_assign_base(const _Copy_assign_base&) = default;
665 _Copy_assign_base(_Copy_assign_base&&) = default;
666 _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
669 template<typename... _Types>
670 struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
672 using _Base = _Move_ctor_alias<_Types...>;
676 template<typename... _Types>
677 using _Copy_assign_alias =
678 _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
680 template<bool, typename... _Types>
681 struct _Move_assign_base : _Copy_assign_alias<_Types...>
683 using _Base = _Copy_assign_alias<_Types...>;
688 operator=(_Move_assign_base&& __rhs)
689 noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
691 __variant::__raw_idx_visit(
692 [this](auto&& __rhs_mem, auto __rhs_index) mutable
694 constexpr size_t __j = __rhs_index;
695 if constexpr (__j != variant_npos)
697 if (this->_M_index == __j)
698 __variant::__get<__j>(*this) = std::move(__rhs_mem);
701 using _Tj = typename _Nth_type<__j, _Types...>::type;
702 if constexpr (is_nothrow_move_constructible_v<_Tj>)
703 __variant::__emplace<__j>(*this, std::move(__rhs_mem));
706 using _Variant = variant<_Types...>;
707 _Variant& __self = __variant_cast<_Types...>(*this);
708 __self.template emplace<__j>(std::move(__rhs_mem));
714 }, __variant_cast<_Types...>(__rhs));
718 _Move_assign_base(const _Move_assign_base&) = default;
719 _Move_assign_base(_Move_assign_base&&) = default;
720 _Move_assign_base& operator=(const _Move_assign_base&) = default;
723 template<typename... _Types>
724 struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
726 using _Base = _Copy_assign_alias<_Types...>;
730 template<typename... _Types>
731 using _Move_assign_alias =
732 _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
734 template<typename... _Types>
735 struct _Variant_base : _Move_assign_alias<_Types...>
737 using _Base = _Move_assign_alias<_Types...>;
740 _Variant_base() noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
741 : _Variant_base(in_place_index<0>) { }
743 template<size_t _Np, typename... _Args>
745 _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
746 : _Base(__i, std::forward<_Args>(__args)...)
749 _Variant_base(const _Variant_base&) = default;
750 _Variant_base(_Variant_base&&) = default;
751 _Variant_base& operator=(const _Variant_base&) = default;
752 _Variant_base& operator=(_Variant_base&&) = default;
755 template<typename _Tp, typename... _Types>
756 inline constexpr bool __exactly_once
757 = std::__find_uniq_type_in_pack<_Tp, _Types...>() < sizeof...(_Types);
759 // Helper used to check for valid conversions that don't involve narrowing.
760 template<typename _Ti> struct _Arr { _Ti _M_x[1]; };
762 // "Build an imaginary function FUN(Ti) for each alternative type Ti"
763 template<size_t _Ind, typename _Tp, typename _Ti, typename = void>
766 // This function means 'using _Build_FUN<I, T, Ti>::_S_fun;' is valid,
767 // but only static functions will be considered in the call below.
771 // "... for which Ti x[] = {std::forward<T>(t)}; is well-formed."
772 template<size_t _Ind, typename _Tp, typename _Ti>
773 struct _Build_FUN<_Ind, _Tp, _Ti,
774 void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
776 // This is the FUN function for type _Ti, with index _Ind
777 static integral_constant<size_t, _Ind> _S_fun(_Ti);
780 template<typename _Tp, typename _Variant,
781 typename = make_index_sequence<variant_size_v<_Variant>>>
784 template<typename _Tp, typename... _Ti, size_t... _Ind>
785 struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
786 : _Build_FUN<_Ind, _Tp, _Ti>...
788 using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
791 // The index j of the overload FUN(Tj) selected by overload resolution
792 // for FUN(std::forward<_Tp>(t))
793 template<typename _Tp, typename _Variant>
795 = decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
797 // The index selected for FUN(std::forward<T>(t)), or variant_npos if none.
798 template<typename _Tp, typename _Variant, typename = void>
799 struct __accepted_index
800 : integral_constant<size_t, variant_npos>
803 template<typename _Tp, typename _Variant>
804 struct __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
805 : _FUN_type<_Tp, _Variant>
808 template <typename _Maybe_variant_cookie, typename _Variant>
809 struct _Extra_visit_slot_needed
811 template <typename> struct _Variant_never_valueless;
813 template <typename... _Types>
814 struct _Variant_never_valueless<variant<_Types...>>
815 : bool_constant<__variant::__never_valueless<_Types...>()> {};
817 static constexpr bool value =
818 (is_same_v<_Maybe_variant_cookie, __variant_cookie>
819 || is_same_v<_Maybe_variant_cookie, __variant_idx_cookie>)
820 && !_Variant_never_valueless<__remove_cvref_t<_Variant>>::value;
823 // Used for storing a multi-dimensional vtable.
824 template<typename _Tp, size_t... _Dimensions>
827 // Partial specialization with rank zero, stores a single _Tp element.
828 template<typename _Tp>
829 struct _Multi_array<_Tp>
832 struct __untag_result
834 { using element_type = _Tp; };
836 template <typename... _Args>
837 struct __untag_result<const void(*)(_Args...)>
839 { using element_type = void(*)(_Args...); };
841 template <typename... _Args>
842 struct __untag_result<__variant_cookie(*)(_Args...)>
844 { using element_type = void(*)(_Args...); };
846 template <typename... _Args>
847 struct __untag_result<__variant_idx_cookie(*)(_Args...)>
849 { using element_type = void(*)(_Args...); };
851 template <typename _Res, typename... _Args>
852 struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
854 { using element_type = _Res(*)(_Args...); };
856 using __result_is_deduced = __untag_result<_Tp>;
858 constexpr const typename __untag_result<_Tp>::element_type&
862 typename __untag_result<_Tp>::element_type _M_data;
865 // Partial specialization with rank >= 1.
866 template<typename _Ret,
868 typename... _Variants,
869 size_t __first, size_t... __rest>
870 struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
872 static constexpr size_t __index =
873 sizeof...(_Variants) - sizeof...(__rest) - 1;
875 using _Variant = typename _Nth_type<__index, _Variants...>::type;
877 static constexpr int __do_cookie =
878 _Extra_visit_slot_needed<_Ret, _Variant>::value ? 1 : 0;
880 using _Tp = _Ret(*)(_Visitor, _Variants...);
882 template<typename... _Args>
883 constexpr decltype(auto)
884 _M_access(size_t __first_index, _Args... __rest_indices) const
886 return _M_arr[__first_index + __do_cookie]
887 ._M_access(__rest_indices...);
890 _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
893 // Creates a multi-dimensional vtable recursively.
896 // visit([](auto, auto){},
897 // variant<int, char>(), // typedef'ed as V1
898 // variant<float, double, long double>()) // typedef'ed as V2
899 // will trigger instantiations of:
900 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
901 // tuple<V1&&, V2&&>, std::index_sequence<>>
902 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
903 // tuple<V1&&, V2&&>, std::index_sequence<0>>
904 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
905 // tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
906 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
907 // tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
908 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
909 // tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
910 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
911 // tuple<V1&&, V2&&>, std::index_sequence<1>>
912 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
913 // tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
914 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
915 // tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
916 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
917 // tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
918 // The returned multi-dimensional vtable can be fast accessed by the visitor
919 // using index calculation.
920 template<typename _Array_type, typename _Index_seq>
921 struct __gen_vtable_impl;
923 // Defines the _S_apply() member that returns a _Multi_array populated
924 // with function pointers that perform the visitation expressions e(m)
925 // for each valid pack of indexes into the variant types _Variants.
927 // This partial specialization builds up the index sequences by recursively
928 // calling _S_apply() on the next specialization of __gen_vtable_impl.
929 // The base case of the recursion defines the actual function pointers.
930 template<typename _Result_type, typename _Visitor, size_t... __dimensions,
931 typename... _Variants, size_t... __indices>
932 struct __gen_vtable_impl<
933 _Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
934 std::index_sequence<__indices...>>
937 remove_reference_t<typename _Nth_type<sizeof...(__indices),
938 _Variants...>::type>;
940 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
943 static constexpr _Array_type
946 _Array_type __vtable{};
948 __vtable, make_index_sequence<variant_size_v<_Next>>());
952 template<size_t... __var_indices>
953 static constexpr void
954 _S_apply_all_alts(_Array_type& __vtable,
955 std::index_sequence<__var_indices...>)
957 if constexpr (_Extra_visit_slot_needed<_Result_type, _Next>::value)
958 (_S_apply_single_alt<true, __var_indices>(
959 __vtable._M_arr[__var_indices + 1],
960 &(__vtable._M_arr[0])), ...);
962 (_S_apply_single_alt<false, __var_indices>(
963 __vtable._M_arr[__var_indices]), ...);
966 template<bool __do_cookie, size_t __index, typename _Tp>
967 static constexpr void
968 _S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
970 if constexpr (__do_cookie)
972 __element = __gen_vtable_impl<
974 std::index_sequence<__indices..., __index>>::_S_apply();
975 *__cookie_element = __gen_vtable_impl<
977 std::index_sequence<__indices..., variant_npos>>::_S_apply();
981 auto __tmp_element = __gen_vtable_impl<
982 remove_reference_t<decltype(__element)>,
983 std::index_sequence<__indices..., __index>>::_S_apply();
984 static_assert(is_same_v<_Tp, decltype(__tmp_element)>,
985 "std::visit requires the visitor to have the same "
986 "return type for all alternatives of a variant");
987 __element = __tmp_element;
992 // This partial specialization is the base case for the recursion.
993 // It populates a _Multi_array element with the address of a function
994 // that invokes the visitor with the alternatives specified by __indices.
995 template<typename _Result_type, typename _Visitor, typename... _Variants,
997 struct __gen_vtable_impl<
998 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
999 std::index_sequence<__indices...>>
1002 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
1004 template<size_t __index, typename _Variant>
1005 static constexpr decltype(auto)
1006 __element_by_index_or_cookie(_Variant&& __var) noexcept
1008 if constexpr (__index != variant_npos)
1009 return __variant::__get<__index>(std::forward<_Variant>(__var));
1011 return __variant_cookie{};
1014 static constexpr decltype(auto)
1015 __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
1017 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
1018 // For raw visitation using indices, pass the indices to the visitor
1019 // and discard the return value:
1020 std::__invoke(std::forward<_Visitor>(__visitor),
1021 __element_by_index_or_cookie<__indices>(
1022 std::forward<_Variants>(__vars))...,
1023 integral_constant<size_t, __indices>()...);
1024 else if constexpr (is_same_v<_Result_type, __variant_cookie>)
1025 // For raw visitation without indices, and discard the return value:
1026 std::__invoke(std::forward<_Visitor>(__visitor),
1027 __element_by_index_or_cookie<__indices>(
1028 std::forward<_Variants>(__vars))...);
1029 else if constexpr (_Array_type::__result_is_deduced::value)
1030 // For the usual std::visit case deduce the return value:
1031 return std::__invoke(std::forward<_Visitor>(__visitor),
1032 __element_by_index_or_cookie<__indices>(
1033 std::forward<_Variants>(__vars))...);
1034 else // for std::visit<R> use INVOKE<R>
1035 return std::__invoke_r<_Result_type>(
1036 std::forward<_Visitor>(__visitor),
1037 __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
1040 static constexpr auto
1043 if constexpr (_Array_type::__result_is_deduced::value)
1045 constexpr bool __visit_ret_type_mismatch =
1046 !is_same_v<typename _Result_type::type,
1047 decltype(__visit_invoke(std::declval<_Visitor>(),
1048 std::declval<_Variants>()...))>;
1049 if constexpr (__visit_ret_type_mismatch)
1051 struct __cannot_match {};
1052 return __cannot_match{};
1055 return _Array_type{&__visit_invoke};
1058 return _Array_type{&__visit_invoke};
1062 template<typename _Result_type, typename _Visitor, typename... _Variants>
1066 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
1067 variant_size_v<remove_reference_t<_Variants>>...>;
1069 static constexpr _Array_type _S_vtable
1070 = __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
1073 template<size_t _Np, typename _Tp>
1074 struct _Base_dedup : public _Tp { };
1076 template<typename _Variant, typename __indices>
1077 struct _Variant_hash_base;
1079 template<typename... _Types, size_t... __indices>
1080 struct _Variant_hash_base<variant<_Types...>,
1081 std::index_sequence<__indices...>>
1082 : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
1084 // Equivalent to decltype(get<_Np>(as-variant(declval<_Variant>())))
1085 template<size_t _Np, typename _Variant,
1086 typename _AsV = decltype(__variant::__as(std::declval<_Variant>())),
1087 typename _Tp = variant_alternative_t<_Np, remove_reference_t<_AsV>>>
1089 = __conditional_t<is_lvalue_reference_v<_Variant>, _Tp&, _Tp&&>;
1091 // Return type of std::visit.
1092 template<typename _Visitor, typename... _Variants>
1093 using __visit_result_t
1094 = invoke_result_t<_Visitor, __get_t<0, _Variants>...>;
1096 template<typename _Tp, typename... _Types>
1097 constexpr inline bool __same_types = (is_same_v<_Tp, _Types> && ...);
1099 template <typename _Visitor, typename _Variant, size_t... _Idxs>
1100 constexpr bool __check_visitor_results(std::index_sequence<_Idxs...>)
1102 return __same_types<
1103 invoke_result_t<_Visitor, __get_t<_Idxs, _Variant>>...
1107 } // namespace __variant
1108 } // namespace __detail
1110 template<typename _Tp, typename... _Types>
1112 holds_alternative(const variant<_Types...>& __v) noexcept
1114 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1115 "T must occur exactly once in alternatives");
1116 return __v.index() == std::__find_uniq_type_in_pack<_Tp, _Types...>();
1119 template<typename _Tp, typename... _Types>
1121 get(variant<_Types...>& __v)
1123 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1124 "T must occur exactly once in alternatives");
1125 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1126 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1127 return std::get<__n>(__v);
1130 template<typename _Tp, typename... _Types>
1132 get(variant<_Types...>&& __v)
1134 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1135 "T must occur exactly once in alternatives");
1136 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1137 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1138 return std::get<__n>(std::move(__v));
1141 template<typename _Tp, typename... _Types>
1142 constexpr const _Tp&
1143 get(const variant<_Types...>& __v)
1145 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1146 "T must occur exactly once in alternatives");
1147 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1148 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1149 return std::get<__n>(__v);
1152 template<typename _Tp, typename... _Types>
1153 constexpr const _Tp&&
1154 get(const variant<_Types...>&& __v)
1156 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1157 "T must occur exactly once in alternatives");
1158 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1159 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1160 return std::get<__n>(std::move(__v));
1163 template<size_t _Np, typename... _Types>
1164 constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
1165 get_if(variant<_Types...>* __ptr) noexcept
1167 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1168 static_assert(_Np < sizeof...(_Types),
1169 "The index must be in [0, number of alternatives)");
1170 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1171 if (__ptr && __ptr->index() == _Np)
1172 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1176 template<size_t _Np, typename... _Types>
1178 add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
1179 get_if(const variant<_Types...>* __ptr) noexcept
1181 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1182 static_assert(_Np < sizeof...(_Types),
1183 "The index must be in [0, number of alternatives)");
1184 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1185 if (__ptr && __ptr->index() == _Np)
1186 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1190 template<typename _Tp, typename... _Types>
1191 constexpr add_pointer_t<_Tp>
1192 get_if(variant<_Types...>* __ptr) noexcept
1194 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1195 "T must occur exactly once in alternatives");
1196 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1197 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1198 return std::get_if<__n>(__ptr);
1201 template<typename _Tp, typename... _Types>
1202 constexpr add_pointer_t<const _Tp>
1203 get_if(const variant<_Types...>* __ptr) noexcept
1205 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1206 "T must occur exactly once in alternatives");
1207 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1208 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1209 return std::get_if<__n>(__ptr);
1212 struct monostate { };
1214 #define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
1215 template<typename... _Types> \
1216 constexpr bool operator __OP(const variant<_Types...>& __lhs, \
1217 const variant<_Types...>& __rhs) \
1219 bool __ret = true; \
1220 __detail::__variant::__raw_idx_visit( \
1221 [&__ret, &__lhs] (auto&& __rhs_mem, auto __rhs_index) mutable \
1223 if constexpr (__rhs_index != variant_npos) \
1225 if (__lhs.index() == __rhs_index) \
1227 auto& __this_mem = std::get<__rhs_index>(__lhs); \
1228 __ret = __this_mem __OP __rhs_mem; \
1231 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1234 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1239 _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
1240 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
1241 _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
1242 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
1243 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
1244 _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
1246 #undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1248 constexpr bool operator==(monostate, monostate) noexcept { return true; }
1250 #ifdef __cpp_lib_three_way_comparison
1251 template<typename... _Types>
1252 requires (three_way_comparable<_Types> && ...)
1254 common_comparison_category_t<compare_three_way_result_t<_Types>...>
1255 operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
1257 common_comparison_category_t<compare_three_way_result_t<_Types>...> __ret
1258 = strong_ordering::equal;
1260 __detail::__variant::__raw_idx_visit(
1261 [&__ret, &__v] (auto&& __w_mem, auto __w_index) mutable
1263 if constexpr (__w_index != variant_npos)
1265 if (__v.index() == __w_index)
1267 auto& __this_mem = std::get<__w_index>(__v);
1268 __ret = __this_mem <=> __w_mem;
1272 __ret = (__v.index() + 1) <=> (__w_index + 1);
1277 constexpr strong_ordering
1278 operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
1280 constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1281 constexpr bool operator<(monostate, monostate) noexcept { return false; }
1282 constexpr bool operator>(monostate, monostate) noexcept { return false; }
1283 constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1284 constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1287 template<typename _Visitor, typename... _Variants>
1288 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1289 visit(_Visitor&&, _Variants&&...);
1291 template<typename... _Types>
1292 _GLIBCXX20_CONSTEXPR
1293 inline enable_if_t<(is_move_constructible_v<_Types> && ...)
1294 && (is_swappable_v<_Types> && ...)>
1295 swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1296 noexcept(noexcept(__lhs.swap(__rhs)))
1297 { __lhs.swap(__rhs); }
1299 template<typename... _Types>
1300 enable_if_t<!((is_move_constructible_v<_Types> && ...)
1301 && (is_swappable_v<_Types> && ...))>
1302 swap(variant<_Types...>&, variant<_Types...>&) = delete;
1304 class bad_variant_access : public exception
1307 bad_variant_access() noexcept { }
1309 const char* what() const noexcept override
1310 { return _M_reason; }
1313 bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
1315 // Must point to a string with static storage duration:
1316 const char* _M_reason = "bad variant access";
1318 friend void __throw_bad_variant_access(const char* __what);
1321 // Must only be called with a string literal
1323 __throw_bad_variant_access(const char* __what)
1324 { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
1327 __throw_bad_variant_access(bool __valueless)
1329 if (__valueless) [[__unlikely__]]
1330 __throw_bad_variant_access("std::get: variant is valueless");
1332 __throw_bad_variant_access("std::get: wrong index for variant");
1335 template<typename... _Types>
1337 : private __detail::__variant::_Variant_base<_Types...>,
1338 private _Enable_default_constructor<
1339 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1340 variant<_Types...>>,
1341 private _Enable_copy_move<
1342 __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1343 __detail::__variant::_Traits<_Types...>::_S_copy_assign,
1344 __detail::__variant::_Traits<_Types...>::_S_move_ctor,
1345 __detail::__variant::_Traits<_Types...>::_S_move_assign,
1349 template <typename... _UTypes, typename _Tp>
1350 friend _GLIBCXX20_CONSTEXPR decltype(auto)
1351 __variant_cast(_Tp&&);
1353 static_assert(sizeof...(_Types) > 0,
1354 "variant must have at least one alternative");
1355 static_assert(!(std::is_reference_v<_Types> || ...),
1356 "variant must have no reference alternative");
1357 static_assert(!(std::is_void_v<_Types> || ...),
1358 "variant must have no void alternative");
1360 using _Base = __detail::__variant::_Variant_base<_Types...>;
1361 using _Default_ctor_enabler =
1362 _Enable_default_constructor<
1363 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1364 variant<_Types...>>;
1366 template<typename _Tp>
1367 static constexpr bool __not_self
1368 = !is_same_v<__remove_cvref_t<_Tp>, variant>;
1370 template<typename _Tp>
1371 static constexpr bool
1372 __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1374 template<typename _Tp>
1375 static constexpr size_t __accepted_index
1376 = __detail::__variant::__accepted_index<_Tp, variant>::value;
1378 template<size_t _Np, typename = enable_if_t<(_Np < sizeof...(_Types))>>
1379 using __to_type = typename _Nth_type<_Np, _Types...>::type;
1381 template<typename _Tp, typename = enable_if_t<__not_self<_Tp>>>
1382 using __accepted_type = __to_type<__accepted_index<_Tp>>;
1384 template<typename _Tp>
1385 static constexpr size_t __index_of
1386 = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1388 using _Traits = __detail::__variant::_Traits<_Types...>;
1390 template<typename _Tp>
1391 struct __is_in_place_tag : false_type { };
1392 template<typename _Tp>
1393 struct __is_in_place_tag<in_place_type_t<_Tp>> : true_type { };
1394 template<size_t _Np>
1395 struct __is_in_place_tag<in_place_index_t<_Np>> : true_type { };
1397 template<typename _Tp>
1398 static constexpr bool __not_in_place_tag
1399 = !__is_in_place_tag<__remove_cvref_t<_Tp>>::value;
1402 variant() = default;
1403 variant(const variant& __rhs) = default;
1404 variant(variant&&) = default;
1405 variant& operator=(const variant&) = default;
1406 variant& operator=(variant&&) = default;
1407 _GLIBCXX20_CONSTEXPR ~variant() = default;
1409 template<typename _Tp,
1410 typename = enable_if_t<sizeof...(_Types) != 0>,
1411 typename = enable_if_t<__not_in_place_tag<_Tp>>,
1412 typename _Tj = __accepted_type<_Tp&&>,
1413 typename = enable_if_t<__exactly_once<_Tj>
1414 && is_constructible_v<_Tj, _Tp>>>
1417 noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
1418 : variant(in_place_index<__accepted_index<_Tp>>,
1419 std::forward<_Tp>(__t))
1422 template<typename _Tp, typename... _Args,
1423 typename = enable_if_t<__exactly_once<_Tp>
1424 && is_constructible_v<_Tp, _Args...>>>
1426 variant(in_place_type_t<_Tp>, _Args&&... __args)
1427 : variant(in_place_index<__index_of<_Tp>>,
1428 std::forward<_Args>(__args)...)
1431 template<typename _Tp, typename _Up, typename... _Args,
1432 typename = enable_if_t<__exactly_once<_Tp>
1433 && is_constructible_v<_Tp,
1434 initializer_list<_Up>&, _Args...>>>
1436 variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1438 : variant(in_place_index<__index_of<_Tp>>, __il,
1439 std::forward<_Args>(__args)...)
1442 template<size_t _Np, typename... _Args,
1443 typename _Tp = __to_type<_Np>,
1444 typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
1446 variant(in_place_index_t<_Np>, _Args&&... __args)
1447 : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
1448 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1451 template<size_t _Np, typename _Up, typename... _Args,
1452 typename _Tp = __to_type<_Np>,
1453 typename = enable_if_t<is_constructible_v<_Tp,
1454 initializer_list<_Up>&,
1457 variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1459 : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
1460 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1463 template<typename _Tp>
1464 _GLIBCXX20_CONSTEXPR
1465 enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1466 && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
1467 && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
1469 operator=(_Tp&& __rhs)
1470 noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
1471 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
1473 constexpr auto __index = __accepted_index<_Tp>;
1474 if (index() == __index)
1475 std::get<__index>(*this) = std::forward<_Tp>(__rhs);
1478 using _Tj = __accepted_type<_Tp&&>;
1479 if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
1480 || !is_nothrow_move_constructible_v<_Tj>)
1481 this->emplace<__index>(std::forward<_Tp>(__rhs));
1483 operator=(variant(std::forward<_Tp>(__rhs)));
1488 template<typename _Tp, typename... _Args>
1489 _GLIBCXX20_CONSTEXPR
1490 enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1492 emplace(_Args&&... __args)
1494 constexpr size_t __index = __index_of<_Tp>;
1495 return this->emplace<__index>(std::forward<_Args>(__args)...);
1498 template<typename _Tp, typename _Up, typename... _Args>
1499 _GLIBCXX20_CONSTEXPR
1500 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1501 && __exactly_once<_Tp>,
1503 emplace(initializer_list<_Up> __il, _Args&&... __args)
1505 constexpr size_t __index = __index_of<_Tp>;
1506 return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
1509 template<size_t _Np, typename... _Args>
1510 _GLIBCXX20_CONSTEXPR
1511 enable_if_t<is_constructible_v<__to_type<_Np>, _Args...>,
1513 emplace(_Args&&... __args)
1515 namespace __variant = std::__detail::__variant;
1516 using type = typename _Nth_type<_Np, _Types...>::type;
1517 // Provide the strong exception-safety guarantee when possible,
1518 // to avoid becoming valueless.
1519 if constexpr (is_nothrow_constructible_v<type, _Args...>)
1521 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1523 else if constexpr (is_scalar_v<type>)
1525 // This might invoke a potentially-throwing conversion operator:
1526 const type __tmp(std::forward<_Args>(__args)...);
1527 // But this won't throw:
1528 __variant::__emplace<_Np>(*this, __tmp);
1530 else if constexpr (__variant::_Never_valueless_alt<type>()
1531 && _Traits::_S_move_assign)
1533 // This construction might throw:
1534 variant __tmp(in_place_index<_Np>,
1535 std::forward<_Args>(__args)...);
1536 // But _Never_valueless_alt<type> means this won't:
1537 *this = std::move(__tmp);
1541 // This case only provides the basic exception-safety guarantee,
1542 // i.e. the variant can become valueless.
1543 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1545 return std::get<_Np>(*this);
1548 template<size_t _Np, typename _Up, typename... _Args>
1549 _GLIBCXX20_CONSTEXPR
1550 enable_if_t<is_constructible_v<__to_type<_Np>,
1551 initializer_list<_Up>&, _Args...>,
1553 emplace(initializer_list<_Up> __il, _Args&&... __args)
1555 namespace __variant = std::__detail::__variant;
1556 using type = typename _Nth_type<_Np, _Types...>::type;
1557 // Provide the strong exception-safety guarantee when possible,
1558 // to avoid becoming valueless.
1559 if constexpr (is_nothrow_constructible_v<type,
1560 initializer_list<_Up>&,
1563 __variant::__emplace<_Np>(*this, __il,
1564 std::forward<_Args>(__args)...);
1566 else if constexpr (__variant::_Never_valueless_alt<type>()
1567 && _Traits::_S_move_assign)
1569 // This construction might throw:
1570 variant __tmp(in_place_index<_Np>, __il,
1571 std::forward<_Args>(__args)...);
1572 // But _Never_valueless_alt<type> means this won't:
1573 *this = std::move(__tmp);
1577 // This case only provides the basic exception-safety guarantee,
1578 // i.e. the variant can become valueless.
1579 __variant::__emplace<_Np>(*this, __il,
1580 std::forward<_Args>(__args)...);
1582 return std::get<_Np>(*this);
1585 template<size_t _Np, typename... _Args>
1586 enable_if_t<!(_Np < sizeof...(_Types))> emplace(_Args&&...) = delete;
1588 template<typename _Tp, typename... _Args>
1589 enable_if_t<!__exactly_once<_Tp>> emplace(_Args&&...) = delete;
1591 constexpr bool valueless_by_exception() const noexcept
1592 { return !this->_M_valid(); }
1594 constexpr size_t index() const noexcept
1596 using __index_type = typename _Base::__index_type;
1597 if constexpr (__detail::__variant::__never_valueless<_Types...>())
1598 return this->_M_index;
1599 else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
1600 return make_signed_t<__index_type>(this->_M_index);
1602 return size_t(__index_type(this->_M_index + 1)) - 1;
1605 _GLIBCXX20_CONSTEXPR
1607 swap(variant& __rhs)
1608 noexcept((__is_nothrow_swappable<_Types>::value && ...)
1609 && is_nothrow_move_constructible_v<variant>)
1611 static_assert((is_move_constructible_v<_Types> && ...));
1613 // Handle this here to simplify the visitation.
1614 if (__rhs.valueless_by_exception()) [[__unlikely__]]
1616 if (!this->valueless_by_exception()) [[__likely__]]
1621 namespace __variant = __detail::__variant;
1623 __variant::__raw_idx_visit(
1624 [this, &__rhs](auto&& __rhs_mem, auto __rhs_index) mutable
1626 constexpr size_t __j = __rhs_index;
1627 if constexpr (__j != variant_npos)
1629 if (this->index() == __j)
1632 swap(std::get<__j>(*this), __rhs_mem);
1636 auto __tmp(std::move(__rhs_mem));
1638 if constexpr (_Traits::_S_trivial_move_assign)
1639 __rhs = std::move(*this);
1641 __variant::__raw_idx_visit(
1642 [&__rhs](auto&& __this_mem, auto __this_index) mutable
1644 constexpr size_t __k = __this_index;
1645 if constexpr (__k != variant_npos)
1646 __variant::__emplace<__k>(__rhs,
1647 std::move(__this_mem));
1650 __variant::__emplace<__j>(*this, std::move(__tmp));
1656 #if defined(__clang__) && __clang_major__ <= 7
1658 using _Base::_M_u; // See https://bugs.llvm.org/show_bug.cgi?id=31852
1662 template<size_t _Np, typename _Vp>
1663 friend constexpr decltype(auto)
1664 __detail::__variant::__get(_Vp&& __v) noexcept;
1666 #define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
1667 template<typename... _Tp> \
1668 friend constexpr bool \
1669 operator __OP(const variant<_Tp...>& __lhs, \
1670 const variant<_Tp...>& __rhs);
1672 _VARIANT_RELATION_FUNCTION_TEMPLATE(<)
1673 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
1674 _VARIANT_RELATION_FUNCTION_TEMPLATE(==)
1675 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
1676 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
1677 _VARIANT_RELATION_FUNCTION_TEMPLATE(>)
1679 #undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1682 template<size_t _Np, typename... _Types>
1683 constexpr variant_alternative_t<_Np, variant<_Types...>>&
1684 get(variant<_Types...>& __v)
1686 static_assert(_Np < sizeof...(_Types),
1687 "The index must be in [0, number of alternatives)");
1688 if (__v.index() != _Np)
1689 __throw_bad_variant_access(__v.valueless_by_exception());
1690 return __detail::__variant::__get<_Np>(__v);
1693 template<size_t _Np, typename... _Types>
1694 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1695 get(variant<_Types...>&& __v)
1697 static_assert(_Np < sizeof...(_Types),
1698 "The index must be in [0, number of alternatives)");
1699 if (__v.index() != _Np)
1700 __throw_bad_variant_access(__v.valueless_by_exception());
1701 return __detail::__variant::__get<_Np>(std::move(__v));
1704 template<size_t _Np, typename... _Types>
1705 constexpr const variant_alternative_t<_Np, variant<_Types...>>&
1706 get(const variant<_Types...>& __v)
1708 static_assert(_Np < sizeof...(_Types),
1709 "The index must be in [0, number of alternatives)");
1710 if (__v.index() != _Np)
1711 __throw_bad_variant_access(__v.valueless_by_exception());
1712 return __detail::__variant::__get<_Np>(__v);
1715 template<size_t _Np, typename... _Types>
1716 constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
1717 get(const variant<_Types...>&& __v)
1719 static_assert(_Np < sizeof...(_Types),
1720 "The index must be in [0, number of alternatives)");
1721 if (__v.index() != _Np)
1722 __throw_bad_variant_access(__v.valueless_by_exception());
1723 return __detail::__variant::__get<_Np>(std::move(__v));
1726 /// @cond undocumented
1727 template<typename _Result_type, typename _Visitor, typename... _Variants>
1728 constexpr decltype(auto)
1729 __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
1731 // Get the silly case of visiting no variants out of the way first.
1732 if constexpr (sizeof...(_Variants) == 0)
1733 return std::forward<_Visitor>(__visitor)();
1736 constexpr size_t __max = 11; // "These go to eleven."
1738 // The type of the first variant in the pack.
1739 using _V0 = typename _Nth_type<0, _Variants...>::type;
1740 // The number of alternatives in that first variant.
1741 constexpr auto __n = variant_size_v<remove_reference_t<_V0>>;
1743 if constexpr (sizeof...(_Variants) > 1 || __n > __max)
1745 // Use a jump table for the general case.
1746 constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1747 _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1749 auto __func_ptr = __vtable._M_access(__variants.index()...);
1750 return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1751 std::forward<_Variants>(__variants)...);
1753 else // We have a single variant with a small number of alternatives.
1755 // A name for the first variant in the pack.
1757 = [](_V0& __v, ...) -> _V0& { return __v; }(__variants...);
1759 using __detail::__variant::_Multi_array;
1760 using __detail::__variant::__gen_vtable_impl;
1761 using _Ma = _Multi_array<_Result_type (*)(_Visitor&&, _V0&&)>;
1763 #ifdef _GLIBCXX_DEBUG
1764 # define _GLIBCXX_VISIT_UNREACHABLE __builtin_trap
1766 # define _GLIBCXX_VISIT_UNREACHABLE __builtin_unreachable
1769 #define _GLIBCXX_VISIT_CASE(N) \
1772 if constexpr (N < __n) \
1774 return __gen_vtable_impl<_Ma, index_sequence<N>>:: \
1775 __visit_invoke(std::forward<_Visitor>(__visitor), \
1776 std::forward<_V0>(__v0)); \
1778 else _GLIBCXX_VISIT_UNREACHABLE(); \
1781 switch (__v0.index())
1783 _GLIBCXX_VISIT_CASE(0)
1784 _GLIBCXX_VISIT_CASE(1)
1785 _GLIBCXX_VISIT_CASE(2)
1786 _GLIBCXX_VISIT_CASE(3)
1787 _GLIBCXX_VISIT_CASE(4)
1788 _GLIBCXX_VISIT_CASE(5)
1789 _GLIBCXX_VISIT_CASE(6)
1790 _GLIBCXX_VISIT_CASE(7)
1791 _GLIBCXX_VISIT_CASE(8)
1792 _GLIBCXX_VISIT_CASE(9)
1793 _GLIBCXX_VISIT_CASE(10)
1795 using __detail::__variant::__variant_idx_cookie;
1796 using __detail::__variant::__variant_cookie;
1797 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>
1798 || is_same_v<_Result_type, __variant_cookie>)
1800 using _Npos = index_sequence<variant_npos>;
1801 return __gen_vtable_impl<_Ma, _Npos>::
1802 __visit_invoke(std::forward<_Visitor>(__visitor),
1803 std::forward<_V0>(__v0));
1806 _GLIBCXX_VISIT_UNREACHABLE();
1808 _GLIBCXX_VISIT_UNREACHABLE();
1810 #undef _GLIBCXX_VISIT_CASE
1811 #undef _GLIBCXX_VISIT_UNREACHABLE
1817 template<typename _Visitor, typename... _Variants>
1818 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1819 visit(_Visitor&& __visitor, _Variants&&... __variants)
1821 namespace __variant = std::__detail::__variant;
1823 if ((__variant::__as(__variants).valueless_by_exception() || ...))
1824 __throw_bad_variant_access("std::visit: variant is valueless");
1827 = __detail::__variant::__visit_result_t<_Visitor, _Variants...>;
1829 using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
1831 if constexpr (sizeof...(_Variants) == 1)
1833 using _Vp = decltype(__variant::__as(std::declval<_Variants>()...));
1835 constexpr bool __visit_rettypes_match = __detail::__variant::
1836 __check_visitor_results<_Visitor, _Vp>(
1837 make_index_sequence<variant_size_v<remove_reference_t<_Vp>>>());
1838 if constexpr (!__visit_rettypes_match)
1840 static_assert(__visit_rettypes_match,
1841 "std::visit requires the visitor to have the same "
1842 "return type for all alternatives of a variant");
1846 return std::__do_visit<_Tag>(
1847 std::forward<_Visitor>(__visitor),
1848 static_cast<_Vp>(__variants)...);
1851 return std::__do_visit<_Tag>(
1852 std::forward<_Visitor>(__visitor),
1853 __variant::__as(std::forward<_Variants>(__variants))...);
1856 #if __cplusplus > 201703L
1857 template<typename _Res, typename _Visitor, typename... _Variants>
1859 visit(_Visitor&& __visitor, _Variants&&... __variants)
1861 namespace __variant = std::__detail::__variant;
1863 if ((__variant::__as(__variants).valueless_by_exception() || ...))
1864 __throw_bad_variant_access("std::visit<R>: variant is valueless");
1866 return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
1867 __variant::__as(std::forward<_Variants>(__variants))...);
1871 /// @cond undocumented
1872 template<bool, typename... _Types>
1873 struct __variant_hash_call_base_impl
1876 operator()(const variant<_Types...>& __t) const
1877 noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
1880 __detail::__variant::__raw_visit(
1881 [&__t, &__ret](auto&& __t_mem) mutable
1883 using _Type = __remove_cvref_t<decltype(__t_mem)>;
1884 if constexpr (!is_same_v<_Type,
1885 __detail::__variant::__variant_cookie>)
1886 __ret = std::hash<size_t>{}(__t.index())
1887 + std::hash<_Type>{}(__t_mem);
1889 __ret = std::hash<size_t>{}(__t.index());
1895 template<typename... _Types>
1896 struct __variant_hash_call_base_impl<false, _Types...> {};
1898 template<typename... _Types>
1899 using __variant_hash_call_base =
1900 __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
1901 __enable_hash_call &&...), _Types...>;
1904 template<typename... _Types>
1905 struct hash<variant<_Types...>>
1906 : private __detail::__variant::_Variant_hash_base<
1907 variant<_Types...>, std::index_sequence_for<_Types...>>,
1908 public __variant_hash_call_base<_Types...>
1910 using result_type [[__deprecated__]] = size_t;
1911 using argument_type [[__deprecated__]] = variant<_Types...>;
1915 struct hash<monostate>
1917 using result_type [[__deprecated__]] = size_t;
1918 using argument_type [[__deprecated__]] = monostate;
1921 operator()(const monostate&) const noexcept
1923 constexpr size_t __magic_monostate_hash = -7777;
1924 return __magic_monostate_hash;
1928 template<typename... _Types>
1929 struct __is_fast_hash<hash<variant<_Types...>>>
1930 : bool_constant<(__is_fast_hash<_Types>::value && ...)>
1933 _GLIBCXX_END_NAMESPACE_VERSION
1938 #endif // _GLIBCXX_VARIANT