1 // <variant> -*- C++ -*-
3 // Copyright (C) 2016-2019 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 <type_traits>
38 #include <bits/enable_special_members.h>
39 #include <bits/functexcept.h>
40 #include <bits/move.h>
41 #include <bits/functional_hash.h>
42 #include <bits/invoke.h>
43 #include <ext/aligned_buffer.h>
44 #include <bits/parse_numbers.h>
45 #include <bits/stl_iterator_base_types.h>
46 #include <bits/stl_iterator_base_funcs.h>
47 #include <bits/stl_construct.h>
49 namespace std _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
57 template<size_t _Np, typename... _Types>
60 template<size_t _Np, typename _First, typename... _Rest>
61 struct _Nth_type<_Np, _First, _Rest...>
62 : _Nth_type<_Np-1, _Rest...> { };
64 template<typename _First, typename... _Rest>
65 struct _Nth_type<0, _First, _Rest...>
66 { using type = _First; };
68 } // namespace __variant
69 } // namespace __detail
71 #define __cpp_lib_variant 201606L
73 template<typename... _Types> class tuple;
74 template<typename... _Types> class variant;
75 template <typename> struct hash;
77 template<typename _Variant>
80 template<typename _Variant>
81 struct variant_size<const _Variant> : variant_size<_Variant> {};
83 template<typename _Variant>
84 struct variant_size<volatile _Variant> : variant_size<_Variant> {};
86 template<typename _Variant>
87 struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
89 template<typename... _Types>
90 struct variant_size<variant<_Types...>>
91 : std::integral_constant<size_t, sizeof...(_Types)> {};
93 template<typename _Variant>
94 inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
96 template<size_t _Np, typename _Variant>
97 struct variant_alternative;
99 template<size_t _Np, typename _First, typename... _Rest>
100 struct variant_alternative<_Np, variant<_First, _Rest...>>
101 : variant_alternative<_Np-1, variant<_Rest...>> {};
103 template<typename _First, typename... _Rest>
104 struct variant_alternative<0, variant<_First, _Rest...>>
105 { using type = _First; };
107 template<size_t _Np, typename _Variant>
108 using variant_alternative_t =
109 typename variant_alternative<_Np, _Variant>::type;
111 template<size_t _Np, typename _Variant>
112 struct variant_alternative<_Np, const _Variant>
113 { using type = add_const_t<variant_alternative_t<_Np, _Variant>>; };
115 template<size_t _Np, typename _Variant>
116 struct variant_alternative<_Np, volatile _Variant>
117 { using type = add_volatile_t<variant_alternative_t<_Np, _Variant>>; };
119 template<size_t _Np, typename _Variant>
120 struct variant_alternative<_Np, const volatile _Variant>
121 { using type = add_cv_t<variant_alternative_t<_Np, _Variant>>; };
123 inline constexpr size_t variant_npos = -1;
125 template<size_t _Np, typename... _Types>
126 constexpr variant_alternative_t<_Np, variant<_Types...>>&
127 get(variant<_Types...>&);
129 template<size_t _Np, typename... _Types>
130 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
131 get(variant<_Types...>&&);
133 template<size_t _Np, typename... _Types>
134 constexpr variant_alternative_t<_Np, variant<_Types...>> const&
135 get(const variant<_Types...>&);
137 template<size_t _Np, typename... _Types>
138 constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
139 get(const variant<_Types...>&&);
141 template<bool __use_index=false,
142 bool __same_return_types = true,
143 typename _Visitor, typename... _Variants>
144 constexpr decltype(auto)
145 __do_visit(_Visitor&& __visitor, _Variants&&... __variants);
147 template <typename... _Types, typename _Tp>
149 __variant_cast(_Tp&& __rhs)
151 if constexpr (is_lvalue_reference_v<_Tp>)
153 if constexpr (is_const_v<remove_reference_t<_Tp>>)
154 return static_cast<const variant<_Types...>&>(__rhs);
156 return static_cast<variant<_Types...>&>(__rhs);
159 return static_cast<variant<_Types...>&&>(__rhs);
166 // Returns the first apparence of _Tp in _Types.
167 // Returns sizeof...(_Types) if _Tp is not in _Types.
168 template<typename _Tp, typename... _Types>
169 struct __index_of : std::integral_constant<size_t, 0> {};
171 template<typename _Tp, typename... _Types>
172 inline constexpr size_t __index_of_v = __index_of<_Tp, _Types...>::value;
174 template<typename _Tp, typename _First, typename... _Rest>
175 struct __index_of<_Tp, _First, _Rest...> :
176 std::integral_constant<size_t, is_same_v<_Tp, _First>
177 ? 0 : __index_of_v<_Tp, _Rest...> + 1> {};
179 // used for raw visitation
180 struct __variant_cookie {};
181 // used for raw visitation with indices passed in
182 struct __variant_idx_cookie { using type = __variant_idx_cookie; };
183 // a more explanatory name than 'true'
184 inline constexpr auto __visit_with_index = bool_constant<true>{};
186 // _Uninitialized<T> is guaranteed to be a literal type, even if T is not.
187 // We have to do this, because [basic.types]p10.5.3 (n4606) is not implemented
188 // yet. When it's implemented, _Uninitialized<T> can be changed to the alias
189 // to T, therefore equivalent to being removed entirely.
191 // Another reason we may not want to remove _Uninitialzied<T> may be that, we
192 // want _Uninitialized<T> to be trivially destructible, no matter whether T
193 // is; but we will see.
194 template<typename _Type, bool = std::is_literal_type_v<_Type>>
195 struct _Uninitialized;
197 template<typename _Type>
198 struct _Uninitialized<_Type, true>
200 template<typename... _Args>
202 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
203 : _M_storage(std::forward<_Args>(__args)...)
206 constexpr const _Type& _M_get() const &
207 { return _M_storage; }
209 constexpr _Type& _M_get() &
210 { return _M_storage; }
212 constexpr const _Type&& _M_get() const &&
213 { return std::move(_M_storage); }
215 constexpr _Type&& _M_get() &&
216 { return std::move(_M_storage); }
221 template<typename _Type>
222 struct _Uninitialized<_Type, false>
224 template<typename... _Args>
226 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
228 ::new ((void*)std::addressof(_M_storage))
229 _Type(std::forward<_Args>(__args)...);
232 const _Type& _M_get() const &
233 { return *_M_storage._M_ptr(); }
236 { return *_M_storage._M_ptr(); }
238 const _Type&& _M_get() const &&
239 { return std::move(*_M_storage._M_ptr()); }
242 { return std::move(*_M_storage._M_ptr()); }
244 __gnu_cxx::__aligned_membuf<_Type> _M_storage;
247 template<typename _Ref>
248 _Ref __ref_cast(void* __ptr)
250 return static_cast<_Ref>(*static_cast<remove_reference_t<_Ref>*>(__ptr));
253 template<typename _Union>
254 constexpr decltype(auto) __get(in_place_index_t<0>, _Union&& __u)
255 { return std::forward<_Union>(__u)._M_first._M_get(); }
257 template<size_t _Np, typename _Union>
258 constexpr decltype(auto) __get(in_place_index_t<_Np>, _Union&& __u)
260 return __variant::__get(in_place_index<_Np-1>,
261 std::forward<_Union>(__u)._M_rest);
264 // Returns the typed storage for __v.
265 template<size_t _Np, typename _Variant>
266 constexpr decltype(auto) __get(_Variant&& __v)
268 return __variant::__get(std::in_place_index<_Np>,
269 std::forward<_Variant>(__v)._M_u);
272 template<typename... _Types>
275 static constexpr bool _S_default_ctor =
276 is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
277 static constexpr bool _S_copy_ctor =
278 (is_copy_constructible_v<_Types> && ...);
279 static constexpr bool _S_move_ctor =
280 (is_move_constructible_v<_Types> && ...);
281 static constexpr bool _S_copy_assign =
282 _S_copy_ctor && _S_move_ctor
283 && (is_copy_assignable_v<_Types> && ...);
284 static constexpr bool _S_move_assign =
286 && (is_move_assignable_v<_Types> && ...);
288 static constexpr bool _S_trivial_dtor =
289 (is_trivially_destructible_v<_Types> && ...);
290 static constexpr bool _S_trivial_copy_ctor =
291 (is_trivially_copy_constructible_v<_Types> && ...);
292 static constexpr bool _S_trivial_move_ctor =
293 (is_trivially_move_constructible_v<_Types> && ...);
294 static constexpr bool _S_trivial_copy_assign =
295 _S_trivial_dtor && (is_trivially_copy_assignable_v<_Types> && ...);
296 static constexpr bool _S_trivial_move_assign =
297 _S_trivial_dtor && (is_trivially_move_assignable_v<_Types> && ...);
299 // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
300 // are always nothrow.
301 static constexpr bool _S_nothrow_default_ctor =
302 is_nothrow_default_constructible_v<
303 typename _Nth_type<0, _Types...>::type>;
304 static constexpr bool _S_nothrow_copy_ctor = false;
305 static constexpr bool _S_nothrow_move_ctor =
306 (is_nothrow_move_constructible_v<_Types> && ...);
307 static constexpr bool _S_nothrow_copy_assign = false;
308 static constexpr bool _S_nothrow_move_assign =
309 _S_nothrow_move_ctor && (is_nothrow_move_assignable_v<_Types> && ...);
312 // Defines members and ctors.
313 template<typename... _Types>
314 union _Variadic_union { };
316 template<typename _First, typename... _Rest>
317 union _Variadic_union<_First, _Rest...>
319 constexpr _Variadic_union() : _M_rest() { }
321 template<typename... _Args>
322 constexpr _Variadic_union(in_place_index_t<0>, _Args&&... __args)
323 : _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
326 template<size_t _Np, typename... _Args>
327 constexpr _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
328 : _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
331 _Uninitialized<_First> _M_first;
332 _Variadic_union<_Rest...> _M_rest;
335 // _Never_valueless_alt is true for variant alternatives that can
336 // always be placed in a variant without it becoming valueless.
338 // For suitably-small, trivially copyable types we can create temporaries
339 // on the stack and then memcpy them into place.
340 template<typename _Tp>
341 struct _Never_valueless_alt
342 : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
345 // Specialize _Never_valueless_alt for other types which have a
346 // non-throwing and cheap move assignment operator, so that emplacing
347 // the type will provide the strong exception-safety guarantee,
348 // by creating and moving a temporary.
349 // Whether _Never_valueless_alt<T> is true or not affects the ABI of a
350 // variant using that alternative, so we can't change the value later!
352 // True if every alternative in _Types... can be emplaced in a variant
353 // without it becoming valueless. If this is true, variant<_Types...>
354 // can never be valueless, which enables some minor optimizations.
355 template <typename... _Types>
356 constexpr bool __never_valueless()
358 return (_Never_valueless_alt<_Types>::value && ...);
361 // Defines index and the dtor, possibly trivial.
362 template<bool __trivially_destructible, typename... _Types>
363 struct _Variant_storage;
365 template <typename... _Types>
366 using __select_index =
367 typename __select_int::_Select_int_base<sizeof...(_Types),
369 unsigned short>::type::value_type;
371 template<typename... _Types>
372 struct _Variant_storage<false, _Types...>
374 constexpr _Variant_storage() : _M_index(variant_npos) { }
376 template<size_t _Np, typename... _Args>
377 constexpr _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
378 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
382 constexpr void _M_reset_impl()
384 __do_visit([](auto&& __this_mem) mutable
385 -> __detail::__variant::__variant_cookie
387 if constexpr (!is_same_v<remove_reference_t<decltype(__this_mem)>,
389 std::_Destroy(std::__addressof(__this_mem));
391 }, __variant_cast<_Types...>(*this));
397 _M_index = variant_npos;
406 return const_cast<void*>(static_cast<const void*>(
407 std::addressof(_M_u)));
411 _M_valid() const noexcept
413 return this->_M_index != __index_type(variant_npos);
416 _Variadic_union<_Types...> _M_u;
417 using __index_type = __select_index<_Types...>;
418 __index_type _M_index;
421 template<typename... _Types>
422 struct _Variant_storage<true, _Types...>
424 constexpr _Variant_storage() : _M_index(variant_npos) { }
426 template<size_t _Np, typename... _Args>
427 constexpr _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
428 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
433 { _M_index = variant_npos; }
438 return const_cast<void*>(static_cast<const void*>(
439 std::addressof(_M_u)));
443 _M_valid() const noexcept
445 if constexpr (__never_valueless<_Types...>())
447 return this->_M_index != __index_type(variant_npos);
450 _Variadic_union<_Types...> _M_u;
451 using __index_type = __select_index<_Types...>;
452 __index_type _M_index;
455 template<typename... _Types>
456 using _Variant_storage_alias =
457 _Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
459 template<typename _Tp, typename _Up>
460 void __variant_construct_single(_Tp&& __lhs, _Up&& __rhs_mem)
462 void* __storage = std::addressof(__lhs._M_u);
463 using _Type = remove_reference_t<decltype(__rhs_mem)>;
464 if constexpr (!is_same_v<_Type, __variant_cookie>)
466 _Type(std::forward<decltype(__rhs_mem)>(__rhs_mem));
469 template<typename... _Types, typename _Tp, typename _Up>
470 void __variant_construct(_Tp&& __lhs, _Up&& __rhs)
472 __lhs._M_index = __rhs._M_index;
473 __do_visit([&__lhs](auto&& __rhs_mem) mutable
474 -> __detail::__variant::__variant_cookie
476 __variant_construct_single(std::forward<_Tp>(__lhs),
477 std::forward<decltype(__rhs_mem)>( __rhs_mem));
479 }, __variant_cast<_Types...>(std::forward<decltype(__rhs)>(__rhs)));
482 // The following are (Copy|Move) (ctor|assign) layers for forwarding
483 // triviality and handling non-trivial SMF behaviors.
485 template<bool, typename... _Types>
486 struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
488 using _Base = _Variant_storage_alias<_Types...>;
491 _Copy_ctor_base(const _Copy_ctor_base& __rhs)
492 noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
494 __variant_construct<_Types...>(*this, __rhs);
497 _Copy_ctor_base(_Copy_ctor_base&&) = default;
498 _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
499 _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
502 template<typename... _Types>
503 struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
505 using _Base = _Variant_storage_alias<_Types...>;
509 template<typename... _Types>
510 using _Copy_ctor_alias =
511 _Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
513 template<bool, typename... _Types>
514 struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
516 using _Base = _Copy_ctor_alias<_Types...>;
519 _Move_ctor_base(_Move_ctor_base&& __rhs)
520 noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
522 __variant_construct<_Types...>(*this,
523 std::forward<_Move_ctor_base>(__rhs));
526 template<typename _Up>
527 void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
530 this->_M_index = __rhs_index;
533 __variant_construct_single(*this,
534 std::forward<_Up>(__rhs));
538 this->_M_index = variant_npos;
539 __throw_exception_again;
543 template<typename _Up>
544 void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
547 this->_M_index = __rhs_index;
550 __variant_construct_single(*this, __rhs);
554 this->_M_index = variant_npos;
555 __throw_exception_again;
559 _Move_ctor_base(const _Move_ctor_base&) = default;
560 _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
561 _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
564 template<typename... _Types>
565 struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
567 using _Base = _Copy_ctor_alias<_Types...>;
570 template<typename _Up>
571 void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
574 this->_M_index = __rhs_index;
575 __variant_construct_single(*this,
576 std::forward<_Up>(__rhs));
579 template<typename _Up>
580 void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
583 this->_M_index = __rhs_index;
584 __variant_construct_single(*this, __rhs);
588 template<typename... _Types>
589 using _Move_ctor_alias =
590 _Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
592 template<bool, typename... _Types>
593 struct _Copy_assign_base : _Move_ctor_alias<_Types...>
595 using _Base = _Move_ctor_alias<_Types...>;
599 operator=(const _Copy_assign_base& __rhs)
600 noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
602 __do_visit<__visit_with_index>([this](auto&& __rhs_mem,
603 auto __rhs_index) mutable
604 -> __detail::__variant::__variant_idx_cookie
606 if constexpr (__rhs_index != variant_npos)
608 if (this->_M_index == __rhs_index)
610 if constexpr (__rhs_index != variant_npos)
613 __variant::__get<__rhs_index>(*this);
614 if constexpr (is_same_v<
615 remove_reference_t<decltype(__this_mem)>,
616 remove_reference_t<decltype(__rhs_mem)>>)
617 __this_mem = __rhs_mem;
623 remove_reference_t<decltype(__rhs_mem)>;
625 (is_nothrow_copy_constructible_v<__rhs_type>
626 || !is_nothrow_move_constructible_v<__rhs_type>)
627 this->_M_destructive_copy(__rhs_index, __rhs_mem);
630 auto __tmp(__rhs_mem);
631 this->_M_destructive_move(__rhs_index,
639 }, __variant_cast<_Types...>(__rhs));
643 _Copy_assign_base(const _Copy_assign_base&) = default;
644 _Copy_assign_base(_Copy_assign_base&&) = default;
645 _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
648 template<typename... _Types>
649 struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
651 using _Base = _Move_ctor_alias<_Types...>;
655 template<typename... _Types>
656 using _Copy_assign_alias =
657 _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign
658 && _Traits<_Types...>::_S_trivial_copy_ctor,
661 template<bool, typename... _Types>
662 struct _Move_assign_base : _Copy_assign_alias<_Types...>
664 using _Base = _Copy_assign_alias<_Types...>;
668 operator=(_Move_assign_base&& __rhs)
669 noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
671 __do_visit<__visit_with_index>([this](auto&& __rhs_mem,
672 auto __rhs_index) mutable
673 -> __detail::__variant::__variant_idx_cookie
675 if constexpr (__rhs_index != variant_npos)
677 if (this->_M_index == __rhs_index)
679 if constexpr (__rhs_index != variant_npos)
682 __variant::__get<__rhs_index>(*this);
683 if constexpr (is_same_v<
684 remove_reference_t<decltype(__this_mem)>,
685 remove_reference_t<decltype(__rhs_mem)>>)
686 __this_mem = std::move(__rhs_mem);
690 this->_M_destructive_move(__rhs_index,
691 std::move(__rhs_mem));
696 }, __variant_cast<_Types...>(__rhs));
700 _Move_assign_base(const _Move_assign_base&) = default;
701 _Move_assign_base(_Move_assign_base&&) = default;
702 _Move_assign_base& operator=(const _Move_assign_base&) = default;
705 template<typename... _Types>
706 struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
708 using _Base = _Copy_assign_alias<_Types...>;
712 template<typename... _Types>
713 using _Move_assign_alias =
714 _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
716 template<typename... _Types>
717 struct _Variant_base : _Move_assign_alias<_Types...>
719 using _Base = _Move_assign_alias<_Types...>;
723 noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
724 : _Variant_base(in_place_index<0>) { }
726 template<size_t _Np, typename... _Args>
728 _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
729 : _Base(__i, std::forward<_Args>(__args)...)
732 _Variant_base(const _Variant_base&) = default;
733 _Variant_base(_Variant_base&&) = default;
734 _Variant_base& operator=(const _Variant_base&) = default;
735 _Variant_base& operator=(_Variant_base&&) = default;
738 // For how many times does _Tp appear in _Tuple?
739 template<typename _Tp, typename _Tuple>
740 struct __tuple_count;
742 template<typename _Tp, typename _Tuple>
743 inline constexpr size_t __tuple_count_v =
744 __tuple_count<_Tp, _Tuple>::value;
746 template<typename _Tp, typename... _Types>
747 struct __tuple_count<_Tp, tuple<_Types...>>
748 : integral_constant<size_t, 0> { };
750 template<typename _Tp, typename _First, typename... _Rest>
751 struct __tuple_count<_Tp, tuple<_First, _Rest...>>
754 __tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { };
756 // TODO: Reuse this in <tuple> ?
757 template<typename _Tp, typename... _Types>
758 inline constexpr bool __exactly_once =
759 __tuple_count_v<_Tp, tuple<_Types...>> == 1;
761 // Takes _Types and create an overloaded _S_fun for each type.
762 // If a type appears more than once in _Types, create only one overload.
763 template<typename... _Types>
764 struct __overload_set
765 { static void _S_fun(); };
767 template<typename _First, typename... _Rest>
768 struct __overload_set<_First, _Rest...> : __overload_set<_Rest...>
770 using __overload_set<_Rest...>::_S_fun;
771 static integral_constant<size_t, sizeof...(_Rest)> _S_fun(_First);
774 template<typename... _Rest>
775 struct __overload_set<void, _Rest...> : __overload_set<_Rest...>
777 using __overload_set<_Rest...>::_S_fun;
780 // Helper for variant(_Tp&&) and variant::operator=(_Tp&&).
781 // __accepted_index maps an arbitrary _Tp to an alternative type in _Variant
782 // (or to variant_npos).
783 template<typename _Tp, typename _Variant, typename = void>
784 struct __accepted_index
785 { static constexpr size_t value = variant_npos; };
787 template<typename _Tp, typename... _Types>
788 struct __accepted_index<
789 _Tp, variant<_Types...>,
790 void_t<decltype(__overload_set<_Types...>::_S_fun(std::declval<_Tp>()))>>
792 static constexpr size_t value = sizeof...(_Types) - 1
793 - decltype(__overload_set<_Types...>::
794 _S_fun(std::declval<_Tp>()))::value;
797 // Returns the raw storage for __v.
798 template<typename _Variant>
799 void* __get_storage(_Variant&& __v)
800 { return __v._M_storage(); }
802 template <typename _Maybe_variant_cookie, typename _Variant>
803 struct _Extra_visit_slot_needed
805 template <typename> struct _Variant_never_valueless;
807 template <typename... _Types>
808 struct _Variant_never_valueless<variant<_Types...>>
809 : bool_constant<__never_valueless<_Types...>()> {};
811 static constexpr bool value =
812 (is_same_v<_Maybe_variant_cookie, __variant_cookie>
813 || is_same_v<_Maybe_variant_cookie, __variant_idx_cookie>)
814 && !_Variant_never_valueless<__remove_cvref_t<_Variant>>::value;
817 // Used for storing a multi-dimensional vtable.
818 template<typename _Tp, size_t... _Dimensions>
821 // Partial specialization with rank zero, stores a single _Tp element.
822 template<typename _Tp>
823 struct _Multi_array<_Tp>
832 // Partial specialization with rank >= 1.
833 template<typename _Ret,
835 typename... _Variants,
836 size_t __first, size_t... __rest>
837 struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
839 static constexpr size_t __index =
840 sizeof...(_Variants) - sizeof...(__rest) - 1;
842 using _Variant = typename _Nth_type<__index, _Variants...>::type;
844 static constexpr int __do_cookie =
845 _Extra_visit_slot_needed<_Ret, _Variant>::value ? 1 : 0;
847 using _Tp = _Ret(*)(_Visitor, _Variants...);
849 template<typename... _Args>
851 _M_access(size_t __first_index, _Args... __rest_indices) const
853 return _M_arr[__first_index + __do_cookie]
854 ._M_access(__rest_indices...);
857 _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
860 // Creates a multi-dimensional vtable recursively.
862 // The __same_return_types non-type template parameter specifies whether
863 // to enforce that all visitor invocations return the same type. This is
864 // required by std::visit but not std::visit<R>.
867 // visit([](auto, auto){},
868 // variant<int, char>(), // typedef'ed as V1
869 // variant<float, double, long double>()) // typedef'ed as V2
870 // will trigger instantiations of:
871 // __gen_vtable_impl<true, _Multi_array<void(*)(V1&&, V2&&), 2, 3>,
872 // tuple<V1&&, V2&&>, std::index_sequence<>>
873 // __gen_vtable_impl<true, _Multi_array<void(*)(V1&&, V2&&), 3>,
874 // tuple<V1&&, V2&&>, std::index_sequence<0>>
875 // __gen_vtable_impl<true, _Multi_array<void(*)(V1&&, V2&&)>,
876 // tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
877 // __gen_vtable_impl<true, _Multi_array<void(*)(V1&&, V2&&)>,
878 // tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
879 // __gen_vtable_impl<true, _Multi_array<void(*)(V1&&, V2&&)>,
880 // tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
881 // __gen_vtable_impl<true, _Multi_array<void(*)(V1&&, V2&&), 3>,
882 // tuple<V1&&, V2&&>, std::index_sequence<1>>
883 // __gen_vtable_impl<true, _Multi_array<void(*)(V1&&, V2&&)>,
884 // tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
885 // __gen_vtable_impl<true, _Multi_array<void(*)(V1&&, V2&&)>,
886 // tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
887 // __gen_vtable_impl<true, _Multi_array<void(*)(V1&&, V2&&)>,
888 // tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
889 // The returned multi-dimensional vtable can be fast accessed by the visitor
890 // using index calculation.
891 template<bool __same_return_types,
892 typename _Array_type, typename _Variant_tuple, typename _Index_seq>
893 struct __gen_vtable_impl;
895 // Defines the _S_apply() member that returns a _Multi_array populated
896 // with function pointers that perform the visitation expressions e(m)
897 // for each valid pack of indexes into the variant types _Variants.
899 // This partial specialization builds up the index sequences by recursively
900 // calling _S_apply() on the next specialization of __gen_vtable_impl.
901 // The base case of the recursion defines the actual function pointers.
902 template<bool __same_return_types,
903 typename _Result_type, typename _Visitor, size_t... __dimensions,
904 typename... _Variants, size_t... __indices>
905 struct __gen_vtable_impl<
907 _Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
908 tuple<_Variants...>, std::index_sequence<__indices...>>
911 remove_reference_t<typename _Nth_type<sizeof...(__indices),
912 _Variants...>::type>;
914 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
917 static constexpr _Array_type
920 _Array_type __vtable{};
922 __vtable, make_index_sequence<variant_size_v<_Next>>());
926 template<size_t... __var_indices>
927 static constexpr void
928 _S_apply_all_alts(_Array_type& __vtable,
929 std::index_sequence<__var_indices...>)
931 if constexpr (_Extra_visit_slot_needed<_Result_type, _Next>::value)
932 (_S_apply_single_alt<true, __var_indices>(
933 __vtable._M_arr[__var_indices + 1],
934 &(__vtable._M_arr[0])), ...);
936 (_S_apply_single_alt<false, __var_indices>(
937 __vtable._M_arr[__var_indices]), ...);
940 template<bool __do_cookie, size_t __index, typename _Tp>
941 static constexpr void
942 _S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
944 using _Alternative = variant_alternative_t<__index, _Next>;
945 if constexpr (__do_cookie)
947 __element = __gen_vtable_impl<
951 std::index_sequence<__indices..., __index>>::_S_apply();
952 *__cookie_element = __gen_vtable_impl<
956 std::index_sequence<__indices..., variant_npos>>::_S_apply();
960 __element = __gen_vtable_impl<
962 remove_reference_t<decltype(__element)>, tuple<_Variants...>,
963 std::index_sequence<__indices..., __index>>::_S_apply();
968 // This partial specialization is the base case for the recursion.
969 // It populates a _Multi_array element with the address of a function
970 // that invokes the visitor with the alternatives specified by __indices.
971 template<bool __same_return_types,
972 typename _Result_type, typename _Visitor, typename... _Variants,
974 struct __gen_vtable_impl<
976 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
977 tuple<_Variants...>, std::index_sequence<__indices...>>
980 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
982 template<size_t __index, typename _Variant>
983 static constexpr decltype(auto)
984 __element_by_index_or_cookie(_Variant&& __var)
986 if constexpr (__index != variant_npos)
987 return __variant::__get<__index>(std::forward<_Variant>(__var));
989 return __variant_cookie{};
992 static constexpr decltype(auto)
993 __visit_invoke_impl(_Visitor&& __visitor, _Variants... __vars)
995 // For raw visitation using indices, pass the indices to the visitor:
996 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
997 return std::__invoke(std::forward<_Visitor>(__visitor),
998 __element_by_index_or_cookie<__indices>(
999 std::forward<_Variants>(__vars))...,
1000 integral_constant<size_t, __indices>()...);
1001 // For std::visit<cv void>, cast the result to void:
1002 else if constexpr (!__same_return_types &&
1003 std::is_void_v<_Result_type>)
1004 return (void)std::__invoke(std::forward<_Visitor>(__visitor),
1005 __element_by_index_or_cookie<__indices>(
1006 std::forward<_Variants>(__vars))...);
1008 return std::__invoke(std::forward<_Visitor>(__visitor),
1009 __element_by_index_or_cookie<__indices>(
1010 std::forward<_Variants>(__vars))...);
1013 static constexpr decltype(auto)
1014 __do_visit_invoke(_Visitor&& __visitor, _Variants... __vars)
1016 return __visit_invoke_impl(std::forward<_Visitor>(__visitor),
1017 std::forward<_Variants>(__vars)...);
1020 // Perform the implicit conversion to _Result_type for std::visit<R>.
1021 static constexpr _Result_type
1022 __do_visit_invoke_r(_Visitor&& __visitor, _Variants... __vars)
1024 return __visit_invoke_impl(std::forward<_Visitor>(__visitor),
1025 std::forward<_Variants>(__vars)...);
1028 static constexpr decltype(auto)
1029 __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
1031 if constexpr (__same_return_types)
1032 return __do_visit_invoke(std::forward<_Visitor>(__visitor),
1033 std::forward<_Variants>(__vars)...);
1035 return __do_visit_invoke_r(std::forward<_Visitor>(__visitor),
1036 std::forward<_Variants>(__vars)...);
1039 static constexpr auto
1041 { return _Array_type{&__visit_invoke}; }
1044 template<bool __same_return_types,
1045 typename _Result_type, typename _Visitor, typename... _Variants>
1049 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
1050 variant_size_v<remove_reference_t<_Variants>>...>;
1052 static constexpr _Array_type _S_vtable
1053 = __gen_vtable_impl<__same_return_types,
1054 _Array_type, tuple<_Variants...>,
1055 std::index_sequence<>>::_S_apply();
1058 template<size_t _Np, typename _Tp>
1059 struct _Base_dedup : public _Tp { };
1061 template<typename _Variant, typename __indices>
1062 struct _Variant_hash_base;
1064 template<typename... _Types, size_t... __indices>
1065 struct _Variant_hash_base<variant<_Types...>,
1066 std::index_sequence<__indices...>>
1067 : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
1069 } // namespace __variant
1070 } // namespace __detail
1072 template<size_t _Np, typename _Variant, typename... _Args>
1073 void __variant_construct_by_index(_Variant& __v, _Args&&... __args)
1076 auto&& __storage = __detail::__variant::__get<_Np>(__v);
1077 ::new ((void*)std::addressof(__storage))
1078 remove_reference_t<decltype(__storage)>
1079 (std::forward<_Args>(__args)...);
1082 template<typename _Tp, typename... _Types>
1084 holds_alternative(const variant<_Types...>& __v) noexcept
1086 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1087 "T should occur for exactly once in alternatives");
1088 return __v.index() == __detail::__variant::__index_of_v<_Tp, _Types...>;
1091 template<typename _Tp, typename... _Types>
1092 constexpr _Tp& get(variant<_Types...>& __v)
1094 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1095 "T should occur for exactly once in alternatives");
1096 static_assert(!is_void_v<_Tp>, "_Tp should not be void");
1097 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
1100 template<typename _Tp, typename... _Types>
1101 constexpr _Tp&& get(variant<_Types...>&& __v)
1103 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1104 "T should occur for exactly once in alternatives");
1105 static_assert(!is_void_v<_Tp>, "_Tp should not be void");
1106 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1110 template<typename _Tp, typename... _Types>
1111 constexpr const _Tp& get(const variant<_Types...>& __v)
1113 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1114 "T should occur for exactly once in alternatives");
1115 static_assert(!is_void_v<_Tp>, "_Tp should not be void");
1116 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
1119 template<typename _Tp, typename... _Types>
1120 constexpr const _Tp&& get(const variant<_Types...>&& __v)
1122 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1123 "T should occur for exactly once in alternatives");
1124 static_assert(!is_void_v<_Tp>, "_Tp should not be void");
1125 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1129 template<size_t _Np, typename... _Types>
1130 constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
1131 get_if(variant<_Types...>* __ptr) noexcept
1133 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1134 static_assert(_Np < sizeof...(_Types),
1135 "The index should be in [0, number of alternatives)");
1136 static_assert(!is_void_v<_Alternative_type>, "_Tp should not be void");
1137 if (__ptr && __ptr->index() == _Np)
1138 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1142 template<size_t _Np, typename... _Types>
1144 add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
1145 get_if(const variant<_Types...>* __ptr) noexcept
1147 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1148 static_assert(_Np < sizeof...(_Types),
1149 "The index should be in [0, number of alternatives)");
1150 static_assert(!is_void_v<_Alternative_type>, "_Tp should not be void");
1151 if (__ptr && __ptr->index() == _Np)
1152 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1156 template<typename _Tp, typename... _Types>
1157 constexpr add_pointer_t<_Tp>
1158 get_if(variant<_Types...>* __ptr) noexcept
1160 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1161 "T should occur for exactly once in alternatives");
1162 static_assert(!is_void_v<_Tp>, "_Tp should not be void");
1163 return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1167 template<typename _Tp, typename... _Types>
1168 constexpr add_pointer_t<const _Tp>
1169 get_if(const variant<_Types...>* __ptr)
1172 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1173 "T should occur for exactly once in alternatives");
1174 static_assert(!is_void_v<_Tp>, "_Tp should not be void");
1175 return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1179 struct monostate { };
1181 #define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
1182 template<typename... _Types> \
1183 constexpr bool operator __OP(const variant<_Types...>& __lhs, \
1184 const variant<_Types...>& __rhs) \
1186 bool __ret = true; \
1187 __do_visit<__detail::__variant::__visit_with_index>( \
1189 (auto&& __rhs_mem, auto __rhs_index) mutable \
1190 -> __detail::__variant::__variant_idx_cookie \
1192 if constexpr (__rhs_index != variant_npos) \
1194 if (__lhs.index() == __rhs_index) \
1196 auto& __this_mem = std::get<__rhs_index>(__lhs); \
1197 __ret = __this_mem __OP __rhs_mem; \
1200 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1203 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1209 constexpr bool operator __OP(monostate, monostate) noexcept \
1210 { return 0 __OP 0; }
1212 _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
1213 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
1214 _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
1215 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
1216 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
1217 _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
1219 #undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1221 template<typename _Visitor, typename... _Variants>
1222 constexpr decltype(auto) visit(_Visitor&&, _Variants&&...);
1224 template<typename... _Types>
1225 inline enable_if_t<(is_move_constructible_v<_Types> && ...)
1226 && (is_swappable_v<_Types> && ...)>
1227 swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1228 noexcept(noexcept(__lhs.swap(__rhs)))
1229 { __lhs.swap(__rhs); }
1231 template<typename... _Types>
1232 enable_if_t<!((is_move_constructible_v<_Types> && ...)
1233 && (is_swappable_v<_Types> && ...))>
1234 swap(variant<_Types...>&, variant<_Types...>&) = delete;
1236 class bad_variant_access : public exception
1239 bad_variant_access() noexcept : _M_reason("Unknown reason") { }
1240 const char* what() const noexcept override
1241 { return _M_reason; }
1244 bad_variant_access(const char* __reason) : _M_reason(__reason) { }
1246 const char* _M_reason;
1248 friend void __throw_bad_variant_access(const char* __what);
1252 __throw_bad_variant_access(const char* __what)
1253 { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
1255 template<typename... _Types>
1257 : private __detail::__variant::_Variant_base<_Types...>,
1258 private _Enable_default_constructor<
1259 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1260 variant<_Types...>>,
1261 private _Enable_copy_move<
1262 __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1263 __detail::__variant::_Traits<_Types...>::_S_copy_assign,
1264 __detail::__variant::_Traits<_Types...>::_S_move_ctor,
1265 __detail::__variant::_Traits<_Types...>::_S_move_assign,
1269 template <typename... _UTypes, typename _Tp>
1270 friend decltype(auto) __variant_cast(_Tp&&);
1271 template<size_t _Np, typename _Variant, typename... _Args>
1272 friend void __variant_construct_by_index(_Variant& __v,
1275 static_assert(sizeof...(_Types) > 0,
1276 "variant must have at least one alternative");
1277 static_assert(!(std::is_reference_v<_Types> || ...),
1278 "variant must have no reference alternative");
1279 static_assert(!(std::is_void_v<_Types> || ...),
1280 "variant must have no void alternative");
1282 using _Base = __detail::__variant::_Variant_base<_Types...>;
1283 using _Default_ctor_enabler =
1284 _Enable_default_constructor<
1285 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1286 variant<_Types...>>;
1288 template<typename _Tp>
1289 static constexpr bool
1290 __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1292 template<typename _Tp>
1293 static constexpr size_t __accepted_index =
1294 __detail::__variant::__accepted_index<_Tp&&, variant>::value;
1296 template<size_t _Np, bool = _Np < sizeof...(_Types)>
1297 struct __to_type_impl;
1299 template<size_t _Np>
1300 struct __to_type_impl<_Np, true>
1301 { using type = variant_alternative_t<_Np, variant>; };
1303 template<size_t _Np>
1304 using __to_type = typename __to_type_impl<_Np>::type;
1306 template<typename _Tp>
1307 using __accepted_type = __to_type<__accepted_index<_Tp>>;
1309 template<typename _Tp>
1310 static constexpr size_t __index_of =
1311 __detail::__variant::__index_of_v<_Tp, _Types...>;
1313 using _Traits = __detail::__variant::_Traits<_Types...>;
1316 variant() = default;
1317 variant(const variant& __rhs) = default;
1318 variant(variant&&) = default;
1319 variant& operator=(const variant&) = default;
1320 variant& operator=(variant&&) = default;
1321 ~variant() = default;
1323 template<typename _Tp,
1324 typename = enable_if_t<!is_same_v<decay_t<_Tp>, variant>>,
1325 typename = enable_if_t<(sizeof...(_Types)>0)>,
1326 typename = enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1327 && is_constructible_v<__accepted_type<_Tp&&>, _Tp&&>>>
1330 noexcept(is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp&&>)
1331 : variant(in_place_index<__accepted_index<_Tp&&>>,
1332 std::forward<_Tp>(__t))
1335 template<typename _Tp, typename... _Args,
1336 typename = enable_if_t<__exactly_once<_Tp>
1337 && is_constructible_v<_Tp, _Args&&...>>>
1339 variant(in_place_type_t<_Tp>, _Args&&... __args)
1340 : variant(in_place_index<__index_of<_Tp>>,
1341 std::forward<_Args>(__args)...)
1344 template<typename _Tp, typename _Up, typename... _Args,
1345 typename = enable_if_t<__exactly_once<_Tp>
1346 && is_constructible_v<
1347 _Tp, initializer_list<_Up>&, _Args&&...>>>
1349 variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1351 : variant(in_place_index<__index_of<_Tp>>, __il,
1352 std::forward<_Args>(__args)...)
1355 template<size_t _Np, typename... _Args,
1356 typename = enable_if_t<
1357 is_constructible_v<__to_type<_Np>, _Args&&...>>>
1359 variant(in_place_index_t<_Np>, _Args&&... __args)
1360 : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
1361 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1364 template<size_t _Np, typename _Up, typename... _Args,
1365 typename = enable_if_t<is_constructible_v<__to_type<_Np>,
1366 initializer_list<_Up>&, _Args&&...>>>
1368 variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1370 : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
1371 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1374 template<typename _Tp>
1375 enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1376 && is_constructible_v<__accepted_type<_Tp&&>, _Tp&&>
1377 && is_assignable_v<__accepted_type<_Tp&&>&, _Tp&&>
1378 && !is_same_v<decay_t<_Tp>, variant>, variant&>
1379 operator=(_Tp&& __rhs)
1380 noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp&&>
1381 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp&&>)
1383 constexpr auto __index = __accepted_index<_Tp&&>;
1384 if (index() == __index)
1385 std::get<__index>(*this) = std::forward<_Tp>(__rhs);
1387 this->emplace<__index>(std::forward<_Tp>(__rhs));
1391 template<typename _Tp, typename... _Args>
1392 enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1394 emplace(_Args&&... __args)
1396 constexpr size_t __index = __index_of<_Tp>;
1397 return this->emplace<__index>(std::forward<_Args>(__args)...);
1400 template<typename _Tp, typename _Up, typename... _Args>
1401 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1402 && __exactly_once<_Tp>,
1404 emplace(initializer_list<_Up> __il, _Args&&... __args)
1406 constexpr size_t __index = __index_of<_Tp>;
1407 return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
1410 template<size_t _Np, typename... _Args>
1411 enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
1413 variant_alternative_t<_Np, variant>&>
1414 emplace(_Args&&... __args)
1416 static_assert(_Np < sizeof...(_Types),
1417 "The index should be in [0, number of alternatives)");
1418 using type = variant_alternative_t<_Np, variant>;
1419 // Provide the strong exception-safety guarantee when possible,
1420 // to avoid becoming valueless.
1421 if constexpr (is_nothrow_constructible_v<type, _Args...>)
1424 __variant_construct_by_index<_Np>(*this,
1425 std::forward<_Args>(__args)...);
1427 else if constexpr (is_scalar_v<type>)
1429 // This might invoke a potentially-throwing conversion operator:
1430 const type __tmp(std::forward<_Args>(__args)...);
1431 // But these steps won't throw:
1433 __variant_construct_by_index<_Np>(*this, __tmp);
1435 else if constexpr (__detail::__variant::_Never_valueless_alt<type>())
1437 // This construction might throw:
1438 variant __tmp(in_place_index<_Np>,
1439 std::forward<_Args>(__args)...);
1440 // But _Never_valueless_alt<type> means this won't:
1441 *this = std::move(__tmp);
1445 // This case only provides the basic exception-safety guarantee,
1446 // i.e. the variant can become valueless.
1450 __variant_construct_by_index<_Np>(*this,
1451 std::forward<_Args>(__args)...);
1455 this->_M_index = variant_npos;
1456 __throw_exception_again;
1459 return std::get<_Np>(*this);
1462 template<size_t _Np, typename _Up, typename... _Args>
1463 enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
1464 initializer_list<_Up>&, _Args...>,
1465 variant_alternative_t<_Np, variant>&>
1466 emplace(initializer_list<_Up> __il, _Args&&... __args)
1468 static_assert(_Np < sizeof...(_Types),
1469 "The index should be in [0, number of alternatives)");
1470 using type = variant_alternative_t<_Np, variant>;
1471 // Provide the strong exception-safety guarantee when possible,
1472 // to avoid becoming valueless.
1473 if constexpr (is_nothrow_constructible_v<type,
1474 initializer_list<_Up>&,
1478 __variant_construct_by_index<_Np>(*this, __il,
1479 std::forward<_Args>(__args)...);
1481 else if constexpr (__detail::__variant::_Never_valueless_alt<type>())
1483 // This construction might throw:
1484 variant __tmp(in_place_index<_Np>, __il,
1485 std::forward<_Args>(__args)...);
1486 // But _Never_valueless_alt<type> means this won't:
1487 *this = std::move(__tmp);
1491 // This case only provides the basic exception-safety guarantee,
1492 // i.e. the variant can become valueless.
1496 __variant_construct_by_index<_Np>(*this, __il,
1497 std::forward<_Args>(__args)...);
1501 this->_M_index = variant_npos;
1502 __throw_exception_again;
1505 return std::get<_Np>(*this);
1508 constexpr bool valueless_by_exception() const noexcept
1509 { return !this->_M_valid(); }
1511 constexpr size_t index() const noexcept
1513 if (this->_M_index ==
1514 typename _Base::__index_type(variant_npos))
1515 return variant_npos;
1516 return this->_M_index;
1520 swap(variant& __rhs)
1521 noexcept((__is_nothrow_swappable<_Types>::value && ...)
1522 && is_nothrow_move_constructible_v<variant>)
1524 __do_visit<__detail::__variant::__visit_with_index>(
1525 [this, &__rhs](auto&& __rhs_mem,
1526 auto __rhs_index) mutable
1527 -> __detail::__variant::__variant_idx_cookie
1529 if constexpr (__rhs_index != variant_npos)
1531 if (this->index() == __rhs_index)
1534 std::get<__rhs_index>(*this);
1536 swap(__this_mem, __rhs_mem);
1540 if (this->index() != variant_npos)
1542 auto __tmp(std::move(__rhs_mem));
1543 __rhs = std::move(*this);
1544 this->_M_destructive_move(__rhs_index,
1549 this->_M_destructive_move(__rhs_index,
1550 std::move(__rhs_mem));
1557 if (this->index() != variant_npos)
1559 __rhs = std::move(*this);
1569 #if defined(__clang__) && __clang_major__ <= 7
1571 using _Base::_M_u; // See https://bugs.llvm.org/show_bug.cgi?id=31852
1575 template<size_t _Np, typename _Vp>
1576 friend constexpr decltype(auto) __detail::__variant::__get(_Vp&& __v);
1578 template<typename _Vp>
1579 friend void* __detail::__variant::__get_storage(_Vp&& __v);
1581 #define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
1582 template<typename... _Tp> \
1583 friend constexpr bool \
1584 operator __OP(const variant<_Tp...>& __lhs, \
1585 const variant<_Tp...>& __rhs);
1587 _VARIANT_RELATION_FUNCTION_TEMPLATE(<)
1588 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
1589 _VARIANT_RELATION_FUNCTION_TEMPLATE(==)
1590 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
1591 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
1592 _VARIANT_RELATION_FUNCTION_TEMPLATE(>)
1594 #undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1597 template<size_t _Np, typename... _Types>
1598 constexpr variant_alternative_t<_Np, variant<_Types...>>&
1599 get(variant<_Types...>& __v)
1601 static_assert(_Np < sizeof...(_Types),
1602 "The index should be in [0, number of alternatives)");
1603 if (__v.index() != _Np)
1604 __throw_bad_variant_access("Unexpected index");
1605 return __detail::__variant::__get<_Np>(__v);
1608 template<size_t _Np, typename... _Types>
1609 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1610 get(variant<_Types...>&& __v)
1612 static_assert(_Np < sizeof...(_Types),
1613 "The index should be in [0, number of alternatives)");
1614 if (__v.index() != _Np)
1615 __throw_bad_variant_access("Unexpected index");
1616 return __detail::__variant::__get<_Np>(std::move(__v));
1619 template<size_t _Np, typename... _Types>
1620 constexpr const variant_alternative_t<_Np, variant<_Types...>>&
1621 get(const variant<_Types...>& __v)
1623 static_assert(_Np < sizeof...(_Types),
1624 "The index should be in [0, number of alternatives)");
1625 if (__v.index() != _Np)
1626 __throw_bad_variant_access("Unexpected index");
1627 return __detail::__variant::__get<_Np>(__v);
1630 template<size_t _Np, typename... _Types>
1631 constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
1632 get(const variant<_Types...>&& __v)
1634 static_assert(_Np < sizeof...(_Types),
1635 "The index should be in [0, number of alternatives)");
1636 if (__v.index() != _Np)
1637 __throw_bad_variant_access("Unexpected index");
1638 return __detail::__variant::__get<_Np>(std::move(__v));
1641 template<bool __use_index,
1642 bool __same_return_types,
1643 typename _Visitor, typename... _Variants>
1644 constexpr decltype(auto)
1645 __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
1647 using _Deduced_type = std::invoke_result<_Visitor,
1648 decltype(std::get<0>(std::declval<_Variants>()))...>;
1650 using _Result_type = typename std::conditional_t<__use_index,
1651 __detail::__variant::__variant_idx_cookie,
1652 _Deduced_type>::type;
1654 constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1655 __same_return_types,
1656 _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1658 auto __func_ptr = __vtable._M_access(__variants.index()...);
1659 return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1660 std::forward<_Variants>(__variants)...);
1663 template<typename _Visitor, typename... _Variants>
1664 constexpr decltype(auto)
1665 visit(_Visitor&& __visitor, _Variants&&... __variants)
1667 if ((__variants.valueless_by_exception() || ...))
1668 __throw_bad_variant_access("Unexpected index");
1670 return __do_visit(std::forward<_Visitor>(__visitor),
1671 std::forward<_Variants>(__variants)...);
1674 #if __cplusplus > 201703L
1675 template<typename _Res, typename _Visitor, typename... _Variants>
1677 visit(_Visitor&& __visitor, _Variants&&... __variants)
1679 if ((__variants.valueless_by_exception() || ...))
1680 __throw_bad_variant_access("Unexpected index");
1682 if constexpr (std::is_void_v<_Res>)
1683 (void) __do_visit<false, false>(std::forward<_Visitor>(__visitor),
1684 std::forward<_Variants>(__variants)...);
1686 return __do_visit<false, false>(std::forward<_Visitor>(__visitor),
1687 std::forward<_Variants>(__variants)...);
1691 template<bool, typename... _Types>
1692 struct __variant_hash_call_base_impl
1695 operator()(const variant<_Types...>& __t) const
1696 noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
1699 __do_visit([&__t, &__ret](auto&& __t_mem) mutable
1700 -> __detail::__variant::__variant_cookie
1702 using _Type = __remove_cvref_t<decltype(__t_mem)>;
1703 if constexpr (!is_same_v<_Type,
1704 __detail::__variant::__variant_cookie>)
1705 __ret = std::hash<size_t>{}(__t.index())
1706 + std::hash<_Type>{}(__t_mem);
1708 __ret = std::hash<size_t>{}(__t.index());
1715 template<typename... _Types>
1716 struct __variant_hash_call_base_impl<false, _Types...> {};
1718 template<typename... _Types>
1719 using __variant_hash_call_base =
1720 __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
1721 __enable_hash_call &&...), _Types...>;
1723 template<typename... _Types>
1724 struct hash<variant<_Types...>>
1725 : private __detail::__variant::_Variant_hash_base<
1726 variant<_Types...>, std::index_sequence_for<_Types...>>,
1727 public __variant_hash_call_base<_Types...>
1729 using result_type [[__deprecated__]] = size_t;
1730 using argument_type [[__deprecated__]] = variant<_Types...>;
1734 struct hash<monostate>
1736 using result_type [[__deprecated__]] = size_t;
1737 using argument_type [[__deprecated__]] = monostate;
1740 operator()(const monostate& __t) const noexcept
1742 constexpr size_t __magic_monostate_hash = -7777;
1743 return __magic_monostate_hash;
1747 template<typename... _Types>
1748 struct __is_fast_hash<hash<variant<_Types...>>>
1749 : bool_constant<(__is_fast_hash<_Types>::value && ...)>
1752 _GLIBCXX_END_NAMESPACE_VERSION
1757 #endif // _GLIBCXX_VARIANT