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 template<typename... _Types>
326 static constexpr bool _S_default_ctor =
327 is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
328 static constexpr bool _S_copy_ctor =
329 (is_copy_constructible_v<_Types> && ...);
330 static constexpr bool _S_move_ctor =
331 (is_move_constructible_v<_Types> && ...);
332 static constexpr bool _S_copy_assign =
334 && (is_copy_assignable_v<_Types> && ...);
335 static constexpr bool _S_move_assign =
337 && (is_move_assignable_v<_Types> && ...);
339 static constexpr bool _S_trivial_dtor =
340 (is_trivially_destructible_v<_Types> && ...);
341 static constexpr bool _S_trivial_copy_ctor =
342 (is_trivially_copy_constructible_v<_Types> && ...);
343 static constexpr bool _S_trivial_move_ctor =
344 (is_trivially_move_constructible_v<_Types> && ...);
345 static constexpr bool _S_trivial_copy_assign =
346 _S_trivial_dtor && _S_trivial_copy_ctor
347 && (is_trivially_copy_assignable_v<_Types> && ...);
348 static constexpr bool _S_trivial_move_assign =
349 _S_trivial_dtor && _S_trivial_move_ctor
350 && (is_trivially_move_assignable_v<_Types> && ...);
352 // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
353 // are always nothrow.
354 static constexpr bool _S_nothrow_default_ctor =
355 is_nothrow_default_constructible_v<
356 typename _Nth_type<0, _Types...>::type>;
357 static constexpr bool _S_nothrow_copy_ctor = false;
358 static constexpr bool _S_nothrow_move_ctor =
359 (is_nothrow_move_constructible_v<_Types> && ...);
360 static constexpr bool _S_nothrow_copy_assign = false;
361 static constexpr bool _S_nothrow_move_assign =
363 && (is_nothrow_move_assignable_v<_Types> && ...);
366 // Defines members and ctors.
367 template<typename... _Types>
368 union _Variadic_union
370 _Variadic_union() = default;
372 template<size_t _Np, typename... _Args>
373 _Variadic_union(in_place_index_t<_Np>, _Args&&...) = delete;
376 template<typename _First, typename... _Rest>
377 union _Variadic_union<_First, _Rest...>
379 constexpr _Variadic_union() : _M_rest() { }
381 template<typename... _Args>
383 _Variadic_union(in_place_index_t<0>, _Args&&... __args)
384 : _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
387 template<size_t _Np, typename... _Args>
389 _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
390 : _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
393#if __cpp_lib_variant >= 202106L
394 _Variadic_union(const _Variadic_union&) = default;
395 _Variadic_union(_Variadic_union&&) = default;
396 _Variadic_union& operator=(const _Variadic_union&) = default;
397 _Variadic_union& operator=(_Variadic_union&&) = default;
399 ~_Variadic_union() = default;
401 constexpr ~_Variadic_union()
402 requires (!__has_trivial_destructor(_First))
403 || (!__has_trivial_destructor(_Variadic_union<_Rest...>))
407 _Uninitialized<_First> _M_first;
408 _Variadic_union<_Rest...> _M_rest;
411 // _Never_valueless_alt is true for variant alternatives that can
412 // always be placed in a variant without it becoming valueless.
414 // For suitably-small, trivially copyable types we can create temporaries
415 // on the stack and then memcpy them into place.
416 template<typename _Tp>
417 struct _Never_valueless_alt
418 : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
421 // Specialize _Never_valueless_alt for other types which have a
422 // non-throwing and cheap move construction and move assignment operator,
423 // so that emplacing the type will provide the strong exception-safety
424 // guarantee, by creating and moving a temporary.
425 // Whether _Never_valueless_alt<T> is true or not affects the ABI of a
426 // variant using that alternative, so we can't change the value later!
428 // True if every alternative in _Types... can be emplaced in a variant
429 // without it becoming valueless. If this is true, variant<_Types...>
430 // can never be valueless, which enables some minor optimizations.
431 template <typename... _Types>
432 constexpr bool __never_valueless()
434 return _Traits<_Types...>::_S_move_assign
435 && (_Never_valueless_alt<_Types>::value && ...);
438 // Defines index and the dtor, possibly trivial.
439 template<bool __trivially_destructible, typename... _Types>
440 struct _Variant_storage;
442 template <typename... _Types>
443 using __select_index =
444 typename __select_int::_Select_int_base<sizeof...(_Types),
446 unsigned short>::type::value_type;
448 template<typename... _Types>
449 struct _Variant_storage<false, _Types...>
453 : _M_index(static_cast<__index_type>(variant_npos))
456 template<size_t _Np, typename... _Args>
458 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
459 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
466 if (!_M_valid()) [[unlikely]]
469 std::__do_visit<void>([](auto&& __this_mem) mutable
471 std::_Destroy(std::__addressof(__this_mem));
472 }, __variant_cast<_Types...>(*this));
474 _M_index = static_cast<__index_type>(variant_npos);
482 _M_valid() const noexcept
484 if constexpr (__variant::__never_valueless<_Types...>())
486 return this->_M_index != __index_type(variant_npos);
489 _Variadic_union<_Types...> _M_u;
490 using __index_type = __select_index<_Types...>;
491 __index_type _M_index;
494 template<typename... _Types>
495 struct _Variant_storage<true, _Types...>
499 : _M_index(static_cast<__index_type>(variant_npos))
502 template<size_t _Np, typename... _Args>
504 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
505 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
511 { _M_index = static_cast<__index_type>(variant_npos); }
514 _M_valid() const noexcept
516 if constexpr (__variant::__never_valueless<_Types...>())
518 // It would be nice if we could just return true for -fno-exceptions.
519 // It's possible (but inadvisable) that a std::variant could become
520 // valueless in a translation unit compiled with -fexceptions and then
521 // be passed to functions compiled with -fno-exceptions. We would need
522 // some #ifdef _GLIBCXX_NO_EXCEPTIONS_GLOBALLY property to elide all
523 // checks for valueless_by_exception().
524 return this->_M_index != static_cast<__index_type>(variant_npos);
527 _Variadic_union<_Types...> _M_u;
528 using __index_type = __select_index<_Types...>;
529 __index_type _M_index;
532 // Implementation of v.emplace<N>(args...).
533 template<size_t _Np, bool _Triv, typename... _Types, typename... _Args>
536 __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
539 auto* __addr = std::__addressof(__variant::__get_n<_Np>(__v._M_u));
540 std::_Construct(__addr, std::forward<_Args>(__args)...);
541 // Construction didn't throw, so can set the new index now:
545 template<typename... _Types>
546 using _Variant_storage_alias =
547 _Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
549 // The following are (Copy|Move) (ctor|assign) layers for forwarding
550 // triviality and handling non-trivial SMF behaviors.
552 template<bool, typename... _Types>
553 struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
555 using _Base = _Variant_storage_alias<_Types...>;
559 _Copy_ctor_base(const _Copy_ctor_base& __rhs)
560 noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
562 __variant::__raw_idx_visit(
563 [this](auto&& __rhs_mem, auto __rhs_index) mutable
565 constexpr size_t __j = __rhs_index;
566 if constexpr (__j != variant_npos)
567 std::_Construct(std::__addressof(this->_M_u),
568 in_place_index<__j>, __rhs_mem);
569 }, __variant_cast<_Types...>(__rhs));
570 this->_M_index = __rhs._M_index;
573 _Copy_ctor_base(_Copy_ctor_base&&) = default;
574 _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
575 _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
578 template<typename... _Types>
579 struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
581 using _Base = _Variant_storage_alias<_Types...>;
585 template<typename... _Types>
586 using _Copy_ctor_alias =
587 _Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
589 template<bool, typename... _Types>
590 struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
592 using _Base = _Copy_ctor_alias<_Types...>;
596 _Move_ctor_base(_Move_ctor_base&& __rhs)
597 noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
599 __variant::__raw_idx_visit(
600 [this](auto&& __rhs_mem, auto __rhs_index) mutable
602 constexpr size_t __j = __rhs_index;
603 if constexpr (__j != variant_npos)
604 std::_Construct(std::__addressof(this->_M_u),
606 std::forward<decltype(__rhs_mem)>(__rhs_mem));
607 }, __variant_cast<_Types...>(std::move(__rhs)));
608 this->_M_index = __rhs._M_index;
611 _Move_ctor_base(const _Move_ctor_base&) = default;
612 _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
613 _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
616 template<typename... _Types>
617 struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
619 using _Base = _Copy_ctor_alias<_Types...>;
623 template<typename... _Types>
624 using _Move_ctor_alias =
625 _Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
627 template<bool, typename... _Types>
628 struct _Copy_assign_base : _Move_ctor_alias<_Types...>
630 using _Base = _Move_ctor_alias<_Types...>;
635 operator=(const _Copy_assign_base& __rhs)
636 noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
638 __variant::__raw_idx_visit(
639 [this](auto&& __rhs_mem, auto __rhs_index) mutable
641 constexpr size_t __j = __rhs_index;
642 if constexpr (__j == variant_npos)
643 this->_M_reset(); // Make *this valueless.
644 else if (this->_M_index == __j)
645 __variant::__get<__j>(*this) = __rhs_mem;
648 using _Tj = typename _Nth_type<__j, _Types...>::type;
649 if constexpr (is_nothrow_copy_constructible_v<_Tj>
650 || !is_nothrow_move_constructible_v<_Tj>)
651 __variant::__emplace<__j>(*this, __rhs_mem);
654 using _Variant = variant<_Types...>;
655 _Variant& __self = __variant_cast<_Types...>(*this);
656 __self = _Variant(in_place_index<__j>, __rhs_mem);
659 }, __variant_cast<_Types...>(__rhs));
663 _Copy_assign_base(const _Copy_assign_base&) = default;
664 _Copy_assign_base(_Copy_assign_base&&) = default;
665 _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
668 template<typename... _Types>
669 struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
671 using _Base = _Move_ctor_alias<_Types...>;
675 template<typename... _Types>
676 using _Copy_assign_alias =
677 _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
679 template<bool, typename... _Types>
680 struct _Move_assign_base : _Copy_assign_alias<_Types...>
682 using _Base = _Copy_assign_alias<_Types...>;
687 operator=(_Move_assign_base&& __rhs)
688 noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
690 __variant::__raw_idx_visit(
691 [this](auto&& __rhs_mem, auto __rhs_index) mutable
693 constexpr size_t __j = __rhs_index;
694 if constexpr (__j != variant_npos)
696 if (this->_M_index == __j)
697 __variant::__get<__j>(*this) = std::move(__rhs_mem);
700 using _Tj = typename _Nth_type<__j, _Types...>::type;
701 if constexpr (is_nothrow_move_constructible_v<_Tj>)
702 __variant::__emplace<__j>(*this, std::move(__rhs_mem));
705 using _Variant = variant<_Types...>;
706 _Variant& __self = __variant_cast<_Types...>(*this);
707 __self.template emplace<__j>(std::move(__rhs_mem));
713 }, __variant_cast<_Types...>(__rhs));
717 _Move_assign_base(const _Move_assign_base&) = default;
718 _Move_assign_base(_Move_assign_base&&) = default;
719 _Move_assign_base& operator=(const _Move_assign_base&) = default;
722 template<typename... _Types>
723 struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
725 using _Base = _Copy_assign_alias<_Types...>;
729 template<typename... _Types>
730 using _Move_assign_alias =
731 _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
733 template<typename... _Types>
734 struct _Variant_base : _Move_assign_alias<_Types...>
736 using _Base = _Move_assign_alias<_Types...>;
739 _Variant_base() noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
740 : _Variant_base(in_place_index<0>) { }
742 template<size_t _Np, typename... _Args>
744 _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
745 : _Base(__i, std::forward<_Args>(__args)...)
748 _Variant_base(const _Variant_base&) = default;
749 _Variant_base(_Variant_base&&) = default;
750 _Variant_base& operator=(const _Variant_base&) = default;
751 _Variant_base& operator=(_Variant_base&&) = default;
754 template<typename _Tp, typename... _Types>
755 inline constexpr bool __exactly_once
756 = std::__find_uniq_type_in_pack<_Tp, _Types...>() < sizeof...(_Types);
758 // Helper used to check for valid conversions that don't involve narrowing.
759 template<typename _Ti> struct _Arr { _Ti _M_x[1]; };
761 // "Build an imaginary function FUN(Ti) for each alternative type Ti"
762 template<size_t _Ind, typename _Tp, typename _Ti, typename = void>
765 // This function means 'using _Build_FUN<I, T, Ti>::_S_fun;' is valid,
766 // but only static functions will be considered in the call below.
767 void _S_fun() = delete;
770 // "... for which Ti x[] = {std::forward<T>(t)}; is well-formed."
771 template<size_t _Ind, typename _Tp, typename _Ti>
772 struct _Build_FUN<_Ind, _Tp, _Ti,
773 void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
775 // This is the FUN function for type _Ti, with index _Ind
776 static integral_constant<size_t, _Ind> _S_fun(_Ti);
779 template<typename _Tp, typename _Variant,
780 typename = make_index_sequence<variant_size_v<_Variant>>>
783 template<typename _Tp, typename... _Ti, size_t... _Ind>
784 struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
785 : _Build_FUN<_Ind, _Tp, _Ti>...
787 using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
790 // The index j of the overload FUN(Tj) selected by overload resolution
791 // for FUN(std::forward<_Tp>(t))
792 template<typename _Tp, typename _Variant>
794 = decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
796 // The index selected for FUN(std::forward<T>(t)), or variant_npos if none.
797 template<typename _Tp, typename _Variant, typename = void>
798 inline constexpr size_t
799 __accepted_index = variant_npos;
801 template<typename _Tp, typename _Variant>
802 inline constexpr size_t
803 __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
804 = _FUN_type<_Tp, _Variant>::value;
806 template<typename _Maybe_variant_cookie, typename _Variant,
807 typename = __remove_cvref_t<_Variant>>
808 inline constexpr bool
809 __extra_visit_slot_needed = false;
811 template<typename _Var, typename... _Types>
812 inline constexpr bool
813 __extra_visit_slot_needed<__variant_cookie, _Var, variant<_Types...>>
814 = !__variant::__never_valueless<_Types...>();
816 template<typename _Var, typename... _Types>
817 inline constexpr bool
818 __extra_visit_slot_needed<__variant_idx_cookie, _Var, variant<_Types...>>
819 = !__variant::__never_valueless<_Types...>();
821 // Used for storing a multi-dimensional vtable.
822 template<typename _Tp, size_t... _Dimensions>
825 // Partial specialization with rank zero, stores a single _Tp element.
826 template<typename _Tp>
827 struct _Multi_array<_Tp>
830 struct __untag_result
832 { using element_type = _Tp; };
834#pragma GCC diagnostic push
835#pragma GCC diagnostic ignored "-Wignored-qualifiers"
836 template <typename... _Args>
837 struct __untag_result<const void(*)(_Args...)>
839 { using element_type = void(*)(_Args...); };
840#pragma GCC diagnostic pop
842 template <typename... _Args>
843 struct __untag_result<__variant_cookie(*)(_Args...)>
845 { using element_type = void(*)(_Args...); };
847 template <typename... _Args>
848 struct __untag_result<__variant_idx_cookie(*)(_Args...)>
850 { using element_type = void(*)(_Args...); };
852 template <typename _Res, typename... _Args>
853 struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
855 { using element_type = _Res(*)(_Args...); };
857 using __result_is_deduced = __untag_result<_Tp>;
859 constexpr const typename __untag_result<_Tp>::element_type&
863 typename __untag_result<_Tp>::element_type _M_data;
866 // Partial specialization with rank >= 1.
867 template<typename _Ret,
869 typename... _Variants,
870 size_t __first, size_t... __rest>
871 struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
873 static constexpr size_t __index =
874 sizeof...(_Variants) - sizeof...(__rest) - 1;
876 using _Variant = typename _Nth_type<__index, _Variants...>::type;
878 static constexpr int __do_cookie =
879 __extra_visit_slot_needed<_Ret, _Variant> ? 1 : 0;
881 using _Tp = _Ret(*)(_Visitor, _Variants...);
883 template<typename... _Args>
884 constexpr decltype(auto)
885 _M_access(size_t __first_index, _Args... __rest_indices) const
887 return _M_arr[__first_index + __do_cookie]
888 ._M_access(__rest_indices...);
891 _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
894 // Creates a multi-dimensional vtable recursively.
897 // visit([](auto, auto){},
898 // variant<int, char>(), // typedef'ed as V1
899 // variant<float, double, long double>()) // typedef'ed as V2
900 // will trigger instantiations of:
901 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
902 // tuple<V1&&, V2&&>, std::index_sequence<>>
903 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
904 // tuple<V1&&, V2&&>, std::index_sequence<0>>
905 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
906 // tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
907 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
908 // tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
909 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
910 // tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
911 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
912 // tuple<V1&&, V2&&>, std::index_sequence<1>>
913 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
914 // tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
915 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
916 // tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
917 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
918 // tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
919 // The returned multi-dimensional vtable can be fast accessed by the visitor
920 // using index calculation.
921 template<typename _Array_type, typename _Index_seq>
922 struct __gen_vtable_impl;
924 // Defines the _S_apply() member that returns a _Multi_array populated
925 // with function pointers that perform the visitation expressions e(m)
926 // for each valid pack of indexes into the variant types _Variants.
928 // This partial specialization builds up the index sequences by recursively
929 // calling _S_apply() on the next specialization of __gen_vtable_impl.
930 // The base case of the recursion defines the actual function pointers.
931 template<typename _Result_type, typename _Visitor, size_t... __dimensions,
932 typename... _Variants, size_t... __indices>
933 struct __gen_vtable_impl<
934 _Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
935 std::index_sequence<__indices...>>
938 remove_reference_t<typename _Nth_type<sizeof...(__indices),
939 _Variants...>::type>;
941 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
944 static constexpr _Array_type
947 _Array_type __vtable{};
949 __vtable, make_index_sequence<variant_size_v<_Next>>());
953 template<size_t... __var_indices>
954 static constexpr void
955 _S_apply_all_alts(_Array_type& __vtable,
956 std::index_sequence<__var_indices...>)
958 if constexpr (__extra_visit_slot_needed<_Result_type, _Next>)
959 (_S_apply_single_alt<true, __var_indices>(
960 __vtable._M_arr[__var_indices + 1],
961 &(__vtable._M_arr[0])), ...);
963 (_S_apply_single_alt<false, __var_indices>(
964 __vtable._M_arr[__var_indices]), ...);
967 template<bool __do_cookie, size_t __index, typename _Tp>
968 static constexpr void
969 _S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
971 if constexpr (__do_cookie)
973 __element = __gen_vtable_impl<
975 std::index_sequence<__indices..., __index>>::_S_apply();
976 *__cookie_element = __gen_vtable_impl<
978 std::index_sequence<__indices..., variant_npos>>::_S_apply();
982 auto __tmp_element = __gen_vtable_impl<
983 remove_reference_t<decltype(__element)>,
984 std::index_sequence<__indices..., __index>>::_S_apply();
985 static_assert(is_same_v<_Tp, decltype(__tmp_element)>,
986 "std::visit requires the visitor to have the same "
987 "return type for all alternatives of a variant");
988 __element = __tmp_element;
993 // This partial specialization is the base case for the recursion.
994 // It populates a _Multi_array element with the address of a function
995 // that invokes the visitor with the alternatives specified by __indices.
996 template<typename _Result_type, typename _Visitor, typename... _Variants,
998 struct __gen_vtable_impl<
999 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
1000 std::index_sequence<__indices...>>
1003 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
1005 template<size_t __index, typename _Variant>
1006 static constexpr decltype(auto)
1007 __element_by_index_or_cookie(_Variant&& __var) noexcept
1009 if constexpr (__index != variant_npos)
1010 return __variant::__get<__index>(std::forward<_Variant>(__var));
1012 return __variant_cookie{};
1015 static constexpr decltype(auto)
1016 __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
1018 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
1019 // For raw visitation using indices, pass the indices to the visitor
1020 // and discard the return value:
1021 std::__invoke(std::forward<_Visitor>(__visitor),
1022 __element_by_index_or_cookie<__indices>(
1023 std::forward<_Variants>(__vars))...,
1024 integral_constant<size_t, __indices>()...);
1025 else if constexpr (is_same_v<_Result_type, __variant_cookie>)
1026 // For raw visitation without indices, and discard the return value:
1027 std::__invoke(std::forward<_Visitor>(__visitor),
1028 __element_by_index_or_cookie<__indices>(
1029 std::forward<_Variants>(__vars))...);
1030 else if constexpr (_Array_type::__result_is_deduced::value)
1031 // For the usual std::visit case deduce the return value:
1032 return std::__invoke(std::forward<_Visitor>(__visitor),
1033 __element_by_index_or_cookie<__indices>(
1034 std::forward<_Variants>(__vars))...);
1035 else // for std::visit<R> use INVOKE<R>
1036 return std::__invoke_r<_Result_type>(
1037 std::forward<_Visitor>(__visitor),
1038 __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
1041 static constexpr auto
1044 if constexpr (_Array_type::__result_is_deduced::value)
1046 constexpr bool __visit_ret_type_mismatch =
1047 !is_same_v<typename _Result_type::type,
1048 decltype(__visit_invoke(std::declval<_Visitor>(),
1049 std::declval<_Variants>()...))>;
1050 if constexpr (__visit_ret_type_mismatch)
1052 struct __cannot_match {};
1053 return __cannot_match{};
1056 return _Array_type{&__visit_invoke};
1059 return _Array_type{&__visit_invoke};
1063 template<typename _Result_type, typename _Visitor, typename... _Variants>
1067 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
1068 variant_size_v<remove_reference_t<_Variants>>...>;
1070 static constexpr _Array_type _S_vtable
1071 = __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
1074 template<size_t _Np, typename _Tp>
1075 struct _Base_dedup : public _Tp { };
1077 template<typename _Variant, typename __indices>
1078 struct _Variant_hash_base;
1080 template<typename... _Types, size_t... __indices>
1081 struct _Variant_hash_base<variant<_Types...>,
1082 std::index_sequence<__indices...>>
1083 : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
1085 // Equivalent to decltype(get<_Np>(as-variant(declval<_Variant>())))
1086 template<size_t _Np, typename _Variant,
1087 typename _AsV = decltype(__variant::__as(std::declval<_Variant>())),
1088 typename _Tp = variant_alternative_t<_Np, remove_reference_t<_AsV>>>
1090 = __conditional_t<is_lvalue_reference_v<_Variant>, _Tp&, _Tp&&>;
1092 // Return type of std::visit.
1093 template<typename _Visitor, typename... _Variants>
1094 using __visit_result_t
1095 = invoke_result_t<_Visitor, __get_t<0, _Variants>...>;
1097 template<typename _Tp, typename... _Types>
1098 constexpr inline bool __same_types = (is_same_v<_Tp, _Types> && ...);
1100 template <typename _Visitor, typename _Variant, size_t... _Idxs>
1101 constexpr bool __check_visitor_results(std::index_sequence<_Idxs...>)
1103 return __same_types<
1104 invoke_result_t<_Visitor, __get_t<_Idxs, _Variant>>...
1108} // namespace __variant
1109} // namespace __detail
1111 template<typename _Tp, typename... _Types>
1113 holds_alternative(const variant<_Types...>& __v) noexcept
1115 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1116 "T must occur exactly once in alternatives");
1117 return __v.index() == std::__find_uniq_type_in_pack<_Tp, _Types...>();
1120 template<typename _Tp, typename... _Types>
1122 get(variant<_Types...>& __v)
1124 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1125 "T must occur exactly once in alternatives");
1126 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1127 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1128 return std::get<__n>(__v);
1131 template<typename _Tp, typename... _Types>
1133 get(variant<_Types...>&& __v)
1135 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1136 "T must occur exactly once in alternatives");
1137 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1138 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1139 return std::get<__n>(std::move(__v));
1142 template<typename _Tp, typename... _Types>
1143 constexpr const _Tp&
1144 get(const variant<_Types...>& __v)
1146 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1147 "T must occur exactly once in alternatives");
1148 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1149 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1150 return std::get<__n>(__v);
1153 template<typename _Tp, typename... _Types>
1154 constexpr const _Tp&&
1155 get(const variant<_Types...>&& __v)
1157 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1158 "T must occur exactly once in alternatives");
1159 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1160 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1161 return std::get<__n>(std::move(__v));
1164 template<size_t _Np, typename... _Types>
1165 constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
1166 get_if(variant<_Types...>* __ptr) noexcept
1168 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1169 static_assert(_Np < sizeof...(_Types),
1170 "The index must be in [0, number of alternatives)");
1171 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1172 if (__ptr && __ptr->index() == _Np)
1173 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1177 template<size_t _Np, typename... _Types>
1179 add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
1180 get_if(const variant<_Types...>* __ptr) noexcept
1182 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1183 static_assert(_Np < sizeof...(_Types),
1184 "The index must be in [0, number of alternatives)");
1185 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1186 if (__ptr && __ptr->index() == _Np)
1187 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1191 template<typename _Tp, typename... _Types>
1192 constexpr add_pointer_t<_Tp>
1193 get_if(variant<_Types...>* __ptr) noexcept
1195 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1196 "T must occur exactly once in alternatives");
1197 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1198 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1199 return std::get_if<__n>(__ptr);
1202 template<typename _Tp, typename... _Types>
1203 constexpr add_pointer_t<const _Tp>
1204 get_if(const variant<_Types...>* __ptr) noexcept
1206 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1207 "T must occur exactly once in alternatives");
1208 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1209 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1210 return std::get_if<__n>(__ptr);
1213 struct monostate { };
1215#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
1216 template<typename... _Types> \
1217 constexpr bool operator __OP(const variant<_Types...>& __lhs, \
1218 const variant<_Types...>& __rhs) \
1220 bool __ret = true; \
1221 __detail::__variant::__raw_idx_visit( \
1222 [&__ret, &__lhs] (auto&& __rhs_mem, auto __rhs_index) mutable \
1224 if constexpr (__rhs_index != variant_npos) \
1226 if (__lhs.index() == __rhs_index) \
1228 auto& __this_mem = std::get<__rhs_index>(__lhs); \
1229 __ret = __this_mem __OP __rhs_mem; \
1232 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1235 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1240 _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
1241 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
1242 _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
1243 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
1244 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
1245 _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
1247#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1249 constexpr bool operator==(monostate, monostate) noexcept { return true; }
1251#ifdef __cpp_lib_three_way_comparison
1252 template<typename... _Types>
1253 requires (three_way_comparable<_Types> && ...)
1255 common_comparison_category_t<compare_three_way_result_t<_Types>...>
1256 operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
1258 common_comparison_category_t<compare_three_way_result_t<_Types>...> __ret
1259 = strong_ordering::equal;
1261 __detail::__variant::__raw_idx_visit(
1262 [&__ret, &__v] (auto&& __w_mem, auto __w_index) mutable
1264 if constexpr (__w_index != variant_npos)
1266 if (__v.index() == __w_index)
1268 auto& __this_mem = std::get<__w_index>(__v);
1269 __ret = __this_mem <=> __w_mem;
1273 __ret = (__v.index() + 1) <=> (__w_index + 1);
1278 constexpr strong_ordering
1279 operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
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 false; }
1284 constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1285 constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1288 template<typename _Visitor, typename... _Variants>
1289 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1290 visit(_Visitor&&, _Variants&&...);
1292 template<typename... _Types>
1293 _GLIBCXX20_CONSTEXPR
1294 inline enable_if_t<(is_move_constructible_v<_Types> && ...)
1295 && (is_swappable_v<_Types> && ...)>
1296 swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1297 noexcept(noexcept(__lhs.swap(__rhs)))
1298 { __lhs.swap(__rhs); }
1300 template<typename... _Types>
1301 enable_if_t<!((is_move_constructible_v<_Types> && ...)
1302 && (is_swappable_v<_Types> && ...))>
1303 swap(variant<_Types...>&, variant<_Types...>&) = delete;
1305 class bad_variant_access : public exception
1308 bad_variant_access() noexcept { }
1310 const char* what() const noexcept override
1311 { return _M_reason; }
1314 bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
1316 // Must point to a string with static storage duration:
1317 const char* _M_reason = "bad variant access";
1319 friend void __throw_bad_variant_access(const char* __what);
1322 // Must only be called with a string literal
1324 __throw_bad_variant_access(const char* __what)
1325 { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
1328 __throw_bad_variant_access(bool __valueless)
1330 if (__valueless) [[__unlikely__]]
1331 __throw_bad_variant_access("std::get: variant is valueless");
1333 __throw_bad_variant_access("std::get: wrong index for variant");
1336 template<typename... _Types>
1338 : private __detail::__variant::_Variant_base<_Types...>,
1339 private _Enable_default_constructor<
1340 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1341 variant<_Types...>>,
1342 private _Enable_copy_move<
1343 __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1344 __detail::__variant::_Traits<_Types...>::_S_copy_assign,
1345 __detail::__variant::_Traits<_Types...>::_S_move_ctor,
1346 __detail::__variant::_Traits<_Types...>::_S_move_assign,
1350 template <typename... _UTypes, typename _Tp>
1351 friend _GLIBCXX20_CONSTEXPR decltype(auto)
1352 __variant_cast(_Tp&&);
1354 static_assert(sizeof...(_Types) > 0,
1355 "variant must have at least one alternative");
1356 static_assert(!(std::is_reference_v<_Types> || ...),
1357 "variant must have no reference alternative");
1358 static_assert(!(std::is_void_v<_Types> || ...),
1359 "variant must have no void alternative");
1361 using _Base = __detail::__variant::_Variant_base<_Types...>;
1362 using _Default_ctor_enabler =
1363 _Enable_default_constructor<
1364 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1365 variant<_Types...>>;
1367 template<typename _Tp>
1368 static constexpr bool __not_self
1369 = !is_same_v<__remove_cvref_t<_Tp>, variant>;
1371 template<typename _Tp>
1372 static constexpr bool
1373 __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1375 template<typename _Tp>
1376 static constexpr size_t __accepted_index
1377 = __detail::__variant::__accepted_index<_Tp, variant>;
1379 template<size_t _Np, typename = enable_if_t<(_Np < sizeof...(_Types))>>
1380 using __to_type = typename _Nth_type<_Np, _Types...>::type;
1382 template<typename _Tp, typename = enable_if_t<__not_self<_Tp>>>
1383 using __accepted_type = __to_type<__accepted_index<_Tp>>;
1385 template<typename _Tp>
1386 static constexpr size_t __index_of
1387 = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1389 using _Traits = __detail::__variant::_Traits<_Types...>;
1391 template<typename _Tp>
1392 struct __is_in_place_tag : false_type { };
1393 template<typename _Tp>
1394 struct __is_in_place_tag<in_place_type_t<_Tp>> : true_type { };
1395 template<size_t _Np>
1396 struct __is_in_place_tag<in_place_index_t<_Np>> : true_type { };
1398 template<typename _Tp>
1399 static constexpr bool __not_in_place_tag
1400 = !__is_in_place_tag<__remove_cvref_t<_Tp>>::value;
1403 variant() = default;
1404 variant(const variant& __rhs) = default;
1405 variant(variant&&) = default;
1406 variant& operator=(const variant&) = default;
1407 variant& operator=(variant&&) = default;
1408 _GLIBCXX20_CONSTEXPR ~variant() = default;
1410 template<typename _Tp,
1411 typename = enable_if_t<sizeof...(_Types) != 0>,
1412 typename = enable_if_t<__not_in_place_tag<_Tp>>,
1413 typename _Tj = __accepted_type<_Tp&&>,
1414 typename = enable_if_t<__exactly_once<_Tj>
1415 && is_constructible_v<_Tj, _Tp>>>
1418 noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
1419 : variant(in_place_index<__accepted_index<_Tp>>,
1420 std::forward<_Tp>(__t))
1423 template<typename _Tp, typename... _Args,
1424 typename = enable_if_t<__exactly_once<_Tp>
1425 && is_constructible_v<_Tp, _Args...>>>
1427 variant(in_place_type_t<_Tp>, _Args&&... __args)
1428 : variant(in_place_index<__index_of<_Tp>>,
1429 std::forward<_Args>(__args)...)
1432 template<typename _Tp, typename _Up, typename... _Args,
1433 typename = enable_if_t<__exactly_once<_Tp>
1434 && is_constructible_v<_Tp,
1435 initializer_list<_Up>&, _Args...>>>
1437 variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1439 : variant(in_place_index<__index_of<_Tp>>, __il,
1440 std::forward<_Args>(__args)...)
1443 template<size_t _Np, typename... _Args,
1444 typename _Tp = __to_type<_Np>,
1445 typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
1447 variant(in_place_index_t<_Np>, _Args&&... __args)
1448 : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
1449 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1452 template<size_t _Np, typename _Up, typename... _Args,
1453 typename _Tp = __to_type<_Np>,
1454 typename = enable_if_t<is_constructible_v<_Tp,
1455 initializer_list<_Up>&,
1458 variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1460 : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
1461 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1464 template<typename _Tp>
1465 _GLIBCXX20_CONSTEXPR
1466 enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1467 && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
1468 && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
1470 operator=(_Tp&& __rhs)
1471 noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
1472 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
1474 constexpr auto __index = __accepted_index<_Tp>;
1475 if (index() == __index)
1476 std::get<__index>(*this) = std::forward<_Tp>(__rhs);
1479 using _Tj = __accepted_type<_Tp&&>;
1480 if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
1481 || !is_nothrow_move_constructible_v<_Tj>)
1482 this->emplace<__index>(std::forward<_Tp>(__rhs));
1484 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1485 // 3585. converting assignment with immovable alternative
1486 this->emplace<__index>(_Tj(std::forward<_Tp>(__rhs)));
1491 template<typename _Tp, typename... _Args>
1492 _GLIBCXX20_CONSTEXPR
1493 enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1495 emplace(_Args&&... __args)
1497 constexpr size_t __index = __index_of<_Tp>;
1498 return this->emplace<__index>(std::forward<_Args>(__args)...);
1501 template<typename _Tp, typename _Up, typename... _Args>
1502 _GLIBCXX20_CONSTEXPR
1503 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1504 && __exactly_once<_Tp>,
1506 emplace(initializer_list<_Up> __il, _Args&&... __args)
1508 constexpr size_t __index = __index_of<_Tp>;
1509 return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
1512 template<size_t _Np, typename... _Args>
1513 _GLIBCXX20_CONSTEXPR
1514 enable_if_t<is_constructible_v<__to_type<_Np>, _Args...>,
1516 emplace(_Args&&... __args)
1518 namespace __variant = std::__detail::__variant;
1519 using type = typename _Nth_type<_Np, _Types...>::type;
1520 // Provide the strong exception-safety guarantee when possible,
1521 // to avoid becoming valueless.
1522 if constexpr (is_nothrow_constructible_v<type, _Args...>)
1524 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1526 else if constexpr (is_scalar_v<type>)
1528 // This might invoke a potentially-throwing conversion operator:
1529 const type __tmp(std::forward<_Args>(__args)...);
1530 // But this won't throw:
1531 __variant::__emplace<_Np>(*this, __tmp);
1533 else if constexpr (__variant::_Never_valueless_alt<type>()
1534 && _Traits::_S_move_assign)
1536 // This construction might throw:
1537 variant __tmp(in_place_index<_Np>,
1538 std::forward<_Args>(__args)...);
1539 // But _Never_valueless_alt<type> means this won't:
1540 *this = std::move(__tmp);
1544 // This case only provides the basic exception-safety guarantee,
1545 // i.e. the variant can become valueless.
1546 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1548 return std::get<_Np>(*this);
1551 template<size_t _Np, typename _Up, typename... _Args>
1552 _GLIBCXX20_CONSTEXPR
1553 enable_if_t<is_constructible_v<__to_type<_Np>,
1554 initializer_list<_Up>&, _Args...>,
1556 emplace(initializer_list<_Up> __il, _Args&&... __args)
1558 namespace __variant = std::__detail::__variant;
1559 using type = typename _Nth_type<_Np, _Types...>::type;
1560 // Provide the strong exception-safety guarantee when possible,
1561 // to avoid becoming valueless.
1562 if constexpr (is_nothrow_constructible_v<type,
1563 initializer_list<_Up>&,
1566 __variant::__emplace<_Np>(*this, __il,
1567 std::forward<_Args>(__args)...);
1569 else if constexpr (__variant::_Never_valueless_alt<type>()
1570 && _Traits::_S_move_assign)
1572 // This construction might throw:
1573 variant __tmp(in_place_index<_Np>, __il,
1574 std::forward<_Args>(__args)...);
1575 // But _Never_valueless_alt<type> means this won't:
1576 *this = std::move(__tmp);
1580 // This case only provides the basic exception-safety guarantee,
1581 // i.e. the variant can become valueless.
1582 __variant::__emplace<_Np>(*this, __il,
1583 std::forward<_Args>(__args)...);
1585 return std::get<_Np>(*this);
1588 template<size_t _Np, typename... _Args>
1589 enable_if_t<!(_Np < sizeof...(_Types))> emplace(_Args&&...) = delete;
1591 template<typename _Tp, typename... _Args>
1592 enable_if_t<!__exactly_once<_Tp>> emplace(_Args&&...) = delete;
1594 constexpr bool valueless_by_exception() const noexcept
1595 { return !this->_M_valid(); }
1597 constexpr size_t index() const noexcept
1599 using __index_type = typename _Base::__index_type;
1600 if constexpr (__detail::__variant::__never_valueless<_Types...>())
1601 return this->_M_index;
1602 else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
1603 return make_signed_t<__index_type>(this->_M_index);
1605 return size_t(__index_type(this->_M_index + 1)) - 1;
1608 _GLIBCXX20_CONSTEXPR
1610 swap(variant& __rhs)
1611 noexcept((__is_nothrow_swappable<_Types>::value && ...)
1612 && is_nothrow_move_constructible_v<variant>)
1614 static_assert((is_move_constructible_v<_Types> && ...));
1616 // Handle this here to simplify the visitation.
1617 if (__rhs.valueless_by_exception()) [[__unlikely__]]
1619 if (!this->valueless_by_exception()) [[__likely__]]
1624 namespace __variant = __detail::__variant;
1626 __variant::__raw_idx_visit(
1627 [this, &__rhs](auto&& __rhs_mem, auto __rhs_index) mutable
1629 constexpr size_t __j = __rhs_index;
1630 if constexpr (__j != variant_npos)
1632 if (this->index() == __j)
1635 swap(std::get<__j>(*this), __rhs_mem);
1639 auto __tmp(std::move(__rhs_mem));
1641 if constexpr (_Traits::_S_trivial_move_assign)
1642 __rhs = std::move(*this);
1644 __variant::__raw_idx_visit(
1645 [&__rhs](auto&& __this_mem, auto __this_index) mutable
1647 constexpr size_t __k = __this_index;
1648 if constexpr (__k != variant_npos)
1649 __variant::__emplace<__k>(__rhs,
1650 std::move(__this_mem));
1653 __variant::__emplace<__j>(*this, std::move(__tmp));
1659#if defined(__clang__) && __clang_major__ <= 7
1661 using _Base::_M_u; // See https://bugs.llvm.org/show_bug.cgi?id=31852
1665 template<size_t _Np, typename _Vp>
1666 friend constexpr decltype(auto)
1667 __detail::__variant::__get(_Vp&& __v) noexcept;
1669#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
1670 template<typename... _Tp> \
1671 friend constexpr bool \
1672 operator __OP(const variant<_Tp...>& __lhs, \
1673 const variant<_Tp...>& __rhs);
1675 _VARIANT_RELATION_FUNCTION_TEMPLATE(<)
1676 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
1677 _VARIANT_RELATION_FUNCTION_TEMPLATE(==)
1678 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
1679 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
1680 _VARIANT_RELATION_FUNCTION_TEMPLATE(>)
1682#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1685 template<size_t _Np, typename... _Types>
1686 constexpr variant_alternative_t<_Np, variant<_Types...>>&
1687 get(variant<_Types...>& __v)
1689 static_assert(_Np < sizeof...(_Types),
1690 "The index must be in [0, number of alternatives)");
1691 if (__v.index() != _Np)
1692 __throw_bad_variant_access(__v.valueless_by_exception());
1693 return __detail::__variant::__get<_Np>(__v);
1696 template<size_t _Np, typename... _Types>
1697 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1698 get(variant<_Types...>&& __v)
1700 static_assert(_Np < sizeof...(_Types),
1701 "The index must be in [0, number of alternatives)");
1702 if (__v.index() != _Np)
1703 __throw_bad_variant_access(__v.valueless_by_exception());
1704 return __detail::__variant::__get<_Np>(std::move(__v));
1707 template<size_t _Np, typename... _Types>
1708 constexpr const variant_alternative_t<_Np, variant<_Types...>>&
1709 get(const variant<_Types...>& __v)
1711 static_assert(_Np < sizeof...(_Types),
1712 "The index must be in [0, number of alternatives)");
1713 if (__v.index() != _Np)
1714 __throw_bad_variant_access(__v.valueless_by_exception());
1715 return __detail::__variant::__get<_Np>(__v);
1718 template<size_t _Np, typename... _Types>
1719 constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
1720 get(const variant<_Types...>&& __v)
1722 static_assert(_Np < sizeof...(_Types),
1723 "The index must be in [0, number of alternatives)");
1724 if (__v.index() != _Np)
1725 __throw_bad_variant_access(__v.valueless_by_exception());
1726 return __detail::__variant::__get<_Np>(std::move(__v));
1729 /// @cond undocumented
1730 template<typename _Result_type, typename _Visitor, typename... _Variants>
1731 constexpr decltype(auto)
1732 __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
1734 // Get the silly case of visiting no variants out of the way first.
1735 if constexpr (sizeof...(_Variants) == 0)
1737 if constexpr (is_void_v<_Result_type>)
1738 return (void) std::forward<_Visitor>(__visitor)();
1740 return std::forward<_Visitor>(__visitor)();
1744 constexpr size_t __max = 11; // "These go to eleven."
1746 // The type of the first variant in the pack.
1747 using _V0 = typename _Nth_type<0, _Variants...>::type;
1748 // The number of alternatives in that first variant.
1749 constexpr auto __n = variant_size_v<remove_reference_t<_V0>>;
1751 if constexpr (sizeof...(_Variants) > 1 || __n > __max)
1753 // Use a jump table for the general case.
1754 constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1755 _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1757 auto __func_ptr = __vtable._M_access(__variants.index()...);
1758 return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1759 std::forward<_Variants>(__variants)...);
1761 else // We have a single variant with a small number of alternatives.
1763 // A name for the first variant in the pack.
1765 = [](_V0& __v, ...) -> _V0& { return __v; }(__variants...);
1767 using __detail::__variant::_Multi_array;
1768 using __detail::__variant::__gen_vtable_impl;
1769 using _Ma = _Multi_array<_Result_type (*)(_Visitor&&, _V0&&)>;
1771#ifdef _GLIBCXX_DEBUG
1772# define _GLIBCXX_VISIT_UNREACHABLE __builtin_trap
1774# define _GLIBCXX_VISIT_UNREACHABLE __builtin_unreachable
1777#define _GLIBCXX_VISIT_CASE(N) \
1780 if constexpr (N < __n) \
1782 return __gen_vtable_impl<_Ma, index_sequence<N>>:: \
1783 __visit_invoke(std::forward<_Visitor>(__visitor), \
1784 std::forward<_V0>(__v0)); \
1786 else _GLIBCXX_VISIT_UNREACHABLE(); \
1789 switch (__v0.index())
1791 _GLIBCXX_VISIT_CASE(0)
1792 _GLIBCXX_VISIT_CASE(1)
1793 _GLIBCXX_VISIT_CASE(2)
1794 _GLIBCXX_VISIT_CASE(3)
1795 _GLIBCXX_VISIT_CASE(4)
1796 _GLIBCXX_VISIT_CASE(5)
1797 _GLIBCXX_VISIT_CASE(6)
1798 _GLIBCXX_VISIT_CASE(7)
1799 _GLIBCXX_VISIT_CASE(8)
1800 _GLIBCXX_VISIT_CASE(9)
1801 _GLIBCXX_VISIT_CASE(10)
1803 using __detail::__variant::__variant_idx_cookie;
1804 using __detail::__variant::__variant_cookie;
1805 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>
1806 || is_same_v<_Result_type, __variant_cookie>)
1808 using _Npos = index_sequence<variant_npos>;
1809 return __gen_vtable_impl<_Ma, _Npos>::
1810 __visit_invoke(std::forward<_Visitor>(__visitor),
1811 std::forward<_V0>(__v0));
1814 _GLIBCXX_VISIT_UNREACHABLE();
1816 _GLIBCXX_VISIT_UNREACHABLE();
1818#undef _GLIBCXX_VISIT_CASE
1819#undef _GLIBCXX_VISIT_UNREACHABLE
1825 template<typename _Visitor, typename... _Variants>
1826 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1827 visit(_Visitor&& __visitor, _Variants&&... __variants)
1829 namespace __variant = std::__detail::__variant;
1831 if ((__variant::__as(__variants).valueless_by_exception() || ...))
1832 __throw_bad_variant_access("std::visit: variant is valueless");
1835 = __detail::__variant::__visit_result_t<_Visitor, _Variants...>;
1837 using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
1839 if constexpr (sizeof...(_Variants) == 1)
1841 using _Vp = decltype(__variant::__as(std::declval<_Variants>()...));
1843 constexpr bool __visit_rettypes_match = __detail::__variant::
1844 __check_visitor_results<_Visitor, _Vp>(
1845 make_index_sequence<variant_size_v<remove_reference_t<_Vp>>>());
1846 if constexpr (!__visit_rettypes_match)
1848 static_assert(__visit_rettypes_match,
1849 "std::visit requires the visitor to have the same "
1850 "return type for all alternatives of a variant");
1854 return std::__do_visit<_Tag>(
1855 std::forward<_Visitor>(__visitor),
1856 static_cast<_Vp>(__variants)...);
1859 return std::__do_visit<_Tag>(
1860 std::forward<_Visitor>(__visitor),
1861 __variant::__as(std::forward<_Variants>(__variants))...);
1864#if __cplusplus > 201703L
1865 template<typename _Res, typename _Visitor, typename... _Variants>
1867 visit(_Visitor&& __visitor, _Variants&&... __variants)
1869 namespace __variant = std::__detail::__variant;
1871 if ((__variant::__as(__variants).valueless_by_exception() || ...))
1872 __throw_bad_variant_access("std::visit<R>: variant is valueless");
1874 return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
1875 __variant::__as(std::forward<_Variants>(__variants))...);
1879 /// @cond undocumented
1880 template<bool, typename... _Types>
1881 struct __variant_hash_call_base_impl
1884 operator()(const variant<_Types...>& __t) const
1885 noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
1888 __detail::__variant::__raw_visit(
1889 [&__t, &__ret](auto&& __t_mem) mutable
1891 using _Type = __remove_cvref_t<decltype(__t_mem)>;
1892 if constexpr (!is_same_v<_Type,
1893 __detail::__variant::__variant_cookie>)
1894 __ret = std::hash<size_t>{}(__t.index())
1895 + std::hash<_Type>{}(__t_mem);
1897 __ret = std::hash<size_t>{}(__t.index());
1903 template<typename... _Types>
1904 struct __variant_hash_call_base_impl<false, _Types...> {};
1906 template<typename... _Types>
1907 using __variant_hash_call_base =
1908 __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
1909 __enable_hash_call &&...), _Types...>;
1912 template<typename... _Types>
1913 struct hash<variant<_Types...>>
1914 : private __detail::__variant::_Variant_hash_base<
1915 variant<_Types...>, std::index_sequence_for<_Types...>>,
1916 public __variant_hash_call_base<_Types...>
1918 using result_type [[__deprecated__]] = size_t;
1919 using argument_type [[__deprecated__]] = variant<_Types...>;
1923 struct hash<monostate>
1925 using result_type [[__deprecated__]] = size_t;
1926 using argument_type [[__deprecated__]] = monostate;
1929 operator()(const monostate&) const noexcept
1931 constexpr size_t __magic_monostate_hash = -7777;
1932 return __magic_monostate_hash;
1936 template<typename... _Types>
1937 struct __is_fast_hash<hash<variant<_Types...>>>
1938 : bool_constant<(__is_fast_hash<_Types>::value && ...)>
1941_GLIBCXX_END_NAMESPACE_VERSION
1946#endif // _GLIBCXX_VARIANT