1 // <variant> -*- C++ -*-
3 // Copyright (C) 2016-2020 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>
48 #if __cplusplus > 201703L
52 namespace std _GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 template<size_t _Np, typename... _Types>
63 template<size_t _Np, typename _First, typename... _Rest>
64 struct _Nth_type<_Np, _First, _Rest...>
65 : _Nth_type<_Np-1, _Rest...> { };
67 template<typename _First, typename... _Rest>
68 struct _Nth_type<0, _First, _Rest...>
69 { using type = _First; };
71 } // namespace __variant
72 } // namespace __detail
74 #define __cpp_lib_variant 201606L
76 template<typename... _Types> class tuple;
77 template<typename... _Types> class variant;
78 template <typename> struct hash;
80 template<typename _Variant>
83 template<typename _Variant>
84 struct variant_size<const _Variant> : variant_size<_Variant> {};
86 template<typename _Variant>
87 struct variant_size<volatile _Variant> : variant_size<_Variant> {};
89 template<typename _Variant>
90 struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
92 template<typename... _Types>
93 struct variant_size<variant<_Types...>>
94 : std::integral_constant<size_t, sizeof...(_Types)> {};
96 template<typename _Variant>
97 inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
99 template<size_t _Np, typename _Variant>
100 struct variant_alternative;
102 template<size_t _Np, typename _First, typename... _Rest>
103 struct variant_alternative<_Np, variant<_First, _Rest...>>
104 : variant_alternative<_Np-1, variant<_Rest...>> {};
106 template<typename _First, typename... _Rest>
107 struct variant_alternative<0, variant<_First, _Rest...>>
108 { using type = _First; };
110 template<size_t _Np, typename _Variant>
111 using variant_alternative_t =
112 typename variant_alternative<_Np, _Variant>::type;
114 template<size_t _Np, typename _Variant>
115 struct variant_alternative<_Np, const _Variant>
116 { using type = add_const_t<variant_alternative_t<_Np, _Variant>>; };
118 template<size_t _Np, typename _Variant>
119 struct variant_alternative<_Np, volatile _Variant>
120 { using type = add_volatile_t<variant_alternative_t<_Np, _Variant>>; };
122 template<size_t _Np, typename _Variant>
123 struct variant_alternative<_Np, const volatile _Variant>
124 { using type = add_cv_t<variant_alternative_t<_Np, _Variant>>; };
126 inline constexpr size_t variant_npos = -1;
128 template<size_t _Np, typename... _Types>
129 constexpr variant_alternative_t<_Np, variant<_Types...>>&
130 get(variant<_Types...>&);
132 template<size_t _Np, typename... _Types>
133 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
134 get(variant<_Types...>&&);
136 template<size_t _Np, typename... _Types>
137 constexpr variant_alternative_t<_Np, variant<_Types...>> const&
138 get(const variant<_Types...>&);
140 template<size_t _Np, typename... _Types>
141 constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
142 get(const variant<_Types...>&&);
144 template<typename _Result_type, typename _Visitor, typename... _Variants>
145 constexpr decltype(auto)
146 __do_visit(_Visitor&& __visitor, _Variants&&... __variants);
148 template <typename... _Types, typename _Tp>
150 __variant_cast(_Tp&& __rhs)
152 if constexpr (is_lvalue_reference_v<_Tp>)
154 if constexpr (is_const_v<remove_reference_t<_Tp>>)
155 return static_cast<const variant<_Types...>&>(__rhs);
157 return static_cast<variant<_Types...>&>(__rhs);
160 return static_cast<variant<_Types...>&&>(__rhs);
167 // Returns the first appearence of _Tp in _Types.
168 // Returns sizeof...(_Types) if _Tp is not in _Types.
169 template<typename _Tp, typename... _Types>
170 struct __index_of : std::integral_constant<size_t, 0> {};
172 template<typename _Tp, typename... _Types>
173 inline constexpr size_t __index_of_v = __index_of<_Tp, _Types...>::value;
175 template<typename _Tp, typename _First, typename... _Rest>
176 struct __index_of<_Tp, _First, _Rest...> :
177 std::integral_constant<size_t, is_same_v<_Tp, _First>
178 ? 0 : __index_of_v<_Tp, _Rest...> + 1> {};
180 // used for raw visitation
181 struct __variant_cookie {};
182 // used for raw visitation with indices passed in
183 struct __variant_idx_cookie { using type = __variant_idx_cookie; };
184 // Used to enable deduction (and same-type checking) for std::visit:
185 template<typename> struct __deduce_visit_result { };
187 // Visit variants that might be valueless.
188 template<typename _Visitor, typename... _Variants>
190 __raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
192 std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
193 std::forward<_Variants>(__variants)...);
196 // Visit variants that might be valueless, passing indices to the visitor.
197 template<typename _Visitor, typename... _Variants>
199 __raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
201 std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
202 std::forward<_Variants>(__variants)...);
205 // _Uninitialized<T> is guaranteed to be a literal type, even if T is not.
206 // We have to do this, because [basic.types]p10.5.3 (n4606) is not implemented
207 // yet. When it's implemented, _Uninitialized<T> can be changed to the alias
208 // to T, therefore equivalent to being removed entirely.
210 // Another reason we may not want to remove _Uninitialzied<T> may be that, we
211 // want _Uninitialized<T> to be trivially destructible, no matter whether T
212 // is; but we will see.
213 template<typename _Type, bool = std::is_literal_type_v<_Type>>
214 struct _Uninitialized;
216 template<typename _Type>
217 struct _Uninitialized<_Type, true>
219 template<typename... _Args>
221 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
222 : _M_storage(std::forward<_Args>(__args)...)
225 constexpr const _Type& _M_get() const & noexcept
226 { return _M_storage; }
228 constexpr _Type& _M_get() & noexcept
229 { return _M_storage; }
231 constexpr const _Type&& _M_get() const && noexcept
232 { return std::move(_M_storage); }
234 constexpr _Type&& _M_get() && noexcept
235 { return std::move(_M_storage); }
240 template<typename _Type>
241 struct _Uninitialized<_Type, false>
243 template<typename... _Args>
245 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
247 ::new ((void*)std::addressof(_M_storage))
248 _Type(std::forward<_Args>(__args)...);
251 const _Type& _M_get() const & noexcept
252 { return *_M_storage._M_ptr(); }
254 _Type& _M_get() & noexcept
255 { return *_M_storage._M_ptr(); }
257 const _Type&& _M_get() const && noexcept
258 { return std::move(*_M_storage._M_ptr()); }
260 _Type&& _M_get() && noexcept
261 { return std::move(*_M_storage._M_ptr()); }
263 __gnu_cxx::__aligned_membuf<_Type> _M_storage;
266 template<typename _Union>
267 constexpr decltype(auto)
268 __get(in_place_index_t<0>, _Union&& __u) noexcept
269 { return std::forward<_Union>(__u)._M_first._M_get(); }
271 template<size_t _Np, typename _Union>
272 constexpr decltype(auto)
273 __get(in_place_index_t<_Np>, _Union&& __u) noexcept
275 return __variant::__get(in_place_index<_Np-1>,
276 std::forward<_Union>(__u)._M_rest);
279 // Returns the typed storage for __v.
280 template<size_t _Np, typename _Variant>
281 constexpr decltype(auto)
282 __get(_Variant&& __v) noexcept
284 return __variant::__get(std::in_place_index<_Np>,
285 std::forward<_Variant>(__v)._M_u);
288 template<typename... _Types>
291 static constexpr bool _S_default_ctor =
292 is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
293 static constexpr bool _S_copy_ctor =
294 (is_copy_constructible_v<_Types> && ...);
295 static constexpr bool _S_move_ctor =
296 (is_move_constructible_v<_Types> && ...);
297 static constexpr bool _S_copy_assign =
299 && (is_copy_assignable_v<_Types> && ...);
300 static constexpr bool _S_move_assign =
302 && (is_move_assignable_v<_Types> && ...);
304 static constexpr bool _S_trivial_dtor =
305 (is_trivially_destructible_v<_Types> && ...);
306 static constexpr bool _S_trivial_copy_ctor =
307 (is_trivially_copy_constructible_v<_Types> && ...);
308 static constexpr bool _S_trivial_move_ctor =
309 (is_trivially_move_constructible_v<_Types> && ...);
310 static constexpr bool _S_trivial_copy_assign =
311 _S_trivial_dtor && _S_trivial_copy_ctor
312 && (is_trivially_copy_assignable_v<_Types> && ...);
313 static constexpr bool _S_trivial_move_assign =
314 _S_trivial_dtor && _S_trivial_move_ctor
315 && (is_trivially_move_assignable_v<_Types> && ...);
317 // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
318 // are always nothrow.
319 static constexpr bool _S_nothrow_default_ctor =
320 is_nothrow_default_constructible_v<
321 typename _Nth_type<0, _Types...>::type>;
322 static constexpr bool _S_nothrow_copy_ctor = false;
323 static constexpr bool _S_nothrow_move_ctor =
324 (is_nothrow_move_constructible_v<_Types> && ...);
325 static constexpr bool _S_nothrow_copy_assign = false;
326 static constexpr bool _S_nothrow_move_assign =
328 && (is_nothrow_move_assignable_v<_Types> && ...);
331 // Defines members and ctors.
332 template<typename... _Types>
333 union _Variadic_union { };
335 template<typename _First, typename... _Rest>
336 union _Variadic_union<_First, _Rest...>
338 constexpr _Variadic_union() : _M_rest() { }
340 template<typename... _Args>
341 constexpr _Variadic_union(in_place_index_t<0>, _Args&&... __args)
342 : _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
345 template<size_t _Np, typename... _Args>
346 constexpr _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
347 : _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
350 _Uninitialized<_First> _M_first;
351 _Variadic_union<_Rest...> _M_rest;
354 // _Never_valueless_alt is true for variant alternatives that can
355 // always be placed in a variant without it becoming valueless.
357 // For suitably-small, trivially copyable types we can create temporaries
358 // on the stack and then memcpy them into place.
359 template<typename _Tp>
360 struct _Never_valueless_alt
361 : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
364 // Specialize _Never_valueless_alt for other types which have a
365 // non-throwing and cheap move construction and move assignment operator,
366 // so that emplacing the type will provide the strong exception-safety
367 // guarantee, by creating and moving a temporary.
368 // Whether _Never_valueless_alt<T> is true or not affects the ABI of a
369 // variant using that alternative, so we can't change the value later!
371 // True if every alternative in _Types... can be emplaced in a variant
372 // without it becoming valueless. If this is true, variant<_Types...>
373 // can never be valueless, which enables some minor optimizations.
374 template <typename... _Types>
375 constexpr bool __never_valueless()
377 return _Traits<_Types...>::_S_move_assign
378 && (_Never_valueless_alt<_Types>::value && ...);
381 // Defines index and the dtor, possibly trivial.
382 template<bool __trivially_destructible, typename... _Types>
383 struct _Variant_storage;
385 template <typename... _Types>
386 using __select_index =
387 typename __select_int::_Select_int_base<sizeof...(_Types),
389 unsigned short>::type::value_type;
391 template<typename... _Types>
392 struct _Variant_storage<false, _Types...>
394 constexpr _Variant_storage() : _M_index(variant_npos) { }
396 template<size_t _Np, typename... _Args>
397 constexpr _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
398 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
404 if (!_M_valid()) [[unlikely]]
407 std::__do_visit<void>([](auto&& __this_mem) mutable
409 std::_Destroy(std::__addressof(__this_mem));
410 }, __variant_cast<_Types...>(*this));
412 _M_index = variant_npos;
419 _M_storage() const noexcept
421 return const_cast<void*>(static_cast<const void*>(
422 std::addressof(_M_u)));
426 _M_valid() const noexcept
428 if constexpr (__variant::__never_valueless<_Types...>())
430 return this->_M_index != __index_type(variant_npos);
433 _Variadic_union<_Types...> _M_u;
434 using __index_type = __select_index<_Types...>;
435 __index_type _M_index;
438 template<typename... _Types>
439 struct _Variant_storage<true, _Types...>
441 constexpr _Variant_storage() : _M_index(variant_npos) { }
443 template<size_t _Np, typename... _Args>
444 constexpr _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
445 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
449 void _M_reset() noexcept
450 { _M_index = variant_npos; }
453 _M_storage() const noexcept
455 return const_cast<void*>(static_cast<const void*>(
456 std::addressof(_M_u)));
460 _M_valid() const noexcept
462 if constexpr (__variant::__never_valueless<_Types...>())
464 return this->_M_index != __index_type(variant_npos);
467 _Variadic_union<_Types...> _M_u;
468 using __index_type = __select_index<_Types...>;
469 __index_type _M_index;
472 template<typename... _Types>
473 using _Variant_storage_alias =
474 _Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
476 template<typename _Tp, typename _Up>
477 void __variant_construct_single(_Tp&& __lhs, _Up&& __rhs_mem)
479 void* __storage = std::addressof(__lhs._M_u);
480 using _Type = remove_reference_t<decltype(__rhs_mem)>;
481 if constexpr (!is_same_v<_Type, __variant_cookie>)
483 _Type(std::forward<decltype(__rhs_mem)>(__rhs_mem));
486 template<typename... _Types, typename _Tp, typename _Up>
487 void __variant_construct(_Tp&& __lhs, _Up&& __rhs)
489 __lhs._M_index = __rhs._M_index;
490 __variant::__raw_visit([&__lhs](auto&& __rhs_mem) mutable
492 __variant_construct_single(std::forward<_Tp>(__lhs),
493 std::forward<decltype(__rhs_mem)>(__rhs_mem));
494 }, __variant_cast<_Types...>(std::forward<_Up>(__rhs)));
497 // The following are (Copy|Move) (ctor|assign) layers for forwarding
498 // triviality and handling non-trivial SMF behaviors.
500 template<bool, typename... _Types>
501 struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
503 using _Base = _Variant_storage_alias<_Types...>;
506 _Copy_ctor_base(const _Copy_ctor_base& __rhs)
507 noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
509 __variant_construct<_Types...>(*this, __rhs);
512 _Copy_ctor_base(_Copy_ctor_base&&) = default;
513 _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
514 _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
517 template<typename... _Types>
518 struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
520 using _Base = _Variant_storage_alias<_Types...>;
524 template<typename... _Types>
525 using _Copy_ctor_alias =
526 _Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
528 template<bool, typename... _Types>
529 struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
531 using _Base = _Copy_ctor_alias<_Types...>;
534 _Move_ctor_base(_Move_ctor_base&& __rhs)
535 noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
537 __variant_construct<_Types...>(*this, std::move(__rhs));
540 template<typename _Up>
541 void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
544 __variant_construct_single(*this, std::forward<_Up>(__rhs));
545 this->_M_index = __rhs_index;
548 template<typename _Up>
549 void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
552 __variant_construct_single(*this, __rhs);
553 this->_M_index = __rhs_index;
556 _Move_ctor_base(const _Move_ctor_base&) = default;
557 _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
558 _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
561 template<typename... _Types>
562 struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
564 using _Base = _Copy_ctor_alias<_Types...>;
567 template<typename _Up>
568 void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
571 __variant_construct_single(*this, std::forward<_Up>(__rhs));
572 this->_M_index = __rhs_index;
575 template<typename _Up>
576 void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
579 __variant_construct_single(*this, __rhs);
580 this->_M_index = __rhs_index;
584 template<typename... _Types>
585 using _Move_ctor_alias =
586 _Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
588 template<bool, typename... _Types>
589 struct _Copy_assign_base : _Move_ctor_alias<_Types...>
591 using _Base = _Move_ctor_alias<_Types...>;
595 operator=(const _Copy_assign_base& __rhs)
596 noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
598 __variant::__raw_idx_visit(
599 [this](auto&& __rhs_mem, auto __rhs_index) mutable
601 if constexpr (__rhs_index != variant_npos)
603 if (this->_M_index == __rhs_index)
604 __variant::__get<__rhs_index>(*this) = __rhs_mem;
607 using __rhs_type = __remove_cvref_t<decltype(__rhs_mem)>;
608 if constexpr (is_nothrow_copy_constructible_v<__rhs_type>
609 || !is_nothrow_move_constructible_v<__rhs_type>)
610 // The standard says this->emplace<__rhs_type>(__rhs_mem)
611 // should be used here, but _M_destructive_copy is
612 // equivalent in this case. Either copy construction
613 // doesn't throw, so _M_destructive_copy gives strong
614 // exception safety guarantee, or both copy construction
615 // and move construction can throw, so emplace only gives
616 // basic exception safety anyway.
617 this->_M_destructive_copy(__rhs_index, __rhs_mem);
619 __variant_cast<_Types...>(*this)
620 = variant<_Types...>(__rhs_mem);
625 }, __variant_cast<_Types...>(__rhs));
629 _Copy_assign_base(const _Copy_assign_base&) = default;
630 _Copy_assign_base(_Copy_assign_base&&) = default;
631 _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
634 template<typename... _Types>
635 struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
637 using _Base = _Move_ctor_alias<_Types...>;
641 template<typename... _Types>
642 using _Copy_assign_alias =
643 _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
645 template<bool, typename... _Types>
646 struct _Move_assign_base : _Copy_assign_alias<_Types...>
648 using _Base = _Copy_assign_alias<_Types...>;
652 operator=(_Move_assign_base&& __rhs)
653 noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
655 __variant::__raw_idx_visit(
656 [this](auto&& __rhs_mem, auto __rhs_index) mutable
658 if constexpr (__rhs_index != variant_npos)
660 if (this->_M_index == __rhs_index)
661 __variant::__get<__rhs_index>(*this) = std::move(__rhs_mem);
663 __variant_cast<_Types...>(*this)
664 .template emplace<__rhs_index>(std::move(__rhs_mem));
668 }, __variant_cast<_Types...>(__rhs));
672 _Move_assign_base(const _Move_assign_base&) = default;
673 _Move_assign_base(_Move_assign_base&&) = default;
674 _Move_assign_base& operator=(const _Move_assign_base&) = default;
677 template<typename... _Types>
678 struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
680 using _Base = _Copy_assign_alias<_Types...>;
684 template<typename... _Types>
685 using _Move_assign_alias =
686 _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
688 template<typename... _Types>
689 struct _Variant_base : _Move_assign_alias<_Types...>
691 using _Base = _Move_assign_alias<_Types...>;
695 noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
696 : _Variant_base(in_place_index<0>) { }
698 template<size_t _Np, typename... _Args>
700 _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
701 : _Base(__i, std::forward<_Args>(__args)...)
704 _Variant_base(const _Variant_base&) = default;
705 _Variant_base(_Variant_base&&) = default;
706 _Variant_base& operator=(const _Variant_base&) = default;
707 _Variant_base& operator=(_Variant_base&&) = default;
710 // For how many times does _Tp appear in _Tuple?
711 template<typename _Tp, typename _Tuple>
712 struct __tuple_count;
714 template<typename _Tp, typename _Tuple>
715 inline constexpr size_t __tuple_count_v =
716 __tuple_count<_Tp, _Tuple>::value;
718 template<typename _Tp, typename... _Types>
719 struct __tuple_count<_Tp, tuple<_Types...>>
720 : integral_constant<size_t, 0> { };
722 template<typename _Tp, typename _First, typename... _Rest>
723 struct __tuple_count<_Tp, tuple<_First, _Rest...>>
726 __tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { };
728 // TODO: Reuse this in <tuple> ?
729 template<typename _Tp, typename... _Types>
730 inline constexpr bool __exactly_once =
731 __tuple_count_v<_Tp, tuple<_Types...>> == 1;
733 // Helper used to check for valid conversions that don't involve narrowing.
734 template<typename _Ti> struct _Arr { _Ti _M_x[1]; };
736 // Build an imaginary function FUN(Ti) for each alternative type Ti
737 template<size_t _Ind, typename _Tp, typename _Ti,
738 bool _Ti_is_cv_bool = is_same_v<remove_cv_t<_Ti>, bool>,
742 // This function means 'using _Build_FUN<I, T, Ti>::_S_fun;' is valid,
743 // but only static functions will be considered in the call below.
747 // ... for which Ti x[] = {std::forward<T>(t)}; is well-formed,
748 template<size_t _Ind, typename _Tp, typename _Ti>
749 struct _Build_FUN<_Ind, _Tp, _Ti, false,
750 void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
752 // This is the FUN function for type _Ti, with index _Ind
753 static integral_constant<size_t, _Ind> _S_fun(_Ti);
756 // ... and if Ti is cv bool, remove_cvref_t<T> is bool.
757 template<size_t _Ind, typename _Tp, typename _Ti>
758 struct _Build_FUN<_Ind, _Tp, _Ti, true,
759 enable_if_t<is_same_v<__remove_cvref_t<_Tp>, bool>>>
761 // This is the FUN function for when _Ti is cv bool, with index _Ind
762 static integral_constant<size_t, _Ind> _S_fun(_Ti);
765 template<typename _Tp, typename _Variant,
766 typename = make_index_sequence<variant_size_v<_Variant>>>
769 template<typename _Tp, typename... _Ti, size_t... _Ind>
770 struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
771 : _Build_FUN<_Ind, _Tp, _Ti>...
773 using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
776 // The index j of the overload FUN(Tj) selected by overload resolution
777 // for FUN(std::forward<_Tp>(t))
778 template<typename _Tp, typename _Variant>
780 = decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
782 // The index selected for FUN(std::forward<T>(t)), or variant_npos if none.
783 template<typename _Tp, typename _Variant, typename = void>
784 struct __accepted_index
785 : integral_constant<size_t, variant_npos>
788 template<typename _Tp, typename _Variant>
789 struct __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
790 : _FUN_type<_Tp, _Variant>
793 // Returns the raw storage for __v.
794 template<typename _Variant>
795 void* __get_storage(_Variant&& __v) noexcept
796 { return __v._M_storage(); }
798 template <typename _Maybe_variant_cookie, typename _Variant>
799 struct _Extra_visit_slot_needed
801 template <typename> struct _Variant_never_valueless;
803 template <typename... _Types>
804 struct _Variant_never_valueless<variant<_Types...>>
805 : bool_constant<__variant::__never_valueless<_Types...>()> {};
807 static constexpr bool value =
808 (is_same_v<_Maybe_variant_cookie, __variant_cookie>
809 || is_same_v<_Maybe_variant_cookie, __variant_idx_cookie>)
810 && !_Variant_never_valueless<__remove_cvref_t<_Variant>>::value;
813 // Used for storing a multi-dimensional vtable.
814 template<typename _Tp, size_t... _Dimensions>
817 // Partial specialization with rank zero, stores a single _Tp element.
818 template<typename _Tp>
819 struct _Multi_array<_Tp>
822 struct __untag_result
824 { using element_type = _Tp; };
826 template <typename... _Args>
827 struct __untag_result<const void(*)(_Args...)>
829 { using element_type = void(*)(_Args...); };
831 template <typename... _Args>
832 struct __untag_result<__variant_cookie(*)(_Args...)>
834 { using element_type = void(*)(_Args...); };
836 template <typename... _Args>
837 struct __untag_result<__variant_idx_cookie(*)(_Args...)>
839 { using element_type = void(*)(_Args...); };
841 template <typename _Res, typename... _Args>
842 struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
844 { using element_type = _Res(*)(_Args...); };
846 using __result_is_deduced = __untag_result<_Tp>;
848 constexpr const typename __untag_result<_Tp>::element_type&
852 typename __untag_result<_Tp>::element_type _M_data;
855 // Partial specialization with rank >= 1.
856 template<typename _Ret,
858 typename... _Variants,
859 size_t __first, size_t... __rest>
860 struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
862 static constexpr size_t __index =
863 sizeof...(_Variants) - sizeof...(__rest) - 1;
865 using _Variant = typename _Nth_type<__index, _Variants...>::type;
867 static constexpr int __do_cookie =
868 _Extra_visit_slot_needed<_Ret, _Variant>::value ? 1 : 0;
870 using _Tp = _Ret(*)(_Visitor, _Variants...);
872 template<typename... _Args>
873 constexpr decltype(auto)
874 _M_access(size_t __first_index, _Args... __rest_indices) const
876 return _M_arr[__first_index + __do_cookie]
877 ._M_access(__rest_indices...);
880 _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
883 // Creates a multi-dimensional vtable recursively.
886 // visit([](auto, auto){},
887 // variant<int, char>(), // typedef'ed as V1
888 // variant<float, double, long double>()) // typedef'ed as V2
889 // will trigger instantiations of:
890 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
891 // tuple<V1&&, V2&&>, std::index_sequence<>>
892 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
893 // tuple<V1&&, V2&&>, std::index_sequence<0>>
894 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
895 // tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
896 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
897 // tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
898 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
899 // tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
900 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
901 // tuple<V1&&, V2&&>, std::index_sequence<1>>
902 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
903 // tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
904 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
905 // tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
906 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
907 // tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
908 // The returned multi-dimensional vtable can be fast accessed by the visitor
909 // using index calculation.
910 template<typename _Array_type, typename _Index_seq>
911 struct __gen_vtable_impl;
913 // Defines the _S_apply() member that returns a _Multi_array populated
914 // with function pointers that perform the visitation expressions e(m)
915 // for each valid pack of indexes into the variant types _Variants.
917 // This partial specialization builds up the index sequences by recursively
918 // calling _S_apply() on the next specialization of __gen_vtable_impl.
919 // The base case of the recursion defines the actual function pointers.
920 template<typename _Result_type, typename _Visitor, size_t... __dimensions,
921 typename... _Variants, size_t... __indices>
922 struct __gen_vtable_impl<
923 _Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
924 std::index_sequence<__indices...>>
927 remove_reference_t<typename _Nth_type<sizeof...(__indices),
928 _Variants...>::type>;
930 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
933 static constexpr _Array_type
936 _Array_type __vtable{};
938 __vtable, make_index_sequence<variant_size_v<_Next>>());
942 template<size_t... __var_indices>
943 static constexpr void
944 _S_apply_all_alts(_Array_type& __vtable,
945 std::index_sequence<__var_indices...>)
947 if constexpr (_Extra_visit_slot_needed<_Result_type, _Next>::value)
948 (_S_apply_single_alt<true, __var_indices>(
949 __vtable._M_arr[__var_indices + 1],
950 &(__vtable._M_arr[0])), ...);
952 (_S_apply_single_alt<false, __var_indices>(
953 __vtable._M_arr[__var_indices]), ...);
956 template<bool __do_cookie, size_t __index, typename _Tp>
957 static constexpr void
958 _S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
960 if constexpr (__do_cookie)
962 __element = __gen_vtable_impl<
964 std::index_sequence<__indices..., __index>>::_S_apply();
965 *__cookie_element = __gen_vtable_impl<
967 std::index_sequence<__indices..., variant_npos>>::_S_apply();
971 __element = __gen_vtable_impl<
972 remove_reference_t<decltype(__element)>,
973 std::index_sequence<__indices..., __index>>::_S_apply();
978 // This partial specialization is the base case for the recursion.
979 // It populates a _Multi_array element with the address of a function
980 // that invokes the visitor with the alternatives specified by __indices.
981 template<typename _Result_type, typename _Visitor, typename... _Variants,
983 struct __gen_vtable_impl<
984 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
985 std::index_sequence<__indices...>>
988 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
990 template<size_t __index, typename _Variant>
991 static constexpr decltype(auto)
992 __element_by_index_or_cookie(_Variant&& __var) noexcept
994 if constexpr (__index != variant_npos)
995 return __variant::__get<__index>(std::forward<_Variant>(__var));
997 return __variant_cookie{};
1000 static constexpr decltype(auto)
1001 __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
1003 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
1004 // For raw visitation using indices, pass the indices to the visitor
1005 // and discard the return value:
1006 std::__invoke(std::forward<_Visitor>(__visitor),
1007 __element_by_index_or_cookie<__indices>(
1008 std::forward<_Variants>(__vars))...,
1009 integral_constant<size_t, __indices>()...);
1010 else if constexpr (is_same_v<_Result_type, __variant_cookie>)
1011 // For raw visitation without indices, and discard the return value:
1012 std::__invoke(std::forward<_Visitor>(__visitor),
1013 __element_by_index_or_cookie<__indices>(
1014 std::forward<_Variants>(__vars))...);
1015 else if constexpr (_Array_type::__result_is_deduced::value)
1016 // For the usual std::visit case deduce the return value:
1017 return std::__invoke(std::forward<_Visitor>(__visitor),
1018 __element_by_index_or_cookie<__indices>(
1019 std::forward<_Variants>(__vars))...);
1020 else // for std::visit<R> use INVOKE<R>
1021 return std::__invoke_r<_Result_type>(
1022 std::forward<_Visitor>(__visitor),
1023 __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
1026 static constexpr auto
1028 { return _Array_type{&__visit_invoke}; }
1031 template<typename _Result_type, typename _Visitor, typename... _Variants>
1035 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
1036 variant_size_v<remove_reference_t<_Variants>>...>;
1038 static constexpr _Array_type _S_vtable
1039 = __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
1042 template<size_t _Np, typename _Tp>
1043 struct _Base_dedup : public _Tp { };
1045 template<typename _Variant, typename __indices>
1046 struct _Variant_hash_base;
1048 template<typename... _Types, size_t... __indices>
1049 struct _Variant_hash_base<variant<_Types...>,
1050 std::index_sequence<__indices...>>
1051 : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
1053 } // namespace __variant
1054 } // namespace __detail
1056 template<size_t _Np, typename _Variant, typename... _Args>
1057 void __variant_construct_by_index(_Variant& __v, _Args&&... __args)
1060 auto&& __storage = __detail::__variant::__get<_Np>(__v);
1061 ::new ((void*)std::addressof(__storage))
1062 remove_reference_t<decltype(__storage)>
1063 (std::forward<_Args>(__args)...);
1066 template<typename _Tp, typename... _Types>
1068 holds_alternative(const variant<_Types...>& __v) noexcept
1070 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1071 "T must occur exactly once in alternatives");
1072 return __v.index() == __detail::__variant::__index_of_v<_Tp, _Types...>;
1075 template<typename _Tp, typename... _Types>
1076 constexpr _Tp& get(variant<_Types...>& __v)
1078 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1079 "T must occur exactly once in alternatives");
1080 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1081 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
1084 template<typename _Tp, typename... _Types>
1085 constexpr _Tp&& get(variant<_Types...>&& __v)
1087 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1088 "T must occur exactly once in alternatives");
1089 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1090 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1094 template<typename _Tp, typename... _Types>
1095 constexpr const _Tp& get(const variant<_Types...>& __v)
1097 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1098 "T must occur exactly once in alternatives");
1099 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1100 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
1103 template<typename _Tp, typename... _Types>
1104 constexpr const _Tp&& get(const variant<_Types...>&& __v)
1106 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1107 "T must occur exactly once in alternatives");
1108 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1109 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1113 template<size_t _Np, typename... _Types>
1114 constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
1115 get_if(variant<_Types...>* __ptr) noexcept
1117 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1118 static_assert(_Np < sizeof...(_Types),
1119 "The index must be in [0, number of alternatives)");
1120 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1121 if (__ptr && __ptr->index() == _Np)
1122 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1126 template<size_t _Np, typename... _Types>
1128 add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
1129 get_if(const variant<_Types...>* __ptr) noexcept
1131 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1132 static_assert(_Np < sizeof...(_Types),
1133 "The index must be in [0, number of alternatives)");
1134 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1135 if (__ptr && __ptr->index() == _Np)
1136 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1140 template<typename _Tp, typename... _Types>
1141 constexpr add_pointer_t<_Tp>
1142 get_if(variant<_Types...>* __ptr) noexcept
1144 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1145 "T must occur exactly once in alternatives");
1146 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1147 return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1151 template<typename _Tp, typename... _Types>
1152 constexpr add_pointer_t<const _Tp>
1153 get_if(const variant<_Types...>* __ptr) noexcept
1155 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1156 "T must occur exactly once in alternatives");
1157 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1158 return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1162 struct monostate { };
1164 #define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
1165 template<typename... _Types> \
1166 constexpr bool operator __OP(const variant<_Types...>& __lhs, \
1167 const variant<_Types...>& __rhs) \
1169 bool __ret = true; \
1170 __detail::__variant::__raw_idx_visit( \
1171 [&__ret, &__lhs] (auto&& __rhs_mem, auto __rhs_index) mutable \
1173 if constexpr (__rhs_index != variant_npos) \
1175 if (__lhs.index() == __rhs_index) \
1177 auto& __this_mem = std::get<__rhs_index>(__lhs); \
1178 __ret = __this_mem __OP __rhs_mem; \
1181 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1184 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1189 _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
1190 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
1191 _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
1192 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
1193 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
1194 _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
1196 #undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1198 constexpr bool operator==(monostate, monostate) noexcept { return true; }
1200 #ifdef __cpp_lib_three_way_comparison
1201 template<typename... _Types>
1202 requires (three_way_comparable<_Types> && ...)
1204 common_comparison_category_t<compare_three_way_result_t<_Types>...>
1205 operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
1207 common_comparison_category_t<compare_three_way_result_t<_Types>...> __ret
1208 = strong_ordering::equal;
1210 __detail::__variant::__raw_idx_visit(
1211 [&__ret, &__v] (auto&& __w_mem, auto __w_index) mutable
1213 if constexpr (__w_index != variant_npos)
1215 if (__v.index() == __w_index)
1217 auto& __this_mem = std::get<__w_index>(__v);
1218 __ret = __this_mem <=> __w_mem;
1222 __ret = (__v.index() + 1) <=> (__w_index + 1);
1227 constexpr strong_ordering
1228 operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
1230 constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1231 constexpr bool operator<(monostate, monostate) noexcept { return false; }
1232 constexpr bool operator>(monostate, monostate) noexcept { return false; }
1233 constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1234 constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1237 template<typename _Visitor, typename... _Variants>
1238 constexpr decltype(auto) visit(_Visitor&&, _Variants&&...);
1240 template<typename... _Types>
1241 inline enable_if_t<(is_move_constructible_v<_Types> && ...)
1242 && (is_swappable_v<_Types> && ...)>
1243 swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1244 noexcept(noexcept(__lhs.swap(__rhs)))
1245 { __lhs.swap(__rhs); }
1247 template<typename... _Types>
1248 enable_if_t<!((is_move_constructible_v<_Types> && ...)
1249 && (is_swappable_v<_Types> && ...))>
1250 swap(variant<_Types...>&, variant<_Types...>&) = delete;
1252 class bad_variant_access : public exception
1255 bad_variant_access() noexcept { }
1257 const char* what() const noexcept override
1258 { return _M_reason; }
1261 bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
1263 // Must point to a string with static storage duration:
1264 const char* _M_reason = "bad variant access";
1266 friend void __throw_bad_variant_access(const char* __what);
1269 // Must only be called with a string literal
1271 __throw_bad_variant_access(const char* __what)
1272 { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
1275 __throw_bad_variant_access(bool __valueless)
1277 if (__valueless) [[__unlikely__]]
1278 __throw_bad_variant_access("std::get: variant is valueless");
1280 __throw_bad_variant_access("std::get: wrong index for variant");
1283 template<typename... _Types>
1285 : private __detail::__variant::_Variant_base<_Types...>,
1286 private _Enable_default_constructor<
1287 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1288 variant<_Types...>>,
1289 private _Enable_copy_move<
1290 __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1291 __detail::__variant::_Traits<_Types...>::_S_copy_assign,
1292 __detail::__variant::_Traits<_Types...>::_S_move_ctor,
1293 __detail::__variant::_Traits<_Types...>::_S_move_assign,
1297 template <typename... _UTypes, typename _Tp>
1298 friend decltype(auto) __variant_cast(_Tp&&);
1299 template<size_t _Np, typename _Variant, typename... _Args>
1300 friend void __variant_construct_by_index(_Variant& __v,
1303 static_assert(sizeof...(_Types) > 0,
1304 "variant must have at least one alternative");
1305 static_assert(!(std::is_reference_v<_Types> || ...),
1306 "variant must have no reference alternative");
1307 static_assert(!(std::is_void_v<_Types> || ...),
1308 "variant must have no void alternative");
1310 using _Base = __detail::__variant::_Variant_base<_Types...>;
1311 using _Default_ctor_enabler =
1312 _Enable_default_constructor<
1313 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1314 variant<_Types...>>;
1316 template<typename _Tp>
1317 static constexpr bool __not_self
1318 = !is_same_v<__remove_cvref_t<_Tp>, variant>;
1320 template<typename _Tp>
1321 static constexpr bool
1322 __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1324 template<typename _Tp>
1325 static constexpr size_t __accepted_index
1326 = __detail::__variant::__accepted_index<_Tp, variant>::value;
1328 template<size_t _Np, typename = enable_if_t<(_Np < sizeof...(_Types))>>
1329 using __to_type = variant_alternative_t<_Np, variant>;
1331 template<typename _Tp, typename = enable_if_t<__not_self<_Tp>>>
1332 using __accepted_type = __to_type<__accepted_index<_Tp>>;
1334 template<typename _Tp>
1335 static constexpr size_t __index_of =
1336 __detail::__variant::__index_of_v<_Tp, _Types...>;
1338 using _Traits = __detail::__variant::_Traits<_Types...>;
1340 template<typename _Tp>
1341 struct __is_in_place_tag : false_type { };
1342 template<typename _Tp>
1343 struct __is_in_place_tag<in_place_type_t<_Tp>> : true_type { };
1344 template<size_t _Np>
1345 struct __is_in_place_tag<in_place_index_t<_Np>> : true_type { };
1347 template<typename _Tp>
1348 static constexpr bool __not_in_place_tag
1349 = !__is_in_place_tag<__remove_cvref_t<_Tp>>::value;
1352 variant() = default;
1353 variant(const variant& __rhs) = default;
1354 variant(variant&&) = default;
1355 variant& operator=(const variant&) = default;
1356 variant& operator=(variant&&) = default;
1357 ~variant() = default;
1359 template<typename _Tp,
1360 typename = enable_if_t<sizeof...(_Types) != 0>,
1361 typename = enable_if_t<__not_in_place_tag<_Tp>>,
1362 typename _Tj = __accepted_type<_Tp&&>,
1363 typename = enable_if_t<__exactly_once<_Tj>
1364 && is_constructible_v<_Tj, _Tp>>>
1367 noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
1368 : variant(in_place_index<__accepted_index<_Tp>>,
1369 std::forward<_Tp>(__t))
1372 template<typename _Tp, typename... _Args,
1373 typename = enable_if_t<__exactly_once<_Tp>
1374 && is_constructible_v<_Tp, _Args...>>>
1376 variant(in_place_type_t<_Tp>, _Args&&... __args)
1377 : variant(in_place_index<__index_of<_Tp>>,
1378 std::forward<_Args>(__args)...)
1381 template<typename _Tp, typename _Up, typename... _Args,
1382 typename = enable_if_t<__exactly_once<_Tp>
1383 && is_constructible_v<_Tp,
1384 initializer_list<_Up>&, _Args...>>>
1386 variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1388 : variant(in_place_index<__index_of<_Tp>>, __il,
1389 std::forward<_Args>(__args)...)
1392 template<size_t _Np, typename... _Args,
1393 typename _Tp = __to_type<_Np>,
1394 typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
1396 variant(in_place_index_t<_Np>, _Args&&... __args)
1397 : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
1398 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1401 template<size_t _Np, typename _Up, typename... _Args,
1402 typename _Tp = __to_type<_Np>,
1403 typename = enable_if_t<is_constructible_v<_Tp,
1404 initializer_list<_Up>&,
1407 variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1409 : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
1410 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1413 template<typename _Tp>
1414 enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1415 && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
1416 && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
1418 operator=(_Tp&& __rhs)
1419 noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
1420 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
1422 constexpr auto __index = __accepted_index<_Tp>;
1423 if (index() == __index)
1424 std::get<__index>(*this) = std::forward<_Tp>(__rhs);
1427 using _Tj = __accepted_type<_Tp&&>;
1428 if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
1429 || !is_nothrow_move_constructible_v<_Tj>)
1430 this->emplace<__index>(std::forward<_Tp>(__rhs));
1432 operator=(variant(std::forward<_Tp>(__rhs)));
1437 template<typename _Tp, typename... _Args>
1438 enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1440 emplace(_Args&&... __args)
1442 constexpr size_t __index = __index_of<_Tp>;
1443 return this->emplace<__index>(std::forward<_Args>(__args)...);
1446 template<typename _Tp, typename _Up, typename... _Args>
1447 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1448 && __exactly_once<_Tp>,
1450 emplace(initializer_list<_Up> __il, _Args&&... __args)
1452 constexpr size_t __index = __index_of<_Tp>;
1453 return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
1456 template<size_t _Np, typename... _Args>
1457 enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
1459 variant_alternative_t<_Np, variant>&>
1460 emplace(_Args&&... __args)
1462 static_assert(_Np < sizeof...(_Types),
1463 "The index must be in [0, number of alternatives)");
1464 using type = variant_alternative_t<_Np, variant>;
1465 // Provide the strong exception-safety guarantee when possible,
1466 // to avoid becoming valueless.
1467 if constexpr (is_nothrow_constructible_v<type, _Args...>)
1470 __variant_construct_by_index<_Np>(*this,
1471 std::forward<_Args>(__args)...);
1473 else if constexpr (is_scalar_v<type>)
1475 // This might invoke a potentially-throwing conversion operator:
1476 const type __tmp(std::forward<_Args>(__args)...);
1477 // But these steps won't throw:
1479 __variant_construct_by_index<_Np>(*this, __tmp);
1481 else if constexpr (__detail::__variant::_Never_valueless_alt<type>()
1482 && _Traits::_S_move_assign)
1484 // This construction might throw:
1485 variant __tmp(in_place_index<_Np>,
1486 std::forward<_Args>(__args)...);
1487 // But _Never_valueless_alt<type> means this won't:
1488 *this = std::move(__tmp);
1492 // This case only provides the basic exception-safety guarantee,
1493 // i.e. the variant can become valueless.
1497 __variant_construct_by_index<_Np>(*this,
1498 std::forward<_Args>(__args)...);
1502 this->_M_index = variant_npos;
1503 __throw_exception_again;
1506 return std::get<_Np>(*this);
1509 template<size_t _Np, typename _Up, typename... _Args>
1510 enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
1511 initializer_list<_Up>&, _Args...>,
1512 variant_alternative_t<_Np, variant>&>
1513 emplace(initializer_list<_Up> __il, _Args&&... __args)
1515 static_assert(_Np < sizeof...(_Types),
1516 "The index must be in [0, number of alternatives)");
1517 using type = variant_alternative_t<_Np, variant>;
1518 // Provide the strong exception-safety guarantee when possible,
1519 // to avoid becoming valueless.
1520 if constexpr (is_nothrow_constructible_v<type,
1521 initializer_list<_Up>&,
1525 __variant_construct_by_index<_Np>(*this, __il,
1526 std::forward<_Args>(__args)...);
1528 else if constexpr (__detail::__variant::_Never_valueless_alt<type>()
1529 && _Traits::_S_move_assign)
1531 // This construction might throw:
1532 variant __tmp(in_place_index<_Np>, __il,
1533 std::forward<_Args>(__args)...);
1534 // But _Never_valueless_alt<type> means this won't:
1535 *this = std::move(__tmp);
1539 // This case only provides the basic exception-safety guarantee,
1540 // i.e. the variant can become valueless.
1544 __variant_construct_by_index<_Np>(*this, __il,
1545 std::forward<_Args>(__args)...);
1549 this->_M_index = variant_npos;
1550 __throw_exception_again;
1553 return std::get<_Np>(*this);
1556 constexpr bool valueless_by_exception() const noexcept
1557 { return !this->_M_valid(); }
1559 constexpr size_t index() const noexcept
1561 using __index_type = typename _Base::__index_type;
1562 if constexpr (__detail::__variant::__never_valueless<_Types...>())
1563 return this->_M_index;
1564 else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
1565 return make_signed_t<__index_type>(this->_M_index);
1567 return size_t(__index_type(this->_M_index + 1)) - 1;
1571 swap(variant& __rhs)
1572 noexcept((__is_nothrow_swappable<_Types>::value && ...)
1573 && is_nothrow_move_constructible_v<variant>)
1575 __detail::__variant::__raw_idx_visit(
1576 [this, &__rhs](auto&& __rhs_mem, auto __rhs_index) mutable
1578 if constexpr (__rhs_index != variant_npos)
1580 if (this->index() == __rhs_index)
1583 std::get<__rhs_index>(*this);
1585 swap(__this_mem, __rhs_mem);
1589 if (!this->valueless_by_exception()) [[__likely__]]
1591 auto __tmp(std::move(__rhs_mem));
1592 __rhs = std::move(*this);
1593 this->_M_destructive_move(__rhs_index,
1598 this->_M_destructive_move(__rhs_index,
1599 std::move(__rhs_mem));
1606 if (!this->valueless_by_exception()) [[__likely__]]
1608 __rhs = std::move(*this);
1617 #if defined(__clang__) && __clang_major__ <= 7
1619 using _Base::_M_u; // See https://bugs.llvm.org/show_bug.cgi?id=31852
1623 template<size_t _Np, typename _Vp>
1624 friend constexpr decltype(auto)
1625 __detail::__variant::__get(_Vp&& __v) noexcept;
1627 template<typename _Vp>
1629 __detail::__variant::__get_storage(_Vp&& __v) noexcept;
1631 #define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
1632 template<typename... _Tp> \
1633 friend constexpr bool \
1634 operator __OP(const variant<_Tp...>& __lhs, \
1635 const variant<_Tp...>& __rhs);
1637 _VARIANT_RELATION_FUNCTION_TEMPLATE(<)
1638 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
1639 _VARIANT_RELATION_FUNCTION_TEMPLATE(==)
1640 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
1641 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
1642 _VARIANT_RELATION_FUNCTION_TEMPLATE(>)
1644 #undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1647 template<size_t _Np, typename... _Types>
1648 constexpr variant_alternative_t<_Np, variant<_Types...>>&
1649 get(variant<_Types...>& __v)
1651 static_assert(_Np < sizeof...(_Types),
1652 "The index must be in [0, number of alternatives)");
1653 if (__v.index() != _Np)
1654 __throw_bad_variant_access(__v.valueless_by_exception());
1655 return __detail::__variant::__get<_Np>(__v);
1658 template<size_t _Np, typename... _Types>
1659 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1660 get(variant<_Types...>&& __v)
1662 static_assert(_Np < sizeof...(_Types),
1663 "The index must be in [0, number of alternatives)");
1664 if (__v.index() != _Np)
1665 __throw_bad_variant_access(__v.valueless_by_exception());
1666 return __detail::__variant::__get<_Np>(std::move(__v));
1669 template<size_t _Np, typename... _Types>
1670 constexpr const variant_alternative_t<_Np, variant<_Types...>>&
1671 get(const variant<_Types...>& __v)
1673 static_assert(_Np < sizeof...(_Types),
1674 "The index must be in [0, number of alternatives)");
1675 if (__v.index() != _Np)
1676 __throw_bad_variant_access(__v.valueless_by_exception());
1677 return __detail::__variant::__get<_Np>(__v);
1680 template<size_t _Np, typename... _Types>
1681 constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
1682 get(const variant<_Types...>&& __v)
1684 static_assert(_Np < sizeof...(_Types),
1685 "The index must be in [0, number of alternatives)");
1686 if (__v.index() != _Np)
1687 __throw_bad_variant_access(__v.valueless_by_exception());
1688 return __detail::__variant::__get<_Np>(std::move(__v));
1691 template<typename _Result_type, typename _Visitor, typename... _Variants>
1692 constexpr decltype(auto)
1693 __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
1695 constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1696 _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1698 auto __func_ptr = __vtable._M_access(__variants.index()...);
1699 return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1700 std::forward<_Variants>(__variants)...);
1703 template<typename _Visitor, typename... _Variants>
1704 constexpr decltype(auto)
1705 visit(_Visitor&& __visitor, _Variants&&... __variants)
1707 if ((__variants.valueless_by_exception() || ...))
1708 __throw_bad_variant_access("std::visit: variant is valueless");
1710 using _Result_type = std::invoke_result_t<_Visitor,
1711 decltype(std::get<0>(std::declval<_Variants>()))...>;
1713 using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
1715 return std::__do_visit<_Tag>(std::forward<_Visitor>(__visitor),
1716 std::forward<_Variants>(__variants)...);
1719 #if __cplusplus > 201703L
1720 template<typename _Res, typename _Visitor, typename... _Variants>
1722 visit(_Visitor&& __visitor, _Variants&&... __variants)
1724 if ((__variants.valueless_by_exception() || ...))
1725 __throw_bad_variant_access("std::visit<R>: variant is valueless");
1727 return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
1728 std::forward<_Variants>(__variants)...);
1732 template<bool, typename... _Types>
1733 struct __variant_hash_call_base_impl
1736 operator()(const variant<_Types...>& __t) const
1737 noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
1740 __detail::__variant::__raw_visit(
1741 [&__t, &__ret](auto&& __t_mem) mutable
1743 using _Type = __remove_cvref_t<decltype(__t_mem)>;
1744 if constexpr (!is_same_v<_Type,
1745 __detail::__variant::__variant_cookie>)
1746 __ret = std::hash<size_t>{}(__t.index())
1747 + std::hash<_Type>{}(__t_mem);
1749 __ret = std::hash<size_t>{}(__t.index());
1755 template<typename... _Types>
1756 struct __variant_hash_call_base_impl<false, _Types...> {};
1758 template<typename... _Types>
1759 using __variant_hash_call_base =
1760 __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
1761 __enable_hash_call &&...), _Types...>;
1763 template<typename... _Types>
1764 struct hash<variant<_Types...>>
1765 : private __detail::__variant::_Variant_hash_base<
1766 variant<_Types...>, std::index_sequence_for<_Types...>>,
1767 public __variant_hash_call_base<_Types...>
1769 using result_type [[__deprecated__]] = size_t;
1770 using argument_type [[__deprecated__]] = variant<_Types...>;
1774 struct hash<monostate>
1776 using result_type [[__deprecated__]] = size_t;
1777 using argument_type [[__deprecated__]] = monostate;
1780 operator()(const monostate&) const noexcept
1782 constexpr size_t __magic_monostate_hash = -7777;
1783 return __magic_monostate_hash;
1787 template<typename... _Types>
1788 struct __is_fast_hash<hash<variant<_Types...>>>
1789 : bool_constant<(__is_fast_hash<_Types>::value && ...)>
1792 _GLIBCXX_END_NAMESPACE_VERSION
1797 #endif // _GLIBCXX_VARIANT