1// <variant> -*- C++ -*-
3// Copyright (C) 2016-2023 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>
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_funcs.h>
44#include <bits/stl_construct.h>
45#include <bits/utility.h> // in_place_index_t
46#if __cplusplus >= 202002L
50#if __cpp_concepts >= 202002L && __cpp_constexpr >= 201811L
51// P2231R1 constexpr needs constexpr unions and constrained destructors.
52# define __cpp_lib_variant 202106L
54# include <ext/aligned_buffer.h> // Use __aligned_membuf instead of union.
55# define __cpp_lib_variant 202102L
58namespace std _GLIBCXX_VISIBILITY(default)
60_GLIBCXX_BEGIN_NAMESPACE_VERSION
62 template<typename... _Types> class tuple;
63 template<typename... _Types> class variant;
64 template <typename> struct hash;
66 template<typename _Variant>
69 template<typename _Variant>
70 struct variant_size<const _Variant> : variant_size<_Variant> {};
72 template<typename _Variant>
73 struct variant_size<volatile _Variant> : variant_size<_Variant> {};
75 template<typename _Variant>
76 struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
78 template<typename... _Types>
79 struct variant_size<variant<_Types...>>
80 : std::integral_constant<size_t, sizeof...(_Types)> {};
82 template<typename _Variant>
83 inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
85 template<typename... _Types>
86 inline constexpr size_t
87 variant_size_v<variant<_Types...>> = sizeof...(_Types);
89 template<typename... _Types>
90 inline constexpr size_t
91 variant_size_v<const variant<_Types...>> = sizeof...(_Types);
93 template<size_t _Np, typename _Variant>
94 struct variant_alternative;
96 template<size_t _Np, typename... _Types>
97 struct variant_alternative<_Np, variant<_Types...>>
99 static_assert(_Np < sizeof...(_Types));
101 using type = typename _Nth_type<_Np, _Types...>::type;
104 template<size_t _Np, typename _Variant>
105 using variant_alternative_t =
106 typename variant_alternative<_Np, _Variant>::type;
108 template<size_t _Np, typename _Variant>
109 struct variant_alternative<_Np, const _Variant>
110 { using type = const variant_alternative_t<_Np, _Variant>; };
112 template<size_t _Np, typename _Variant>
113 struct variant_alternative<_Np, volatile _Variant>
114 { using type = volatile variant_alternative_t<_Np, _Variant>; };
116 template<size_t _Np, typename _Variant>
117 struct variant_alternative<_Np, const volatile _Variant>
118 { using type = const volatile variant_alternative_t<_Np, _Variant>; };
120 inline constexpr size_t variant_npos = -1;
122 template<size_t _Np, typename... _Types>
123 constexpr variant_alternative_t<_Np, variant<_Types...>>&
124 get(variant<_Types...>&);
126 template<size_t _Np, typename... _Types>
127 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
128 get(variant<_Types...>&&);
130 template<size_t _Np, typename... _Types>
131 constexpr variant_alternative_t<_Np, variant<_Types...>> const&
132 get(const variant<_Types...>&);
134 template<size_t _Np, typename... _Types>
135 constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
136 get(const variant<_Types...>&&);
138 template<typename _Result_type, typename _Visitor, typename... _Variants>
139 constexpr decltype(auto)
140 __do_visit(_Visitor&& __visitor, _Variants&&... __variants);
142 template <typename... _Types, typename _Tp>
145 __variant_cast(_Tp&& __rhs)
147 if constexpr (is_lvalue_reference_v<_Tp>)
149 if constexpr (is_const_v<remove_reference_t<_Tp>>)
150 return static_cast<const variant<_Types...>&>(__rhs);
152 return static_cast<variant<_Types...>&>(__rhs);
155 return static_cast<variant<_Types...>&&>(__rhs);
162 // used for raw visitation
163 struct __variant_cookie {};
164 // used for raw visitation with indices passed in
165 struct __variant_idx_cookie { using type = __variant_idx_cookie; };
166 // Used to enable deduction (and same-type checking) for std::visit:
167 template<typename _Tp> struct __deduce_visit_result { using type = _Tp; };
169 // Visit variants that might be valueless.
170 template<typename _Visitor, typename... _Variants>
172 __raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
174 std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
175 std::forward<_Variants>(__variants)...);
178 // Visit variants that might be valueless, passing indices to the visitor.
179 template<typename _Visitor, typename... _Variants>
181 __raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
183 std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
184 std::forward<_Variants>(__variants)...);
187 // The __as function templates implement the exposition-only "as-variant"
189 template<typename... _Types>
190 constexpr std::variant<_Types...>&
191 __as(std::variant<_Types...>& __v) noexcept
194 template<typename... _Types>
195 constexpr const std::variant<_Types...>&
196 __as(const std::variant<_Types...>& __v) noexcept
199 template<typename... _Types>
200 constexpr std::variant<_Types...>&&
201 __as(std::variant<_Types...>&& __v) noexcept
202 { return std::move(__v); }
204 template<typename... _Types>
205 constexpr const std::variant<_Types...>&&
206 __as(const std::variant<_Types...>&& __v) noexcept
207 { return std::move(__v); }
210 // _Uninitialized<T> is guaranteed to be a trivially destructible type,
213 // _Uninitialized<T> is trivially destructible iff T is, so _Variant_union
214 // needs a constrained non-trivial destructor.
215 template<typename _Type, bool = std::is_trivially_destructible_v<_Type>>
216 struct _Uninitialized;
218 template<typename _Type>
219 struct _Uninitialized<_Type, true>
221 template<typename... _Args>
223 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
224 : _M_storage(std::forward<_Args>(__args)...)
227 constexpr const _Type& _M_get() const & noexcept
228 { return _M_storage; }
230 constexpr _Type& _M_get() & noexcept
231 { return _M_storage; }
233 constexpr const _Type&& _M_get() const && noexcept
234 { return std::move(_M_storage); }
236 constexpr _Type&& _M_get() && noexcept
237 { return std::move(_M_storage); }
242 template<typename _Type>
243 struct _Uninitialized<_Type, false>
245#if __cpp_lib_variant >= 202106L
246 template<typename... _Args>
248 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
249 : _M_storage(std::forward<_Args>(__args)...)
252 constexpr ~_Uninitialized() { }
254 _Uninitialized(const _Uninitialized&) = default;
255 _Uninitialized(_Uninitialized&&) = default;
256 _Uninitialized& operator=(const _Uninitialized&) = default;
257 _Uninitialized& operator=(_Uninitialized&&) = default;
259 constexpr const _Type& _M_get() const & noexcept
260 { return _M_storage; }
262 constexpr _Type& _M_get() & noexcept
263 { return _M_storage; }
265 constexpr const _Type&& _M_get() const && noexcept
266 { return std::move(_M_storage); }
268 constexpr _Type&& _M_get() && noexcept
269 { return std::move(_M_storage); }
271 struct _Empty_byte { };
274 _Empty_byte _M_empty;
278 template<typename... _Args>
280 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
282 ::new ((void*)std::addressof(_M_storage))
283 _Type(std::forward<_Args>(__args)...);
286 const _Type& _M_get() const & noexcept
287 { return *_M_storage._M_ptr(); }
289 _Type& _M_get() & noexcept
290 { return *_M_storage._M_ptr(); }
292 const _Type&& _M_get() const && noexcept
293 { return std::move(*_M_storage._M_ptr()); }
295 _Type&& _M_get() && noexcept
296 { return std::move(*_M_storage._M_ptr()); }
298 __gnu_cxx::__aligned_membuf<_Type> _M_storage;
302 template<size_t _Np, typename _Union>
303 constexpr decltype(auto)
304 __get_n(_Union&& __u) noexcept
306 if constexpr (_Np == 0)
307 return std::forward<_Union>(__u)._M_first._M_get();
308 else if constexpr (_Np == 1)
309 return std::forward<_Union>(__u)._M_rest._M_first._M_get();
310 else if constexpr (_Np == 2)
311 return std::forward<_Union>(__u)._M_rest._M_rest._M_first._M_get();
313 return __variant::__get_n<_Np - 3>(
314 std::forward<_Union>(__u)._M_rest._M_rest._M_rest);
317 // Returns the typed storage for __v.
318 template<size_t _Np, typename _Variant>
319 constexpr decltype(auto)
320 __get(_Variant&& __v) noexcept
321 { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
323 // Gets the _Uninitialized to construct into for __u.
324 template<size_t _Np, typename _Union>
325 constexpr decltype(auto)
326 __construct_n(_Union& __u) noexcept
328 if constexpr (_Np == 0)
329 return &__u._M_first;
330 else if constexpr (_Np == 1)
332 std::_Construct(&__u._M_rest);
333 return &__u._M_rest._M_first;
335 else if constexpr (_Np == 2)
337 std::_Construct(&__u._M_rest);
338 std::_Construct(&__u._M_rest._M_rest);
339 return &__u._M_rest._M_rest._M_first;
343 std::_Construct(&__u._M_rest);
344 std::_Construct(&__u._M_rest._M_rest);
345 std::_Construct(&__u._M_rest._M_rest._M_rest);
346 return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest);
350 template<typename... _Types>
353 static constexpr bool _S_default_ctor =
354 is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
355 static constexpr bool _S_copy_ctor =
356 (is_copy_constructible_v<_Types> && ...);
357 static constexpr bool _S_move_ctor =
358 (is_move_constructible_v<_Types> && ...);
359 static constexpr bool _S_copy_assign =
361 && (is_copy_assignable_v<_Types> && ...);
362 static constexpr bool _S_move_assign =
364 && (is_move_assignable_v<_Types> && ...);
366 static constexpr bool _S_trivial_dtor =
367 (is_trivially_destructible_v<_Types> && ...);
368 static constexpr bool _S_trivial_copy_ctor =
369 (is_trivially_copy_constructible_v<_Types> && ...);
370 static constexpr bool _S_trivial_move_ctor =
371 (is_trivially_move_constructible_v<_Types> && ...);
372 static constexpr bool _S_trivial_copy_assign =
373 _S_trivial_dtor && _S_trivial_copy_ctor
374 && (is_trivially_copy_assignable_v<_Types> && ...);
375 static constexpr bool _S_trivial_move_assign =
376 _S_trivial_dtor && _S_trivial_move_ctor
377 && (is_trivially_move_assignable_v<_Types> && ...);
379 // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
380 // are always nothrow.
381 static constexpr bool _S_nothrow_default_ctor =
382 is_nothrow_default_constructible_v<
383 typename _Nth_type<0, _Types...>::type>;
384 static constexpr bool _S_nothrow_copy_ctor = false;
385 static constexpr bool _S_nothrow_move_ctor =
386 (is_nothrow_move_constructible_v<_Types> && ...);
387 static constexpr bool _S_nothrow_copy_assign = false;
388 static constexpr bool _S_nothrow_move_assign =
390 && (is_nothrow_move_assignable_v<_Types> && ...);
393 // Defines members and ctors.
394 template<typename... _Types>
395 union _Variadic_union
397 _Variadic_union() = default;
399 template<size_t _Np, typename... _Args>
400 _Variadic_union(in_place_index_t<_Np>, _Args&&...) = delete;
403 template<typename _First, typename... _Rest>
404 union _Variadic_union<_First, _Rest...>
406 constexpr _Variadic_union() : _M_rest() { }
408 template<typename... _Args>
410 _Variadic_union(in_place_index_t<0>, _Args&&... __args)
411 : _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
414 template<size_t _Np, typename... _Args>
416 _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
417 : _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
420#if __cpp_lib_variant >= 202106L
421 _Variadic_union(const _Variadic_union&) = default;
422 _Variadic_union(_Variadic_union&&) = default;
423 _Variadic_union& operator=(const _Variadic_union&) = default;
424 _Variadic_union& operator=(_Variadic_union&&) = default;
426 ~_Variadic_union() = default;
428 constexpr ~_Variadic_union()
429 requires (!__has_trivial_destructor(_First))
430 || (!__has_trivial_destructor(_Variadic_union<_Rest...>))
434 _Uninitialized<_First> _M_first;
435 _Variadic_union<_Rest...> _M_rest;
438 // _Never_valueless_alt is true for variant alternatives that can
439 // always be placed in a variant without it becoming valueless.
441 // For suitably-small, trivially copyable types we can create temporaries
442 // on the stack and then memcpy them into place.
443 template<typename _Tp>
444 struct _Never_valueless_alt
445 : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
448 // Specialize _Never_valueless_alt for other types which have a
449 // non-throwing and cheap move construction and move assignment operator,
450 // so that emplacing the type will provide the strong exception-safety
451 // guarantee, by creating and moving a temporary.
452 // Whether _Never_valueless_alt<T> is true or not affects the ABI of a
453 // variant using that alternative, so we can't change the value later!
455 // True if every alternative in _Types... can be emplaced in a variant
456 // without it becoming valueless. If this is true, variant<_Types...>
457 // can never be valueless, which enables some minor optimizations.
458 template <typename... _Types>
459 constexpr bool __never_valueless()
461 return _Traits<_Types...>::_S_move_assign
462 && (_Never_valueless_alt<_Types>::value && ...);
465 // Defines index and the dtor, possibly trivial.
466 template<bool __trivially_destructible, typename... _Types>
467 struct _Variant_storage;
469 template <typename... _Types>
470 using __select_index =
471 typename __select_int::_Select_int_base<sizeof...(_Types),
473 unsigned short>::type::value_type;
475 template<typename... _Types>
476 struct _Variant_storage<false, _Types...>
480 : _M_index(static_cast<__index_type>(variant_npos))
483 template<size_t _Np, typename... _Args>
485 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
486 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
493 if (!_M_valid()) [[unlikely]]
496 std::__do_visit<void>([](auto&& __this_mem) mutable
498 std::_Destroy(std::__addressof(__this_mem));
499 }, __variant_cast<_Types...>(*this));
501 _M_index = static_cast<__index_type>(variant_npos);
509 _M_valid() const noexcept
511 if constexpr (__variant::__never_valueless<_Types...>())
513 return this->_M_index != __index_type(variant_npos);
516 _Variadic_union<_Types...> _M_u;
517 using __index_type = __select_index<_Types...>;
518 __index_type _M_index;
521 template<typename... _Types>
522 struct _Variant_storage<true, _Types...>
526 : _M_index(static_cast<__index_type>(variant_npos))
529 template<size_t _Np, typename... _Args>
531 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
532 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
538 { _M_index = static_cast<__index_type>(variant_npos); }
541 _M_valid() const noexcept
543 if constexpr (__variant::__never_valueless<_Types...>())
545 // It would be nice if we could just return true for -fno-exceptions.
546 // It's possible (but inadvisable) that a std::variant could become
547 // valueless in a translation unit compiled with -fexceptions and then
548 // be passed to functions compiled with -fno-exceptions. We would need
549 // some #ifdef _GLIBCXX_NO_EXCEPTIONS_GLOBALLY property to elide all
550 // checks for valueless_by_exception().
551 return this->_M_index != static_cast<__index_type>(variant_npos);
554 _Variadic_union<_Types...> _M_u;
555 using __index_type = __select_index<_Types...>;
556 __index_type _M_index;
559 // Implementation of v.emplace<N>(args...).
560 template<size_t _Np, bool _Triv, typename... _Types, typename... _Args>
563 __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
566 auto* __addr = __variant::__construct_n<_Np>(__v._M_u);
567 std::_Construct(__addr, in_place_index<0>,
568 std::forward<_Args>(__args)...);
569 // Construction didn't throw, so can set the new index now:
573 template<typename... _Types>
574 using _Variant_storage_alias =
575 _Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
577 // The following are (Copy|Move) (ctor|assign) layers for forwarding
578 // triviality and handling non-trivial SMF behaviors.
580 template<bool, typename... _Types>
581 struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
583 using _Base = _Variant_storage_alias<_Types...>;
587 _Copy_ctor_base(const _Copy_ctor_base& __rhs)
588 noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
590 __variant::__raw_idx_visit(
591 [this](auto&& __rhs_mem, auto __rhs_index) mutable
593 constexpr size_t __j = __rhs_index;
594 if constexpr (__j != variant_npos)
595 std::_Construct(std::__addressof(this->_M_u),
596 in_place_index<__j>, __rhs_mem);
597 }, __variant_cast<_Types...>(__rhs));
598 this->_M_index = __rhs._M_index;
601 _Copy_ctor_base(_Copy_ctor_base&&) = default;
602 _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
603 _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
606 template<typename... _Types>
607 struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
609 using _Base = _Variant_storage_alias<_Types...>;
613 template<typename... _Types>
614 using _Copy_ctor_alias =
615 _Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
617 template<bool, typename... _Types>
618 struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
620 using _Base = _Copy_ctor_alias<_Types...>;
624 _Move_ctor_base(_Move_ctor_base&& __rhs)
625 noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
627 __variant::__raw_idx_visit(
628 [this](auto&& __rhs_mem, auto __rhs_index) mutable
630 constexpr size_t __j = __rhs_index;
631 if constexpr (__j != variant_npos)
632 std::_Construct(std::__addressof(this->_M_u),
634 std::forward<decltype(__rhs_mem)>(__rhs_mem));
635 }, __variant_cast<_Types...>(std::move(__rhs)));
636 this->_M_index = __rhs._M_index;
639 _Move_ctor_base(const _Move_ctor_base&) = default;
640 _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
641 _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
644 template<typename... _Types>
645 struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
647 using _Base = _Copy_ctor_alias<_Types...>;
651 template<typename... _Types>
652 using _Move_ctor_alias =
653 _Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
655 template<bool, typename... _Types>
656 struct _Copy_assign_base : _Move_ctor_alias<_Types...>
658 using _Base = _Move_ctor_alias<_Types...>;
663 operator=(const _Copy_assign_base& __rhs)
664 noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
666 __variant::__raw_idx_visit(
667 [this](auto&& __rhs_mem, auto __rhs_index) mutable
669 constexpr size_t __j = __rhs_index;
670 if constexpr (__j == variant_npos)
671 this->_M_reset(); // Make *this valueless.
672 else if (this->_M_index == __j)
673 __variant::__get<__j>(*this) = __rhs_mem;
676 using _Tj = typename _Nth_type<__j, _Types...>::type;
677 if constexpr (is_nothrow_copy_constructible_v<_Tj>
678 || !is_nothrow_move_constructible_v<_Tj>)
679 __variant::__emplace<__j>(*this, __rhs_mem);
682 using _Variant = variant<_Types...>;
683 _Variant& __self = __variant_cast<_Types...>(*this);
684 __self = _Variant(in_place_index<__j>, __rhs_mem);
687 }, __variant_cast<_Types...>(__rhs));
691 _Copy_assign_base(const _Copy_assign_base&) = default;
692 _Copy_assign_base(_Copy_assign_base&&) = default;
693 _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
696 template<typename... _Types>
697 struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
699 using _Base = _Move_ctor_alias<_Types...>;
703 template<typename... _Types>
704 using _Copy_assign_alias =
705 _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
707 template<bool, typename... _Types>
708 struct _Move_assign_base : _Copy_assign_alias<_Types...>
710 using _Base = _Copy_assign_alias<_Types...>;
715 operator=(_Move_assign_base&& __rhs)
716 noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
718 __variant::__raw_idx_visit(
719 [this](auto&& __rhs_mem, auto __rhs_index) mutable
721 constexpr size_t __j = __rhs_index;
722 if constexpr (__j != variant_npos)
724 if (this->_M_index == __j)
725 __variant::__get<__j>(*this) = std::move(__rhs_mem);
728 using _Tj = typename _Nth_type<__j, _Types...>::type;
729 if constexpr (is_nothrow_move_constructible_v<_Tj>)
730 __variant::__emplace<__j>(*this, std::move(__rhs_mem));
733 using _Variant = variant<_Types...>;
734 _Variant& __self = __variant_cast<_Types...>(*this);
735 __self.template emplace<__j>(std::move(__rhs_mem));
741 }, __variant_cast<_Types...>(__rhs));
745 _Move_assign_base(const _Move_assign_base&) = default;
746 _Move_assign_base(_Move_assign_base&&) = default;
747 _Move_assign_base& operator=(const _Move_assign_base&) = default;
750 template<typename... _Types>
751 struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
753 using _Base = _Copy_assign_alias<_Types...>;
757 template<typename... _Types>
758 using _Move_assign_alias =
759 _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
761 template<typename... _Types>
762 struct _Variant_base : _Move_assign_alias<_Types...>
764 using _Base = _Move_assign_alias<_Types...>;
767 _Variant_base() noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
768 : _Variant_base(in_place_index<0>) { }
770 template<size_t _Np, typename... _Args>
772 _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
773 : _Base(__i, std::forward<_Args>(__args)...)
776 _Variant_base(const _Variant_base&) = default;
777 _Variant_base(_Variant_base&&) = default;
778 _Variant_base& operator=(const _Variant_base&) = default;
779 _Variant_base& operator=(_Variant_base&&) = default;
782 template<typename _Tp, typename... _Types>
783 inline constexpr bool __exactly_once
784 = std::__find_uniq_type_in_pack<_Tp, _Types...>() < sizeof...(_Types);
786 // Helper used to check for valid conversions that don't involve narrowing.
787 template<typename _Ti> struct _Arr { _Ti _M_x[1]; };
789 // "Build an imaginary function FUN(Ti) for each alternative type Ti"
790 template<size_t _Ind, typename _Tp, typename _Ti, typename = void>
793 // This function means 'using _Build_FUN<I, T, Ti>::_S_fun;' is valid,
794 // but only static functions will be considered in the call below.
795 void _S_fun() = delete;
798 // "... for which Ti x[] = {std::forward<T>(t)}; is well-formed."
799 template<size_t _Ind, typename _Tp, typename _Ti>
800 struct _Build_FUN<_Ind, _Tp, _Ti,
801 void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
803 // This is the FUN function for type _Ti, with index _Ind
804 static integral_constant<size_t, _Ind> _S_fun(_Ti);
807 template<typename _Tp, typename _Variant,
808 typename = make_index_sequence<variant_size_v<_Variant>>>
811 template<typename _Tp, typename... _Ti, size_t... _Ind>
812 struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
813 : _Build_FUN<_Ind, _Tp, _Ti>...
815 using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
818 // The index j of the overload FUN(Tj) selected by overload resolution
819 // for FUN(std::forward<_Tp>(t))
820 template<typename _Tp, typename _Variant>
822 = decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
824 // The index selected for FUN(std::forward<T>(t)), or variant_npos if none.
825 template<typename _Tp, typename _Variant, typename = void>
826 inline constexpr size_t
827 __accepted_index = variant_npos;
829 template<typename _Tp, typename _Variant>
830 inline constexpr size_t
831 __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
832 = _FUN_type<_Tp, _Variant>::value;
834 template<typename _Maybe_variant_cookie, typename _Variant,
835 typename = __remove_cvref_t<_Variant>>
836 inline constexpr bool
837 __extra_visit_slot_needed = false;
839 template<typename _Var, typename... _Types>
840 inline constexpr bool
841 __extra_visit_slot_needed<__variant_cookie, _Var, variant<_Types...>>
842 = !__variant::__never_valueless<_Types...>();
844 template<typename _Var, typename... _Types>
845 inline constexpr bool
846 __extra_visit_slot_needed<__variant_idx_cookie, _Var, variant<_Types...>>
847 = !__variant::__never_valueless<_Types...>();
849 // Used for storing a multi-dimensional vtable.
850 template<typename _Tp, size_t... _Dimensions>
853 // Partial specialization with rank zero, stores a single _Tp element.
854 template<typename _Tp>
855 struct _Multi_array<_Tp>
858 struct __untag_result
860 { using element_type = _Tp; };
862#pragma GCC diagnostic push
863#pragma GCC diagnostic ignored "-Wignored-qualifiers"
864 template <typename... _Args>
865 struct __untag_result<const void(*)(_Args...)>
867 { using element_type = void(*)(_Args...); };
868#pragma GCC diagnostic pop
870 template <typename... _Args>
871 struct __untag_result<__variant_cookie(*)(_Args...)>
873 { using element_type = void(*)(_Args...); };
875 template <typename... _Args>
876 struct __untag_result<__variant_idx_cookie(*)(_Args...)>
878 { using element_type = void(*)(_Args...); };
880 template <typename _Res, typename... _Args>
881 struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
883 { using element_type = _Res(*)(_Args...); };
885 using __result_is_deduced = __untag_result<_Tp>;
887 constexpr const typename __untag_result<_Tp>::element_type&
891 typename __untag_result<_Tp>::element_type _M_data;
894 // Partial specialization with rank >= 1.
895 template<typename _Ret,
897 typename... _Variants,
898 size_t __first, size_t... __rest>
899 struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
901 static constexpr size_t __index =
902 sizeof...(_Variants) - sizeof...(__rest) - 1;
904 using _Variant = typename _Nth_type<__index, _Variants...>::type;
906 static constexpr int __do_cookie =
907 __extra_visit_slot_needed<_Ret, _Variant> ? 1 : 0;
909 using _Tp = _Ret(*)(_Visitor, _Variants...);
911 template<typename... _Args>
912 constexpr decltype(auto)
913 _M_access(size_t __first_index, _Args... __rest_indices) const
915 return _M_arr[__first_index + __do_cookie]
916 ._M_access(__rest_indices...);
919 _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
922 // Creates a multi-dimensional vtable recursively.
925 // visit([](auto, auto){},
926 // variant<int, char>(), // typedef'ed as V1
927 // variant<float, double, long double>()) // typedef'ed as V2
928 // will trigger instantiations of:
929 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
930 // tuple<V1&&, V2&&>, std::index_sequence<>>
931 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
932 // tuple<V1&&, V2&&>, std::index_sequence<0>>
933 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
934 // tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
935 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
936 // tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
937 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
938 // tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
939 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
940 // tuple<V1&&, V2&&>, std::index_sequence<1>>
941 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
942 // tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
943 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
944 // tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
945 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
946 // tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
947 // The returned multi-dimensional vtable can be fast accessed by the visitor
948 // using index calculation.
949 template<typename _Array_type, typename _Index_seq>
950 struct __gen_vtable_impl;
952 // Defines the _S_apply() member that returns a _Multi_array populated
953 // with function pointers that perform the visitation expressions e(m)
954 // for each valid pack of indexes into the variant types _Variants.
956 // This partial specialization builds up the index sequences by recursively
957 // calling _S_apply() on the next specialization of __gen_vtable_impl.
958 // The base case of the recursion defines the actual function pointers.
959 template<typename _Result_type, typename _Visitor, size_t... __dimensions,
960 typename... _Variants, size_t... __indices>
961 struct __gen_vtable_impl<
962 _Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
963 std::index_sequence<__indices...>>
966 remove_reference_t<typename _Nth_type<sizeof...(__indices),
967 _Variants...>::type>;
969 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
972 static constexpr _Array_type
975 _Array_type __vtable{};
977 __vtable, make_index_sequence<variant_size_v<_Next>>());
981 template<size_t... __var_indices>
982 static constexpr void
983 _S_apply_all_alts(_Array_type& __vtable,
984 std::index_sequence<__var_indices...>)
986 if constexpr (__extra_visit_slot_needed<_Result_type, _Next>)
987 (_S_apply_single_alt<true, __var_indices>(
988 __vtable._M_arr[__var_indices + 1],
989 &(__vtable._M_arr[0])), ...);
991 (_S_apply_single_alt<false, __var_indices>(
992 __vtable._M_arr[__var_indices]), ...);
995 template<bool __do_cookie, size_t __index, typename _Tp>
996 static constexpr void
997 _S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
999 if constexpr (__do_cookie)
1001 __element = __gen_vtable_impl<
1003 std::index_sequence<__indices..., __index>>::_S_apply();
1004 *__cookie_element = __gen_vtable_impl<
1006 std::index_sequence<__indices..., variant_npos>>::_S_apply();
1010 auto __tmp_element = __gen_vtable_impl<
1011 remove_reference_t<decltype(__element)>,
1012 std::index_sequence<__indices..., __index>>::_S_apply();
1013 static_assert(is_same_v<_Tp, decltype(__tmp_element)>,
1014 "std::visit requires the visitor to have the same "
1015 "return type for all alternatives of a variant");
1016 __element = __tmp_element;
1021 // This partial specialization is the base case for the recursion.
1022 // It populates a _Multi_array element with the address of a function
1023 // that invokes the visitor with the alternatives specified by __indices.
1024 template<typename _Result_type, typename _Visitor, typename... _Variants,
1025 size_t... __indices>
1026 struct __gen_vtable_impl<
1027 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
1028 std::index_sequence<__indices...>>
1031 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
1033 template<size_t __index, typename _Variant>
1034 static constexpr decltype(auto)
1035 __element_by_index_or_cookie(_Variant&& __var) noexcept
1037 if constexpr (__index != variant_npos)
1038 return __variant::__get<__index>(std::forward<_Variant>(__var));
1040 return __variant_cookie{};
1043 static constexpr decltype(auto)
1044 __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
1046 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
1047 // For raw visitation using indices, pass the indices to the visitor
1048 // and discard the return value:
1049 std::__invoke(std::forward<_Visitor>(__visitor),
1050 __element_by_index_or_cookie<__indices>(
1051 std::forward<_Variants>(__vars))...,
1052 integral_constant<size_t, __indices>()...);
1053 else if constexpr (is_same_v<_Result_type, __variant_cookie>)
1054 // For raw visitation without indices, and discard the return value:
1055 std::__invoke(std::forward<_Visitor>(__visitor),
1056 __element_by_index_or_cookie<__indices>(
1057 std::forward<_Variants>(__vars))...);
1058 else if constexpr (_Array_type::__result_is_deduced::value)
1059 // For the usual std::visit case deduce the return value:
1060 return std::__invoke(std::forward<_Visitor>(__visitor),
1061 __element_by_index_or_cookie<__indices>(
1062 std::forward<_Variants>(__vars))...);
1063 else // for std::visit<R> use INVOKE<R>
1064 return std::__invoke_r<_Result_type>(
1065 std::forward<_Visitor>(__visitor),
1066 __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
1069 static constexpr auto
1072 if constexpr (_Array_type::__result_is_deduced::value)
1074 constexpr bool __visit_ret_type_mismatch =
1075 !is_same_v<typename _Result_type::type,
1076 decltype(__visit_invoke(std::declval<_Visitor>(),
1077 std::declval<_Variants>()...))>;
1078 if constexpr (__visit_ret_type_mismatch)
1080 struct __cannot_match {};
1081 return __cannot_match{};
1084 return _Array_type{&__visit_invoke};
1087 return _Array_type{&__visit_invoke};
1091 template<typename _Result_type, typename _Visitor, typename... _Variants>
1095 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
1096 variant_size_v<remove_reference_t<_Variants>>...>;
1098 static constexpr _Array_type _S_vtable
1099 = __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
1102 template<size_t _Np, typename _Tp>
1103 struct _Base_dedup : public _Tp { };
1105 template<typename _Variant, typename __indices>
1106 struct _Variant_hash_base;
1108 template<typename... _Types, size_t... __indices>
1109 struct _Variant_hash_base<variant<_Types...>,
1110 std::index_sequence<__indices...>>
1111 : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
1113 // Equivalent to decltype(get<_Np>(as-variant(declval<_Variant>())))
1114 template<size_t _Np, typename _Variant,
1115 typename _AsV = decltype(__variant::__as(std::declval<_Variant>())),
1116 typename _Tp = variant_alternative_t<_Np, remove_reference_t<_AsV>>>
1118 = __conditional_t<is_lvalue_reference_v<_Variant>, _Tp&, _Tp&&>;
1120 // Return type of std::visit.
1121 template<typename _Visitor, typename... _Variants>
1122 using __visit_result_t
1123 = invoke_result_t<_Visitor, __get_t<0, _Variants>...>;
1125 template<typename _Tp, typename... _Types>
1126 constexpr inline bool __same_types = (is_same_v<_Tp, _Types> && ...);
1128 template <typename _Visitor, typename _Variant, size_t... _Idxs>
1129 constexpr bool __check_visitor_results(std::index_sequence<_Idxs...>)
1131 return __same_types<
1132 invoke_result_t<_Visitor, __get_t<_Idxs, _Variant>>...
1136} // namespace __variant
1137} // namespace __detail
1139 template<typename _Tp, typename... _Types>
1141 holds_alternative(const variant<_Types...>& __v) noexcept
1143 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1144 "T must occur exactly once in alternatives");
1145 return __v.index() == std::__find_uniq_type_in_pack<_Tp, _Types...>();
1148 template<typename _Tp, typename... _Types>
1150 get(variant<_Types...>& __v)
1152 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1153 "T must occur exactly once in alternatives");
1154 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1155 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1156 return std::get<__n>(__v);
1159 template<typename _Tp, typename... _Types>
1161 get(variant<_Types...>&& __v)
1163 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1164 "T must occur exactly once in alternatives");
1165 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1166 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1167 return std::get<__n>(std::move(__v));
1170 template<typename _Tp, typename... _Types>
1171 constexpr const _Tp&
1172 get(const variant<_Types...>& __v)
1174 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1175 "T must occur exactly once in alternatives");
1176 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1177 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1178 return std::get<__n>(__v);
1181 template<typename _Tp, typename... _Types>
1182 constexpr const _Tp&&
1183 get(const variant<_Types...>&& __v)
1185 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1186 "T must occur exactly once in alternatives");
1187 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1188 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1189 return std::get<__n>(std::move(__v));
1192 template<size_t _Np, typename... _Types>
1193 constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
1194 get_if(variant<_Types...>* __ptr) noexcept
1196 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1197 static_assert(_Np < sizeof...(_Types),
1198 "The index must be in [0, number of alternatives)");
1199 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1200 if (__ptr && __ptr->index() == _Np)
1201 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1205 template<size_t _Np, typename... _Types>
1207 add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
1208 get_if(const variant<_Types...>* __ptr) noexcept
1210 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1211 static_assert(_Np < sizeof...(_Types),
1212 "The index must be in [0, number of alternatives)");
1213 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1214 if (__ptr && __ptr->index() == _Np)
1215 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1219 template<typename _Tp, typename... _Types>
1220 constexpr add_pointer_t<_Tp>
1221 get_if(variant<_Types...>* __ptr) noexcept
1223 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1224 "T must occur exactly once in alternatives");
1225 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1226 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1227 return std::get_if<__n>(__ptr);
1230 template<typename _Tp, typename... _Types>
1231 constexpr add_pointer_t<const _Tp>
1232 get_if(const variant<_Types...>* __ptr) noexcept
1234 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1235 "T must occur exactly once in alternatives");
1236 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1237 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1238 return std::get_if<__n>(__ptr);
1241 struct monostate { };
1243#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
1244 template<typename... _Types> \
1245 constexpr bool operator __OP(const variant<_Types...>& __lhs, \
1246 const variant<_Types...>& __rhs) \
1248 bool __ret = true; \
1249 __detail::__variant::__raw_idx_visit( \
1250 [&__ret, &__lhs] (auto&& __rhs_mem, auto __rhs_index) mutable \
1252 if constexpr (__rhs_index != variant_npos) \
1254 if (__lhs.index() == __rhs_index) \
1256 auto& __this_mem = std::get<__rhs_index>(__lhs); \
1257 __ret = __this_mem __OP __rhs_mem; \
1260 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1263 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1268 _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
1269 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
1270 _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
1271 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
1272 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
1273 _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
1275#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1277 constexpr bool operator==(monostate, monostate) noexcept { return true; }
1279#ifdef __cpp_lib_three_way_comparison
1280 template<typename... _Types>
1281 requires (three_way_comparable<_Types> && ...)
1283 common_comparison_category_t<compare_three_way_result_t<_Types>...>
1284 operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
1286 common_comparison_category_t<compare_three_way_result_t<_Types>...> __ret
1287 = strong_ordering::equal;
1289 __detail::__variant::__raw_idx_visit(
1290 [&__ret, &__v] (auto&& __w_mem, auto __w_index) mutable
1292 if constexpr (__w_index != variant_npos)
1294 if (__v.index() == __w_index)
1296 auto& __this_mem = std::get<__w_index>(__v);
1297 __ret = __this_mem <=> __w_mem;
1301 __ret = (__v.index() + 1) <=> (__w_index + 1);
1306 constexpr strong_ordering
1307 operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
1309 constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1310 constexpr bool operator<(monostate, monostate) noexcept { return false; }
1311 constexpr bool operator>(monostate, monostate) noexcept { return false; }
1312 constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1313 constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1316 template<typename _Visitor, typename... _Variants>
1317 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1318 visit(_Visitor&&, _Variants&&...);
1320 template<typename... _Types>
1321 _GLIBCXX20_CONSTEXPR
1322 inline enable_if_t<(is_move_constructible_v<_Types> && ...)
1323 && (is_swappable_v<_Types> && ...)>
1324 swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1325 noexcept(noexcept(__lhs.swap(__rhs)))
1326 { __lhs.swap(__rhs); }
1328 template<typename... _Types>
1329 enable_if_t<!((is_move_constructible_v<_Types> && ...)
1330 && (is_swappable_v<_Types> && ...))>
1331 swap(variant<_Types...>&, variant<_Types...>&) = delete;
1333 class bad_variant_access : public exception
1336 bad_variant_access() noexcept { }
1338 const char* what() const noexcept override
1339 { return _M_reason; }
1342 bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
1344 // Must point to a string with static storage duration:
1345 const char* _M_reason = "bad variant access";
1347 friend void __throw_bad_variant_access(const char* __what);
1350 // Must only be called with a string literal
1352 __throw_bad_variant_access(const char* __what)
1353 { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
1356 __throw_bad_variant_access(bool __valueless)
1358 if (__valueless) [[__unlikely__]]
1359 __throw_bad_variant_access("std::get: variant is valueless");
1361 __throw_bad_variant_access("std::get: wrong index for variant");
1364 template<typename... _Types>
1366 : private __detail::__variant::_Variant_base<_Types...>,
1367 private _Enable_default_constructor<
1368 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1369 variant<_Types...>>,
1370 private _Enable_copy_move<
1371 __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1372 __detail::__variant::_Traits<_Types...>::_S_copy_assign,
1373 __detail::__variant::_Traits<_Types...>::_S_move_ctor,
1374 __detail::__variant::_Traits<_Types...>::_S_move_assign,
1378 template <typename... _UTypes, typename _Tp>
1379 friend _GLIBCXX20_CONSTEXPR decltype(auto)
1380 __variant_cast(_Tp&&);
1382 static_assert(sizeof...(_Types) > 0,
1383 "variant must have at least one alternative");
1384 static_assert(!(std::is_reference_v<_Types> || ...),
1385 "variant must have no reference alternative");
1386 static_assert(!(std::is_void_v<_Types> || ...),
1387 "variant must have no void alternative");
1389 using _Base = __detail::__variant::_Variant_base<_Types...>;
1390 using _Default_ctor_enabler =
1391 _Enable_default_constructor<
1392 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1393 variant<_Types...>>;
1395 template<typename _Tp>
1396 static constexpr bool __not_self
1397 = !is_same_v<__remove_cvref_t<_Tp>, variant>;
1399 template<typename _Tp>
1400 static constexpr bool
1401 __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1403 template<typename _Tp>
1404 static constexpr size_t __accepted_index
1405 = __detail::__variant::__accepted_index<_Tp, variant>;
1407 template<size_t _Np, typename = enable_if_t<(_Np < sizeof...(_Types))>>
1408 using __to_type = typename _Nth_type<_Np, _Types...>::type;
1410 template<typename _Tp, typename = enable_if_t<__not_self<_Tp>>>
1411 using __accepted_type = __to_type<__accepted_index<_Tp>>;
1413 template<typename _Tp>
1414 static constexpr size_t __index_of
1415 = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1417 using _Traits = __detail::__variant::_Traits<_Types...>;
1419 template<typename _Tp>
1420 struct __is_in_place_tag : false_type { };
1421 template<typename _Tp>
1422 struct __is_in_place_tag<in_place_type_t<_Tp>> : true_type { };
1423 template<size_t _Np>
1424 struct __is_in_place_tag<in_place_index_t<_Np>> : true_type { };
1426 template<typename _Tp>
1427 static constexpr bool __not_in_place_tag
1428 = !__is_in_place_tag<__remove_cvref_t<_Tp>>::value;
1431 variant() = default;
1432 variant(const variant& __rhs) = default;
1433 variant(variant&&) = default;
1434 variant& operator=(const variant&) = default;
1435 variant& operator=(variant&&) = default;
1436 _GLIBCXX20_CONSTEXPR ~variant() = default;
1438 template<typename _Tp,
1439 typename = enable_if_t<sizeof...(_Types) != 0>,
1440 typename = enable_if_t<__not_in_place_tag<_Tp>>,
1441 typename _Tj = __accepted_type<_Tp&&>,
1442 typename = enable_if_t<__exactly_once<_Tj>
1443 && is_constructible_v<_Tj, _Tp>>>
1446 noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
1447 : variant(in_place_index<__accepted_index<_Tp>>,
1448 std::forward<_Tp>(__t))
1451 template<typename _Tp, typename... _Args,
1452 typename = enable_if_t<__exactly_once<_Tp>
1453 && is_constructible_v<_Tp, _Args...>>>
1455 variant(in_place_type_t<_Tp>, _Args&&... __args)
1456 : variant(in_place_index<__index_of<_Tp>>,
1457 std::forward<_Args>(__args)...)
1460 template<typename _Tp, typename _Up, typename... _Args,
1461 typename = enable_if_t<__exactly_once<_Tp>
1462 && is_constructible_v<_Tp,
1463 initializer_list<_Up>&, _Args...>>>
1465 variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1467 : variant(in_place_index<__index_of<_Tp>>, __il,
1468 std::forward<_Args>(__args)...)
1471 template<size_t _Np, typename... _Args,
1472 typename _Tp = __to_type<_Np>,
1473 typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
1475 variant(in_place_index_t<_Np>, _Args&&... __args)
1476 : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
1477 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1480 template<size_t _Np, typename _Up, typename... _Args,
1481 typename _Tp = __to_type<_Np>,
1482 typename = enable_if_t<is_constructible_v<_Tp,
1483 initializer_list<_Up>&,
1486 variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1488 : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
1489 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1492 template<typename _Tp>
1493 _GLIBCXX20_CONSTEXPR
1494 enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1495 && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
1496 && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
1498 operator=(_Tp&& __rhs)
1499 noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
1500 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
1502 constexpr auto __index = __accepted_index<_Tp>;
1503 if (index() == __index)
1504 std::get<__index>(*this) = std::forward<_Tp>(__rhs);
1507 using _Tj = __accepted_type<_Tp&&>;
1508 if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
1509 || !is_nothrow_move_constructible_v<_Tj>)
1510 this->emplace<__index>(std::forward<_Tp>(__rhs));
1512 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1513 // 3585. converting assignment with immovable alternative
1514 this->emplace<__index>(_Tj(std::forward<_Tp>(__rhs)));
1519 template<typename _Tp, typename... _Args>
1520 _GLIBCXX20_CONSTEXPR
1521 enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1523 emplace(_Args&&... __args)
1525 constexpr size_t __index = __index_of<_Tp>;
1526 return this->emplace<__index>(std::forward<_Args>(__args)...);
1529 template<typename _Tp, typename _Up, typename... _Args>
1530 _GLIBCXX20_CONSTEXPR
1531 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1532 && __exactly_once<_Tp>,
1534 emplace(initializer_list<_Up> __il, _Args&&... __args)
1536 constexpr size_t __index = __index_of<_Tp>;
1537 return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
1540 template<size_t _Np, typename... _Args>
1541 _GLIBCXX20_CONSTEXPR
1542 enable_if_t<is_constructible_v<__to_type<_Np>, _Args...>,
1544 emplace(_Args&&... __args)
1546 namespace __variant = std::__detail::__variant;
1547 using type = typename _Nth_type<_Np, _Types...>::type;
1548 // Provide the strong exception-safety guarantee when possible,
1549 // to avoid becoming valueless.
1550 if constexpr (is_nothrow_constructible_v<type, _Args...>)
1552 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1554 else if constexpr (is_scalar_v<type>)
1556 // This might invoke a potentially-throwing conversion operator:
1557 const type __tmp(std::forward<_Args>(__args)...);
1558 // But this won't throw:
1559 __variant::__emplace<_Np>(*this, __tmp);
1561 else if constexpr (__variant::_Never_valueless_alt<type>()
1562 && _Traits::_S_move_assign)
1564 // This construction might throw:
1565 variant __tmp(in_place_index<_Np>,
1566 std::forward<_Args>(__args)...);
1567 // But _Never_valueless_alt<type> means this won't:
1568 *this = std::move(__tmp);
1572 // This case only provides the basic exception-safety guarantee,
1573 // i.e. the variant can become valueless.
1574 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1576 return std::get<_Np>(*this);
1579 template<size_t _Np, typename _Up, typename... _Args>
1580 _GLIBCXX20_CONSTEXPR
1581 enable_if_t<is_constructible_v<__to_type<_Np>,
1582 initializer_list<_Up>&, _Args...>,
1584 emplace(initializer_list<_Up> __il, _Args&&... __args)
1586 namespace __variant = std::__detail::__variant;
1587 using type = typename _Nth_type<_Np, _Types...>::type;
1588 // Provide the strong exception-safety guarantee when possible,
1589 // to avoid becoming valueless.
1590 if constexpr (is_nothrow_constructible_v<type,
1591 initializer_list<_Up>&,
1594 __variant::__emplace<_Np>(*this, __il,
1595 std::forward<_Args>(__args)...);
1597 else if constexpr (__variant::_Never_valueless_alt<type>()
1598 && _Traits::_S_move_assign)
1600 // This construction might throw:
1601 variant __tmp(in_place_index<_Np>, __il,
1602 std::forward<_Args>(__args)...);
1603 // But _Never_valueless_alt<type> means this won't:
1604 *this = std::move(__tmp);
1608 // This case only provides the basic exception-safety guarantee,
1609 // i.e. the variant can become valueless.
1610 __variant::__emplace<_Np>(*this, __il,
1611 std::forward<_Args>(__args)...);
1613 return std::get<_Np>(*this);
1616 template<size_t _Np, typename... _Args>
1617 enable_if_t<!(_Np < sizeof...(_Types))> emplace(_Args&&...) = delete;
1619 template<typename _Tp, typename... _Args>
1620 enable_if_t<!__exactly_once<_Tp>> emplace(_Args&&...) = delete;
1622 constexpr bool valueless_by_exception() const noexcept
1623 { return !this->_M_valid(); }
1625 constexpr size_t index() const noexcept
1627 using __index_type = typename _Base::__index_type;
1628 if constexpr (__detail::__variant::__never_valueless<_Types...>())
1629 return this->_M_index;
1630 else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
1631 return make_signed_t<__index_type>(this->_M_index);
1633 return size_t(__index_type(this->_M_index + 1)) - 1;
1636 _GLIBCXX20_CONSTEXPR
1638 swap(variant& __rhs)
1639 noexcept((__is_nothrow_swappable<_Types>::value && ...)
1640 && is_nothrow_move_constructible_v<variant>)
1642 static_assert((is_move_constructible_v<_Types> && ...));
1644 // Handle this here to simplify the visitation.
1645 if (__rhs.valueless_by_exception()) [[__unlikely__]]
1647 if (!this->valueless_by_exception()) [[__likely__]]
1652 namespace __variant = __detail::__variant;
1654 __variant::__raw_idx_visit(
1655 [this, &__rhs](auto&& __rhs_mem, auto __rhs_index) mutable
1657 constexpr size_t __j = __rhs_index;
1658 if constexpr (__j != variant_npos)
1660 if (this->index() == __j)
1663 swap(std::get<__j>(*this), __rhs_mem);
1667 auto __tmp(std::move(__rhs_mem));
1669 if constexpr (_Traits::_S_trivial_move_assign)
1670 __rhs = std::move(*this);
1672 __variant::__raw_idx_visit(
1673 [&__rhs](auto&& __this_mem, auto __this_index) mutable
1675 constexpr size_t __k = __this_index;
1676 if constexpr (__k != variant_npos)
1677 __variant::__emplace<__k>(__rhs,
1678 std::move(__this_mem));
1681 __variant::__emplace<__j>(*this, std::move(__tmp));
1687#if defined(__clang__) && __clang_major__ <= 7
1689 using _Base::_M_u; // See https://bugs.llvm.org/show_bug.cgi?id=31852
1693 template<size_t _Np, typename _Vp>
1694 friend constexpr decltype(auto)
1695 __detail::__variant::__get(_Vp&& __v) noexcept;
1697#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
1698 template<typename... _Tp> \
1699 friend constexpr bool \
1700 operator __OP(const variant<_Tp...>& __lhs, \
1701 const variant<_Tp...>& __rhs);
1703 _VARIANT_RELATION_FUNCTION_TEMPLATE(<)
1704 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
1705 _VARIANT_RELATION_FUNCTION_TEMPLATE(==)
1706 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
1707 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
1708 _VARIANT_RELATION_FUNCTION_TEMPLATE(>)
1710#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1713 template<size_t _Np, typename... _Types>
1714 constexpr variant_alternative_t<_Np, variant<_Types...>>&
1715 get(variant<_Types...>& __v)
1717 static_assert(_Np < sizeof...(_Types),
1718 "The index must be in [0, number of alternatives)");
1719 if (__v.index() != _Np)
1720 __throw_bad_variant_access(__v.valueless_by_exception());
1721 return __detail::__variant::__get<_Np>(__v);
1724 template<size_t _Np, typename... _Types>
1725 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1726 get(variant<_Types...>&& __v)
1728 static_assert(_Np < sizeof...(_Types),
1729 "The index must be in [0, number of alternatives)");
1730 if (__v.index() != _Np)
1731 __throw_bad_variant_access(__v.valueless_by_exception());
1732 return __detail::__variant::__get<_Np>(std::move(__v));
1735 template<size_t _Np, typename... _Types>
1736 constexpr const variant_alternative_t<_Np, variant<_Types...>>&
1737 get(const variant<_Types...>& __v)
1739 static_assert(_Np < sizeof...(_Types),
1740 "The index must be in [0, number of alternatives)");
1741 if (__v.index() != _Np)
1742 __throw_bad_variant_access(__v.valueless_by_exception());
1743 return __detail::__variant::__get<_Np>(__v);
1746 template<size_t _Np, typename... _Types>
1747 constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
1748 get(const variant<_Types...>&& __v)
1750 static_assert(_Np < sizeof...(_Types),
1751 "The index must be in [0, number of alternatives)");
1752 if (__v.index() != _Np)
1753 __throw_bad_variant_access(__v.valueless_by_exception());
1754 return __detail::__variant::__get<_Np>(std::move(__v));
1757 /// @cond undocumented
1758 template<typename _Result_type, typename _Visitor, typename... _Variants>
1759 constexpr decltype(auto)
1760 __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
1762 // Get the silly case of visiting no variants out of the way first.
1763 if constexpr (sizeof...(_Variants) == 0)
1765 if constexpr (is_void_v<_Result_type>)
1766 return (void) std::forward<_Visitor>(__visitor)();
1768 return std::forward<_Visitor>(__visitor)();
1772 constexpr size_t __max = 11; // "These go to eleven."
1774 // The type of the first variant in the pack.
1775 using _V0 = typename _Nth_type<0, _Variants...>::type;
1776 // The number of alternatives in that first variant.
1777 constexpr auto __n = variant_size_v<remove_reference_t<_V0>>;
1779 if constexpr (sizeof...(_Variants) > 1 || __n > __max)
1781 // Use a jump table for the general case.
1782 constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1783 _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1785 auto __func_ptr = __vtable._M_access(__variants.index()...);
1786 return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1787 std::forward<_Variants>(__variants)...);
1789 else // We have a single variant with a small number of alternatives.
1791 // A name for the first variant in the pack.
1793 = [](_V0& __v, ...) -> _V0& { return __v; }(__variants...);
1795 using __detail::__variant::_Multi_array;
1796 using __detail::__variant::__gen_vtable_impl;
1797 using _Ma = _Multi_array<_Result_type (*)(_Visitor&&, _V0&&)>;
1799#ifdef _GLIBCXX_DEBUG
1800# define _GLIBCXX_VISIT_UNREACHABLE __builtin_trap
1802# define _GLIBCXX_VISIT_UNREACHABLE __builtin_unreachable
1805#define _GLIBCXX_VISIT_CASE(N) \
1808 if constexpr (N < __n) \
1810 return __gen_vtable_impl<_Ma, index_sequence<N>>:: \
1811 __visit_invoke(std::forward<_Visitor>(__visitor), \
1812 std::forward<_V0>(__v0)); \
1814 else _GLIBCXX_VISIT_UNREACHABLE(); \
1817 switch (__v0.index())
1819 _GLIBCXX_VISIT_CASE(0)
1820 _GLIBCXX_VISIT_CASE(1)
1821 _GLIBCXX_VISIT_CASE(2)
1822 _GLIBCXX_VISIT_CASE(3)
1823 _GLIBCXX_VISIT_CASE(4)
1824 _GLIBCXX_VISIT_CASE(5)
1825 _GLIBCXX_VISIT_CASE(6)
1826 _GLIBCXX_VISIT_CASE(7)
1827 _GLIBCXX_VISIT_CASE(8)
1828 _GLIBCXX_VISIT_CASE(9)
1829 _GLIBCXX_VISIT_CASE(10)
1831 using __detail::__variant::__variant_idx_cookie;
1832 using __detail::__variant::__variant_cookie;
1833 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>
1834 || is_same_v<_Result_type, __variant_cookie>)
1836 using _Npos = index_sequence<variant_npos>;
1837 return __gen_vtable_impl<_Ma, _Npos>::
1838 __visit_invoke(std::forward<_Visitor>(__visitor),
1839 std::forward<_V0>(__v0));
1842 _GLIBCXX_VISIT_UNREACHABLE();
1844 _GLIBCXX_VISIT_UNREACHABLE();
1846#undef _GLIBCXX_VISIT_CASE
1847#undef _GLIBCXX_VISIT_UNREACHABLE
1853 template<typename _Visitor, typename... _Variants>
1854 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1855 visit(_Visitor&& __visitor, _Variants&&... __variants)
1857 namespace __variant = std::__detail::__variant;
1859 if ((__variant::__as(__variants).valueless_by_exception() || ...))
1860 __throw_bad_variant_access("std::visit: variant is valueless");
1863 = __detail::__variant::__visit_result_t<_Visitor, _Variants...>;
1865 using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
1867 if constexpr (sizeof...(_Variants) == 1)
1869 using _Vp = decltype(__variant::__as(std::declval<_Variants>()...));
1871 constexpr bool __visit_rettypes_match = __detail::__variant::
1872 __check_visitor_results<_Visitor, _Vp>(
1873 make_index_sequence<variant_size_v<remove_reference_t<_Vp>>>());
1874 if constexpr (!__visit_rettypes_match)
1876 static_assert(__visit_rettypes_match,
1877 "std::visit requires the visitor to have the same "
1878 "return type for all alternatives of a variant");
1882 return std::__do_visit<_Tag>(
1883 std::forward<_Visitor>(__visitor),
1884 static_cast<_Vp>(__variants)...);
1887 return std::__do_visit<_Tag>(
1888 std::forward<_Visitor>(__visitor),
1889 __variant::__as(std::forward<_Variants>(__variants))...);
1892#if __cplusplus > 201703L
1893 template<typename _Res, typename _Visitor, typename... _Variants>
1895 visit(_Visitor&& __visitor, _Variants&&... __variants)
1897 namespace __variant = std::__detail::__variant;
1899 if ((__variant::__as(__variants).valueless_by_exception() || ...))
1900 __throw_bad_variant_access("std::visit<R>: variant is valueless");
1902 return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
1903 __variant::__as(std::forward<_Variants>(__variants))...);
1907 /// @cond undocumented
1908 template<bool, typename... _Types>
1909 struct __variant_hash_call_base_impl
1912 operator()(const variant<_Types...>& __t) const
1913 noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
1916 __detail::__variant::__raw_visit(
1917 [&__t, &__ret](auto&& __t_mem) mutable
1919 using _Type = __remove_cvref_t<decltype(__t_mem)>;
1920 if constexpr (!is_same_v<_Type,
1921 __detail::__variant::__variant_cookie>)
1922 __ret = std::hash<size_t>{}(__t.index())
1923 + std::hash<_Type>{}(__t_mem);
1925 __ret = std::hash<size_t>{}(__t.index());
1931 template<typename... _Types>
1932 struct __variant_hash_call_base_impl<false, _Types...> {};
1934 template<typename... _Types>
1935 using __variant_hash_call_base =
1936 __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
1937 __enable_hash_call &&...), _Types...>;
1940 template<typename... _Types>
1941 struct hash<variant<_Types...>>
1942 : private __detail::__variant::_Variant_hash_base<
1943 variant<_Types...>, std::index_sequence_for<_Types...>>,
1944 public __variant_hash_call_base<_Types...>
1946 using result_type [[__deprecated__]] = size_t;
1947 using argument_type [[__deprecated__]] = variant<_Types...>;
1951 struct hash<monostate>
1953 using result_type [[__deprecated__]] = size_t;
1954 using argument_type [[__deprecated__]] = monostate;
1957 operator()(const monostate&) const noexcept
1959 constexpr size_t __magic_monostate_hash = -7777;
1960 return __magic_monostate_hash;
1964 template<typename... _Types>
1965 struct __is_fast_hash<hash<variant<_Types...>>>
1966 : bool_constant<(__is_fast_hash<_Types>::value && ...)>
1969_GLIBCXX_END_NAMESPACE_VERSION
1974#endif // _GLIBCXX_VARIANT