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 trivially destructible type,
207 template<typename _Type, bool = std::is_trivially_destructible_v<_Type>>
208 struct _Uninitialized;
210 template<typename _Type>
211 struct _Uninitialized<_Type, true>
213 template<typename... _Args>
215 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
216 : _M_storage(std::forward<_Args>(__args)...)
219 constexpr const _Type& _M_get() const & noexcept
220 { return _M_storage; }
222 constexpr _Type& _M_get() & noexcept
223 { return _M_storage; }
225 constexpr const _Type&& _M_get() const && noexcept
226 { return std::move(_M_storage); }
228 constexpr _Type&& _M_get() && noexcept
229 { return std::move(_M_storage); }
234 template<typename _Type>
235 struct _Uninitialized<_Type, false>
237 template<typename... _Args>
239 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
241 ::new ((void*)std::addressof(_M_storage))
242 _Type(std::forward<_Args>(__args)...);
245 const _Type& _M_get() const & noexcept
246 { return *_M_storage._M_ptr(); }
248 _Type& _M_get() & noexcept
249 { return *_M_storage._M_ptr(); }
251 const _Type&& _M_get() const && noexcept
252 { return std::move(*_M_storage._M_ptr()); }
254 _Type&& _M_get() && noexcept
255 { return std::move(*_M_storage._M_ptr()); }
257 __gnu_cxx::__aligned_membuf<_Type> _M_storage;
260 template<typename _Union>
261 constexpr decltype(auto)
262 __get(in_place_index_t<0>, _Union&& __u) noexcept
263 { return std::forward<_Union>(__u)._M_first._M_get(); }
265 template<size_t _Np, typename _Union>
266 constexpr decltype(auto)
267 __get(in_place_index_t<_Np>, _Union&& __u) noexcept
269 return __variant::__get(in_place_index<_Np-1>,
270 std::forward<_Union>(__u)._M_rest);
273 // Returns the typed storage for __v.
274 template<size_t _Np, typename _Variant>
275 constexpr decltype(auto)
276 __get(_Variant&& __v) noexcept
278 return __variant::__get(std::in_place_index<_Np>,
279 std::forward<_Variant>(__v)._M_u);
282 template<typename... _Types>
285 static constexpr bool _S_default_ctor =
286 is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
287 static constexpr bool _S_copy_ctor =
288 (is_copy_constructible_v<_Types> && ...);
289 static constexpr bool _S_move_ctor =
290 (is_move_constructible_v<_Types> && ...);
291 static constexpr bool _S_copy_assign =
293 && (is_copy_assignable_v<_Types> && ...);
294 static constexpr bool _S_move_assign =
296 && (is_move_assignable_v<_Types> && ...);
298 static constexpr bool _S_trivial_dtor =
299 (is_trivially_destructible_v<_Types> && ...);
300 static constexpr bool _S_trivial_copy_ctor =
301 (is_trivially_copy_constructible_v<_Types> && ...);
302 static constexpr bool _S_trivial_move_ctor =
303 (is_trivially_move_constructible_v<_Types> && ...);
304 static constexpr bool _S_trivial_copy_assign =
305 _S_trivial_dtor && _S_trivial_copy_ctor
306 && (is_trivially_copy_assignable_v<_Types> && ...);
307 static constexpr bool _S_trivial_move_assign =
308 _S_trivial_dtor && _S_trivial_move_ctor
309 && (is_trivially_move_assignable_v<_Types> && ...);
311 // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
312 // are always nothrow.
313 static constexpr bool _S_nothrow_default_ctor =
314 is_nothrow_default_constructible_v<
315 typename _Nth_type<0, _Types...>::type>;
316 static constexpr bool _S_nothrow_copy_ctor = false;
317 static constexpr bool _S_nothrow_move_ctor =
318 (is_nothrow_move_constructible_v<_Types> && ...);
319 static constexpr bool _S_nothrow_copy_assign = false;
320 static constexpr bool _S_nothrow_move_assign =
322 && (is_nothrow_move_assignable_v<_Types> && ...);
325 // Defines members and ctors.
326 template<typename... _Types>
327 union _Variadic_union { };
329 template<typename _First, typename... _Rest>
330 union _Variadic_union<_First, _Rest...>
332 constexpr _Variadic_union() : _M_rest() { }
334 template<typename... _Args>
335 constexpr _Variadic_union(in_place_index_t<0>, _Args&&... __args)
336 : _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
339 template<size_t _Np, typename... _Args>
340 constexpr _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
341 : _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
344 _Uninitialized<_First> _M_first;
345 _Variadic_union<_Rest...> _M_rest;
348 // _Never_valueless_alt is true for variant alternatives that can
349 // always be placed in a variant without it becoming valueless.
351 // For suitably-small, trivially copyable types we can create temporaries
352 // on the stack and then memcpy them into place.
353 template<typename _Tp>
354 struct _Never_valueless_alt
355 : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
358 // Specialize _Never_valueless_alt for other types which have a
359 // non-throwing and cheap move construction and move assignment operator,
360 // so that emplacing the type will provide the strong exception-safety
361 // guarantee, by creating and moving a temporary.
362 // Whether _Never_valueless_alt<T> is true or not affects the ABI of a
363 // variant using that alternative, so we can't change the value later!
365 // True if every alternative in _Types... can be emplaced in a variant
366 // without it becoming valueless. If this is true, variant<_Types...>
367 // can never be valueless, which enables some minor optimizations.
368 template <typename... _Types>
369 constexpr bool __never_valueless()
371 return _Traits<_Types...>::_S_move_assign
372 && (_Never_valueless_alt<_Types>::value && ...);
375 // Defines index and the dtor, possibly trivial.
376 template<bool __trivially_destructible, typename... _Types>
377 struct _Variant_storage;
379 template <typename... _Types>
380 using __select_index =
381 typename __select_int::_Select_int_base<sizeof...(_Types),
383 unsigned short>::type::value_type;
385 template<typename... _Types>
386 struct _Variant_storage<false, _Types...>
388 constexpr _Variant_storage() : _M_index(variant_npos) { }
390 template<size_t _Np, typename... _Args>
391 constexpr _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
392 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
398 if (!_M_valid()) [[unlikely]]
401 std::__do_visit<void>([](auto&& __this_mem) mutable
403 std::_Destroy(std::__addressof(__this_mem));
404 }, __variant_cast<_Types...>(*this));
406 _M_index = variant_npos;
413 _M_storage() const noexcept
415 return const_cast<void*>(static_cast<const void*>(
416 std::addressof(_M_u)));
420 _M_valid() const noexcept
422 if constexpr (__variant::__never_valueless<_Types...>())
424 return this->_M_index != __index_type(variant_npos);
427 _Variadic_union<_Types...> _M_u;
428 using __index_type = __select_index<_Types...>;
429 __index_type _M_index;
432 template<typename... _Types>
433 struct _Variant_storage<true, _Types...>
435 constexpr _Variant_storage() : _M_index(variant_npos) { }
437 template<size_t _Np, typename... _Args>
438 constexpr _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
439 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
443 void _M_reset() noexcept
444 { _M_index = variant_npos; }
447 _M_storage() const noexcept
449 return const_cast<void*>(static_cast<const void*>(
450 std::addressof(_M_u)));
454 _M_valid() const noexcept
456 if constexpr (__variant::__never_valueless<_Types...>())
458 return this->_M_index != __index_type(variant_npos);
461 _Variadic_union<_Types...> _M_u;
462 using __index_type = __select_index<_Types...>;
463 __index_type _M_index;
466 template<typename... _Types>
467 using _Variant_storage_alias =
468 _Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
470 template<typename _Tp, typename _Up>
471 void __variant_construct_single(_Tp&& __lhs, _Up&& __rhs_mem)
473 void* __storage = std::addressof(__lhs._M_u);
474 using _Type = remove_reference_t<decltype(__rhs_mem)>;
475 if constexpr (!is_same_v<_Type, __variant_cookie>)
477 _Type(std::forward<decltype(__rhs_mem)>(__rhs_mem));
480 template<typename... _Types, typename _Tp, typename _Up>
481 void __variant_construct(_Tp&& __lhs, _Up&& __rhs)
483 __lhs._M_index = __rhs._M_index;
484 __variant::__raw_visit([&__lhs](auto&& __rhs_mem) mutable
486 __variant_construct_single(std::forward<_Tp>(__lhs),
487 std::forward<decltype(__rhs_mem)>(__rhs_mem));
488 }, __variant_cast<_Types...>(std::forward<_Up>(__rhs)));
491 // The following are (Copy|Move) (ctor|assign) layers for forwarding
492 // triviality and handling non-trivial SMF behaviors.
494 template<bool, typename... _Types>
495 struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
497 using _Base = _Variant_storage_alias<_Types...>;
500 _Copy_ctor_base(const _Copy_ctor_base& __rhs)
501 noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
503 __variant_construct<_Types...>(*this, __rhs);
506 _Copy_ctor_base(_Copy_ctor_base&&) = default;
507 _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
508 _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
511 template<typename... _Types>
512 struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
514 using _Base = _Variant_storage_alias<_Types...>;
518 template<typename... _Types>
519 using _Copy_ctor_alias =
520 _Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
522 template<bool, typename... _Types>
523 struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
525 using _Base = _Copy_ctor_alias<_Types...>;
528 _Move_ctor_base(_Move_ctor_base&& __rhs)
529 noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
531 __variant_construct<_Types...>(*this, std::move(__rhs));
534 template<typename _Up>
535 void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
538 __variant_construct_single(*this, std::forward<_Up>(__rhs));
539 this->_M_index = __rhs_index;
542 template<typename _Up>
543 void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
546 __variant_construct_single(*this, __rhs);
547 this->_M_index = __rhs_index;
550 _Move_ctor_base(const _Move_ctor_base&) = default;
551 _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
552 _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
555 template<typename... _Types>
556 struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
558 using _Base = _Copy_ctor_alias<_Types...>;
561 template<typename _Up>
562 void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
565 __variant_construct_single(*this, std::forward<_Up>(__rhs));
566 this->_M_index = __rhs_index;
569 template<typename _Up>
570 void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
573 __variant_construct_single(*this, __rhs);
574 this->_M_index = __rhs_index;
578 template<typename... _Types>
579 using _Move_ctor_alias =
580 _Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
582 template<bool, typename... _Types>
583 struct _Copy_assign_base : _Move_ctor_alias<_Types...>
585 using _Base = _Move_ctor_alias<_Types...>;
589 operator=(const _Copy_assign_base& __rhs)
590 noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
592 __variant::__raw_idx_visit(
593 [this](auto&& __rhs_mem, auto __rhs_index) mutable
595 if constexpr (__rhs_index != variant_npos)
597 if (this->_M_index == __rhs_index)
598 __variant::__get<__rhs_index>(*this) = __rhs_mem;
601 using __rhs_type = __remove_cvref_t<decltype(__rhs_mem)>;
602 if constexpr (is_nothrow_copy_constructible_v<__rhs_type>
603 || !is_nothrow_move_constructible_v<__rhs_type>)
604 // The standard says this->emplace<__rhs_type>(__rhs_mem)
605 // should be used here, but _M_destructive_copy is
606 // equivalent in this case. Either copy construction
607 // doesn't throw, so _M_destructive_copy gives strong
608 // exception safety guarantee, or both copy construction
609 // and move construction can throw, so emplace only gives
610 // basic exception safety anyway.
611 this->_M_destructive_copy(__rhs_index, __rhs_mem);
613 __variant_cast<_Types...>(*this)
614 = variant<_Types...>(std::in_place_index<__rhs_index>,
620 }, __variant_cast<_Types...>(__rhs));
624 _Copy_assign_base(const _Copy_assign_base&) = default;
625 _Copy_assign_base(_Copy_assign_base&&) = default;
626 _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
629 template<typename... _Types>
630 struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
632 using _Base = _Move_ctor_alias<_Types...>;
636 template<typename... _Types>
637 using _Copy_assign_alias =
638 _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
640 template<bool, typename... _Types>
641 struct _Move_assign_base : _Copy_assign_alias<_Types...>
643 using _Base = _Copy_assign_alias<_Types...>;
647 operator=(_Move_assign_base&& __rhs)
648 noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
650 __variant::__raw_idx_visit(
651 [this](auto&& __rhs_mem, auto __rhs_index) mutable
653 if constexpr (__rhs_index != variant_npos)
655 if (this->_M_index == __rhs_index)
656 __variant::__get<__rhs_index>(*this) = std::move(__rhs_mem);
658 __variant_cast<_Types...>(*this)
659 .template emplace<__rhs_index>(std::move(__rhs_mem));
663 }, __variant_cast<_Types...>(__rhs));
667 _Move_assign_base(const _Move_assign_base&) = default;
668 _Move_assign_base(_Move_assign_base&&) = default;
669 _Move_assign_base& operator=(const _Move_assign_base&) = default;
672 template<typename... _Types>
673 struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
675 using _Base = _Copy_assign_alias<_Types...>;
679 template<typename... _Types>
680 using _Move_assign_alias =
681 _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
683 template<typename... _Types>
684 struct _Variant_base : _Move_assign_alias<_Types...>
686 using _Base = _Move_assign_alias<_Types...>;
690 noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
691 : _Variant_base(in_place_index<0>) { }
693 template<size_t _Np, typename... _Args>
695 _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
696 : _Base(__i, std::forward<_Args>(__args)...)
699 _Variant_base(const _Variant_base&) = default;
700 _Variant_base(_Variant_base&&) = default;
701 _Variant_base& operator=(const _Variant_base&) = default;
702 _Variant_base& operator=(_Variant_base&&) = default;
705 // For how many times does _Tp appear in _Tuple?
706 template<typename _Tp, typename _Tuple>
707 struct __tuple_count;
709 template<typename _Tp, typename _Tuple>
710 inline constexpr size_t __tuple_count_v =
711 __tuple_count<_Tp, _Tuple>::value;
713 template<typename _Tp, typename... _Types>
714 struct __tuple_count<_Tp, tuple<_Types...>>
715 : integral_constant<size_t, 0> { };
717 template<typename _Tp, typename _First, typename... _Rest>
718 struct __tuple_count<_Tp, tuple<_First, _Rest...>>
721 __tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { };
723 // TODO: Reuse this in <tuple> ?
724 template<typename _Tp, typename... _Types>
725 inline constexpr bool __exactly_once =
726 __tuple_count_v<_Tp, tuple<_Types...>> == 1;
728 // Helper used to check for valid conversions that don't involve narrowing.
729 template<typename _Ti> struct _Arr { _Ti _M_x[1]; };
731 // Build an imaginary function FUN(Ti) for each alternative type Ti
732 template<size_t _Ind, typename _Tp, typename _Ti,
733 bool _Ti_is_cv_bool = is_same_v<remove_cv_t<_Ti>, bool>,
737 // This function means 'using _Build_FUN<I, T, Ti>::_S_fun;' is valid,
738 // but only static functions will be considered in the call below.
742 // ... for which Ti x[] = {std::forward<T>(t)}; is well-formed,
743 template<size_t _Ind, typename _Tp, typename _Ti>
744 struct _Build_FUN<_Ind, _Tp, _Ti, false,
745 void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
747 // This is the FUN function for type _Ti, with index _Ind
748 static integral_constant<size_t, _Ind> _S_fun(_Ti);
751 // ... and if Ti is cv bool, remove_cvref_t<T> is bool.
752 template<size_t _Ind, typename _Tp, typename _Ti>
753 struct _Build_FUN<_Ind, _Tp, _Ti, true,
754 enable_if_t<is_same_v<__remove_cvref_t<_Tp>, bool>>>
756 // This is the FUN function for when _Ti is cv bool, with index _Ind
757 static integral_constant<size_t, _Ind> _S_fun(_Ti);
760 template<typename _Tp, typename _Variant,
761 typename = make_index_sequence<variant_size_v<_Variant>>>
764 template<typename _Tp, typename... _Ti, size_t... _Ind>
765 struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
766 : _Build_FUN<_Ind, _Tp, _Ti>...
768 using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
771 // The index j of the overload FUN(Tj) selected by overload resolution
772 // for FUN(std::forward<_Tp>(t))
773 template<typename _Tp, typename _Variant>
775 = decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
777 // The index selected for FUN(std::forward<T>(t)), or variant_npos if none.
778 template<typename _Tp, typename _Variant, typename = void>
779 struct __accepted_index
780 : integral_constant<size_t, variant_npos>
783 template<typename _Tp, typename _Variant>
784 struct __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
785 : _FUN_type<_Tp, _Variant>
788 // Returns the raw storage for __v.
789 template<typename _Variant>
790 void* __get_storage(_Variant&& __v) noexcept
791 { return __v._M_storage(); }
793 template <typename _Maybe_variant_cookie, typename _Variant>
794 struct _Extra_visit_slot_needed
796 template <typename> struct _Variant_never_valueless;
798 template <typename... _Types>
799 struct _Variant_never_valueless<variant<_Types...>>
800 : bool_constant<__variant::__never_valueless<_Types...>()> {};
802 static constexpr bool value =
803 (is_same_v<_Maybe_variant_cookie, __variant_cookie>
804 || is_same_v<_Maybe_variant_cookie, __variant_idx_cookie>)
805 && !_Variant_never_valueless<__remove_cvref_t<_Variant>>::value;
808 // Used for storing a multi-dimensional vtable.
809 template<typename _Tp, size_t... _Dimensions>
812 // Partial specialization with rank zero, stores a single _Tp element.
813 template<typename _Tp>
814 struct _Multi_array<_Tp>
817 struct __untag_result
819 { using element_type = _Tp; };
821 template <typename... _Args>
822 struct __untag_result<const void(*)(_Args...)>
824 { using element_type = void(*)(_Args...); };
826 template <typename... _Args>
827 struct __untag_result<__variant_cookie(*)(_Args...)>
829 { using element_type = void(*)(_Args...); };
831 template <typename... _Args>
832 struct __untag_result<__variant_idx_cookie(*)(_Args...)>
834 { using element_type = void(*)(_Args...); };
836 template <typename _Res, typename... _Args>
837 struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
839 { using element_type = _Res(*)(_Args...); };
841 using __result_is_deduced = __untag_result<_Tp>;
843 constexpr const typename __untag_result<_Tp>::element_type&
847 typename __untag_result<_Tp>::element_type _M_data;
850 // Partial specialization with rank >= 1.
851 template<typename _Ret,
853 typename... _Variants,
854 size_t __first, size_t... __rest>
855 struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
857 static constexpr size_t __index =
858 sizeof...(_Variants) - sizeof...(__rest) - 1;
860 using _Variant = typename _Nth_type<__index, _Variants...>::type;
862 static constexpr int __do_cookie =
863 _Extra_visit_slot_needed<_Ret, _Variant>::value ? 1 : 0;
865 using _Tp = _Ret(*)(_Visitor, _Variants...);
867 template<typename... _Args>
868 constexpr decltype(auto)
869 _M_access(size_t __first_index, _Args... __rest_indices) const
871 return _M_arr[__first_index + __do_cookie]
872 ._M_access(__rest_indices...);
875 _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
878 // Creates a multi-dimensional vtable recursively.
881 // visit([](auto, auto){},
882 // variant<int, char>(), // typedef'ed as V1
883 // variant<float, double, long double>()) // typedef'ed as V2
884 // will trigger instantiations of:
885 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
886 // tuple<V1&&, V2&&>, std::index_sequence<>>
887 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
888 // tuple<V1&&, V2&&>, std::index_sequence<0>>
889 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
890 // tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
891 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
892 // tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
893 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
894 // tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
895 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
896 // tuple<V1&&, V2&&>, std::index_sequence<1>>
897 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
898 // tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
899 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
900 // tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
901 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
902 // tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
903 // The returned multi-dimensional vtable can be fast accessed by the visitor
904 // using index calculation.
905 template<typename _Array_type, typename _Index_seq>
906 struct __gen_vtable_impl;
908 // Defines the _S_apply() member that returns a _Multi_array populated
909 // with function pointers that perform the visitation expressions e(m)
910 // for each valid pack of indexes into the variant types _Variants.
912 // This partial specialization builds up the index sequences by recursively
913 // calling _S_apply() on the next specialization of __gen_vtable_impl.
914 // The base case of the recursion defines the actual function pointers.
915 template<typename _Result_type, typename _Visitor, size_t... __dimensions,
916 typename... _Variants, size_t... __indices>
917 struct __gen_vtable_impl<
918 _Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
919 std::index_sequence<__indices...>>
922 remove_reference_t<typename _Nth_type<sizeof...(__indices),
923 _Variants...>::type>;
925 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
928 static constexpr _Array_type
931 _Array_type __vtable{};
933 __vtable, make_index_sequence<variant_size_v<_Next>>());
937 template<size_t... __var_indices>
938 static constexpr void
939 _S_apply_all_alts(_Array_type& __vtable,
940 std::index_sequence<__var_indices...>)
942 if constexpr (_Extra_visit_slot_needed<_Result_type, _Next>::value)
943 (_S_apply_single_alt<true, __var_indices>(
944 __vtable._M_arr[__var_indices + 1],
945 &(__vtable._M_arr[0])), ...);
947 (_S_apply_single_alt<false, __var_indices>(
948 __vtable._M_arr[__var_indices]), ...);
951 template<bool __do_cookie, size_t __index, typename _Tp>
952 static constexpr void
953 _S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
955 if constexpr (__do_cookie)
957 __element = __gen_vtable_impl<
959 std::index_sequence<__indices..., __index>>::_S_apply();
960 *__cookie_element = __gen_vtable_impl<
962 std::index_sequence<__indices..., variant_npos>>::_S_apply();
966 __element = __gen_vtable_impl<
967 remove_reference_t<decltype(__element)>,
968 std::index_sequence<__indices..., __index>>::_S_apply();
973 // This partial specialization is the base case for the recursion.
974 // It populates a _Multi_array element with the address of a function
975 // that invokes the visitor with the alternatives specified by __indices.
976 template<typename _Result_type, typename _Visitor, typename... _Variants,
978 struct __gen_vtable_impl<
979 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
980 std::index_sequence<__indices...>>
983 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
985 template<size_t __index, typename _Variant>
986 static constexpr decltype(auto)
987 __element_by_index_or_cookie(_Variant&& __var) noexcept
989 if constexpr (__index != variant_npos)
990 return __variant::__get<__index>(std::forward<_Variant>(__var));
992 return __variant_cookie{};
995 static constexpr decltype(auto)
996 __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
998 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
999 // For raw visitation using indices, pass the indices to the visitor
1000 // and discard the return value:
1001 std::__invoke(std::forward<_Visitor>(__visitor),
1002 __element_by_index_or_cookie<__indices>(
1003 std::forward<_Variants>(__vars))...,
1004 integral_constant<size_t, __indices>()...);
1005 else if constexpr (is_same_v<_Result_type, __variant_cookie>)
1006 // For raw visitation without indices, and discard the return value:
1007 std::__invoke(std::forward<_Visitor>(__visitor),
1008 __element_by_index_or_cookie<__indices>(
1009 std::forward<_Variants>(__vars))...);
1010 else if constexpr (_Array_type::__result_is_deduced::value)
1011 // For the usual std::visit case deduce the return value:
1012 return std::__invoke(std::forward<_Visitor>(__visitor),
1013 __element_by_index_or_cookie<__indices>(
1014 std::forward<_Variants>(__vars))...);
1015 else // for std::visit<R> use INVOKE<R>
1016 return std::__invoke_r<_Result_type>(
1017 std::forward<_Visitor>(__visitor),
1018 __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
1021 static constexpr auto
1023 { return _Array_type{&__visit_invoke}; }
1026 template<typename _Result_type, typename _Visitor, typename... _Variants>
1030 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
1031 variant_size_v<remove_reference_t<_Variants>>...>;
1033 static constexpr _Array_type _S_vtable
1034 = __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
1037 template<size_t _Np, typename _Tp>
1038 struct _Base_dedup : public _Tp { };
1040 template<typename _Variant, typename __indices>
1041 struct _Variant_hash_base;
1043 template<typename... _Types, size_t... __indices>
1044 struct _Variant_hash_base<variant<_Types...>,
1045 std::index_sequence<__indices...>>
1046 : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
1048 } // namespace __variant
1049 } // namespace __detail
1051 template<size_t _Np, typename _Variant, typename... _Args>
1052 void __variant_construct_by_index(_Variant& __v, _Args&&... __args)
1055 auto&& __storage = __detail::__variant::__get<_Np>(__v);
1056 ::new ((void*)std::addressof(__storage))
1057 remove_reference_t<decltype(__storage)>
1058 (std::forward<_Args>(__args)...);
1061 template<typename _Tp, typename... _Types>
1063 holds_alternative(const variant<_Types...>& __v) noexcept
1065 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1066 "T must occur exactly once in alternatives");
1067 return __v.index() == __detail::__variant::__index_of_v<_Tp, _Types...>;
1070 template<typename _Tp, typename... _Types>
1071 constexpr _Tp& get(variant<_Types...>& __v)
1073 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1074 "T must occur exactly once in alternatives");
1075 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1076 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
1079 template<typename _Tp, typename... _Types>
1080 constexpr _Tp&& get(variant<_Types...>&& __v)
1082 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1083 "T must occur exactly once in alternatives");
1084 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1085 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1089 template<typename _Tp, typename... _Types>
1090 constexpr const _Tp& get(const variant<_Types...>& __v)
1092 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1093 "T must occur exactly once in alternatives");
1094 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1095 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
1098 template<typename _Tp, typename... _Types>
1099 constexpr const _Tp&& get(const variant<_Types...>&& __v)
1101 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1102 "T must occur exactly once in alternatives");
1103 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1104 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1108 template<size_t _Np, typename... _Types>
1109 constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
1110 get_if(variant<_Types...>* __ptr) noexcept
1112 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1113 static_assert(_Np < sizeof...(_Types),
1114 "The index must be in [0, number of alternatives)");
1115 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1116 if (__ptr && __ptr->index() == _Np)
1117 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1121 template<size_t _Np, typename... _Types>
1123 add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
1124 get_if(const variant<_Types...>* __ptr) noexcept
1126 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1127 static_assert(_Np < sizeof...(_Types),
1128 "The index must be in [0, number of alternatives)");
1129 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1130 if (__ptr && __ptr->index() == _Np)
1131 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1135 template<typename _Tp, typename... _Types>
1136 constexpr add_pointer_t<_Tp>
1137 get_if(variant<_Types...>* __ptr) noexcept
1139 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1140 "T must occur exactly once in alternatives");
1141 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1142 return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1146 template<typename _Tp, typename... _Types>
1147 constexpr add_pointer_t<const _Tp>
1148 get_if(const variant<_Types...>* __ptr) noexcept
1150 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1151 "T must occur exactly once in alternatives");
1152 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1153 return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1157 struct monostate { };
1159 #define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
1160 template<typename... _Types> \
1161 constexpr bool operator __OP(const variant<_Types...>& __lhs, \
1162 const variant<_Types...>& __rhs) \
1164 bool __ret = true; \
1165 __detail::__variant::__raw_idx_visit( \
1166 [&__ret, &__lhs] (auto&& __rhs_mem, auto __rhs_index) mutable \
1168 if constexpr (__rhs_index != variant_npos) \
1170 if (__lhs.index() == __rhs_index) \
1172 auto& __this_mem = std::get<__rhs_index>(__lhs); \
1173 __ret = __this_mem __OP __rhs_mem; \
1176 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1179 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1184 _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
1185 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
1186 _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
1187 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
1188 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
1189 _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
1191 #undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1193 constexpr bool operator==(monostate, monostate) noexcept { return true; }
1195 #ifdef __cpp_lib_three_way_comparison
1196 template<typename... _Types>
1197 requires (three_way_comparable<_Types> && ...)
1199 common_comparison_category_t<compare_three_way_result_t<_Types>...>
1200 operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
1202 common_comparison_category_t<compare_three_way_result_t<_Types>...> __ret
1203 = strong_ordering::equal;
1205 __detail::__variant::__raw_idx_visit(
1206 [&__ret, &__v] (auto&& __w_mem, auto __w_index) mutable
1208 if constexpr (__w_index != variant_npos)
1210 if (__v.index() == __w_index)
1212 auto& __this_mem = std::get<__w_index>(__v);
1213 __ret = __this_mem <=> __w_mem;
1217 __ret = (__v.index() + 1) <=> (__w_index + 1);
1222 constexpr strong_ordering
1223 operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
1225 constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1226 constexpr bool operator<(monostate, monostate) noexcept { return false; }
1227 constexpr bool operator>(monostate, monostate) noexcept { return false; }
1228 constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1229 constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1232 template<typename _Visitor, typename... _Variants>
1233 constexpr decltype(auto) visit(_Visitor&&, _Variants&&...);
1235 template<typename... _Types>
1236 inline enable_if_t<(is_move_constructible_v<_Types> && ...)
1237 && (is_swappable_v<_Types> && ...)>
1238 swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1239 noexcept(noexcept(__lhs.swap(__rhs)))
1240 { __lhs.swap(__rhs); }
1242 template<typename... _Types>
1243 enable_if_t<!((is_move_constructible_v<_Types> && ...)
1244 && (is_swappable_v<_Types> && ...))>
1245 swap(variant<_Types...>&, variant<_Types...>&) = delete;
1247 class bad_variant_access : public exception
1250 bad_variant_access() noexcept { }
1252 const char* what() const noexcept override
1253 { return _M_reason; }
1256 bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
1258 // Must point to a string with static storage duration:
1259 const char* _M_reason = "bad variant access";
1261 friend void __throw_bad_variant_access(const char* __what);
1264 // Must only be called with a string literal
1266 __throw_bad_variant_access(const char* __what)
1267 { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
1270 __throw_bad_variant_access(bool __valueless)
1272 if (__valueless) [[__unlikely__]]
1273 __throw_bad_variant_access("std::get: variant is valueless");
1275 __throw_bad_variant_access("std::get: wrong index for variant");
1278 template<typename... _Types>
1280 : private __detail::__variant::_Variant_base<_Types...>,
1281 private _Enable_default_constructor<
1282 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1283 variant<_Types...>>,
1284 private _Enable_copy_move<
1285 __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1286 __detail::__variant::_Traits<_Types...>::_S_copy_assign,
1287 __detail::__variant::_Traits<_Types...>::_S_move_ctor,
1288 __detail::__variant::_Traits<_Types...>::_S_move_assign,
1292 template <typename... _UTypes, typename _Tp>
1293 friend decltype(auto) __variant_cast(_Tp&&);
1294 template<size_t _Np, typename _Variant, typename... _Args>
1295 friend void __variant_construct_by_index(_Variant& __v,
1298 static_assert(sizeof...(_Types) > 0,
1299 "variant must have at least one alternative");
1300 static_assert(!(std::is_reference_v<_Types> || ...),
1301 "variant must have no reference alternative");
1302 static_assert(!(std::is_void_v<_Types> || ...),
1303 "variant must have no void alternative");
1305 using _Base = __detail::__variant::_Variant_base<_Types...>;
1306 using _Default_ctor_enabler =
1307 _Enable_default_constructor<
1308 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1309 variant<_Types...>>;
1311 template<typename _Tp>
1312 static constexpr bool __not_self
1313 = !is_same_v<__remove_cvref_t<_Tp>, variant>;
1315 template<typename _Tp>
1316 static constexpr bool
1317 __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1319 template<typename _Tp>
1320 static constexpr size_t __accepted_index
1321 = __detail::__variant::__accepted_index<_Tp, variant>::value;
1323 template<size_t _Np, typename = enable_if_t<(_Np < sizeof...(_Types))>>
1324 using __to_type = variant_alternative_t<_Np, variant>;
1326 template<typename _Tp, typename = enable_if_t<__not_self<_Tp>>>
1327 using __accepted_type = __to_type<__accepted_index<_Tp>>;
1329 template<typename _Tp>
1330 static constexpr size_t __index_of =
1331 __detail::__variant::__index_of_v<_Tp, _Types...>;
1333 using _Traits = __detail::__variant::_Traits<_Types...>;
1335 template<typename _Tp>
1336 struct __is_in_place_tag : false_type { };
1337 template<typename _Tp>
1338 struct __is_in_place_tag<in_place_type_t<_Tp>> : true_type { };
1339 template<size_t _Np>
1340 struct __is_in_place_tag<in_place_index_t<_Np>> : true_type { };
1342 template<typename _Tp>
1343 static constexpr bool __not_in_place_tag
1344 = !__is_in_place_tag<__remove_cvref_t<_Tp>>::value;
1347 variant() = default;
1348 variant(const variant& __rhs) = default;
1349 variant(variant&&) = default;
1350 variant& operator=(const variant&) = default;
1351 variant& operator=(variant&&) = default;
1352 ~variant() = default;
1354 template<typename _Tp,
1355 typename = enable_if_t<sizeof...(_Types) != 0>,
1356 typename = enable_if_t<__not_in_place_tag<_Tp>>,
1357 typename _Tj = __accepted_type<_Tp&&>,
1358 typename = enable_if_t<__exactly_once<_Tj>
1359 && is_constructible_v<_Tj, _Tp>>>
1362 noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
1363 : variant(in_place_index<__accepted_index<_Tp>>,
1364 std::forward<_Tp>(__t))
1367 template<typename _Tp, typename... _Args,
1368 typename = enable_if_t<__exactly_once<_Tp>
1369 && is_constructible_v<_Tp, _Args...>>>
1371 variant(in_place_type_t<_Tp>, _Args&&... __args)
1372 : variant(in_place_index<__index_of<_Tp>>,
1373 std::forward<_Args>(__args)...)
1376 template<typename _Tp, typename _Up, typename... _Args,
1377 typename = enable_if_t<__exactly_once<_Tp>
1378 && is_constructible_v<_Tp,
1379 initializer_list<_Up>&, _Args...>>>
1381 variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1383 : variant(in_place_index<__index_of<_Tp>>, __il,
1384 std::forward<_Args>(__args)...)
1387 template<size_t _Np, typename... _Args,
1388 typename _Tp = __to_type<_Np>,
1389 typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
1391 variant(in_place_index_t<_Np>, _Args&&... __args)
1392 : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
1393 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1396 template<size_t _Np, typename _Up, typename... _Args,
1397 typename _Tp = __to_type<_Np>,
1398 typename = enable_if_t<is_constructible_v<_Tp,
1399 initializer_list<_Up>&,
1402 variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1404 : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
1405 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1408 template<typename _Tp>
1409 enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1410 && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
1411 && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
1413 operator=(_Tp&& __rhs)
1414 noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
1415 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
1417 constexpr auto __index = __accepted_index<_Tp>;
1418 if (index() == __index)
1419 std::get<__index>(*this) = std::forward<_Tp>(__rhs);
1422 using _Tj = __accepted_type<_Tp&&>;
1423 if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
1424 || !is_nothrow_move_constructible_v<_Tj>)
1425 this->emplace<__index>(std::forward<_Tp>(__rhs));
1427 operator=(variant(std::forward<_Tp>(__rhs)));
1432 template<typename _Tp, typename... _Args>
1433 enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1435 emplace(_Args&&... __args)
1437 constexpr size_t __index = __index_of<_Tp>;
1438 return this->emplace<__index>(std::forward<_Args>(__args)...);
1441 template<typename _Tp, typename _Up, typename... _Args>
1442 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1443 && __exactly_once<_Tp>,
1445 emplace(initializer_list<_Up> __il, _Args&&... __args)
1447 constexpr size_t __index = __index_of<_Tp>;
1448 return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
1451 template<size_t _Np, typename... _Args>
1452 enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
1454 variant_alternative_t<_Np, variant>&>
1455 emplace(_Args&&... __args)
1457 static_assert(_Np < sizeof...(_Types),
1458 "The index must be in [0, number of alternatives)");
1459 using type = variant_alternative_t<_Np, variant>;
1460 // Provide the strong exception-safety guarantee when possible,
1461 // to avoid becoming valueless.
1462 if constexpr (is_nothrow_constructible_v<type, _Args...>)
1465 __variant_construct_by_index<_Np>(*this,
1466 std::forward<_Args>(__args)...);
1468 else if constexpr (is_scalar_v<type>)
1470 // This might invoke a potentially-throwing conversion operator:
1471 const type __tmp(std::forward<_Args>(__args)...);
1472 // But these steps won't throw:
1474 __variant_construct_by_index<_Np>(*this, __tmp);
1476 else if constexpr (__detail::__variant::_Never_valueless_alt<type>()
1477 && _Traits::_S_move_assign)
1479 // This construction might throw:
1480 variant __tmp(in_place_index<_Np>,
1481 std::forward<_Args>(__args)...);
1482 // But _Never_valueless_alt<type> means this won't:
1483 *this = std::move(__tmp);
1487 // This case only provides the basic exception-safety guarantee,
1488 // i.e. the variant can become valueless.
1492 __variant_construct_by_index<_Np>(*this,
1493 std::forward<_Args>(__args)...);
1497 this->_M_index = variant_npos;
1498 __throw_exception_again;
1501 return std::get<_Np>(*this);
1504 template<size_t _Np, typename _Up, typename... _Args>
1505 enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
1506 initializer_list<_Up>&, _Args...>,
1507 variant_alternative_t<_Np, variant>&>
1508 emplace(initializer_list<_Up> __il, _Args&&... __args)
1510 static_assert(_Np < sizeof...(_Types),
1511 "The index must be in [0, number of alternatives)");
1512 using type = variant_alternative_t<_Np, variant>;
1513 // Provide the strong exception-safety guarantee when possible,
1514 // to avoid becoming valueless.
1515 if constexpr (is_nothrow_constructible_v<type,
1516 initializer_list<_Up>&,
1520 __variant_construct_by_index<_Np>(*this, __il,
1521 std::forward<_Args>(__args)...);
1523 else if constexpr (__detail::__variant::_Never_valueless_alt<type>()
1524 && _Traits::_S_move_assign)
1526 // This construction might throw:
1527 variant __tmp(in_place_index<_Np>, __il,
1528 std::forward<_Args>(__args)...);
1529 // But _Never_valueless_alt<type> means this won't:
1530 *this = std::move(__tmp);
1534 // This case only provides the basic exception-safety guarantee,
1535 // i.e. the variant can become valueless.
1539 __variant_construct_by_index<_Np>(*this, __il,
1540 std::forward<_Args>(__args)...);
1544 this->_M_index = variant_npos;
1545 __throw_exception_again;
1548 return std::get<_Np>(*this);
1551 constexpr bool valueless_by_exception() const noexcept
1552 { return !this->_M_valid(); }
1554 constexpr size_t index() const noexcept
1556 using __index_type = typename _Base::__index_type;
1557 if constexpr (__detail::__variant::__never_valueless<_Types...>())
1558 return this->_M_index;
1559 else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
1560 return make_signed_t<__index_type>(this->_M_index);
1562 return size_t(__index_type(this->_M_index + 1)) - 1;
1566 swap(variant& __rhs)
1567 noexcept((__is_nothrow_swappable<_Types>::value && ...)
1568 && is_nothrow_move_constructible_v<variant>)
1570 __detail::__variant::__raw_idx_visit(
1571 [this, &__rhs](auto&& __rhs_mem, auto __rhs_index) mutable
1573 if constexpr (__rhs_index != variant_npos)
1575 if (this->index() == __rhs_index)
1578 std::get<__rhs_index>(*this);
1580 swap(__this_mem, __rhs_mem);
1584 if (!this->valueless_by_exception()) [[__likely__]]
1586 auto __tmp(std::move(__rhs_mem));
1587 __rhs = std::move(*this);
1588 this->_M_destructive_move(__rhs_index,
1593 this->_M_destructive_move(__rhs_index,
1594 std::move(__rhs_mem));
1601 if (!this->valueless_by_exception()) [[__likely__]]
1603 __rhs = std::move(*this);
1612 #if defined(__clang__) && __clang_major__ <= 7
1614 using _Base::_M_u; // See https://bugs.llvm.org/show_bug.cgi?id=31852
1618 template<size_t _Np, typename _Vp>
1619 friend constexpr decltype(auto)
1620 __detail::__variant::__get(_Vp&& __v) noexcept;
1622 template<typename _Vp>
1624 __detail::__variant::__get_storage(_Vp&& __v) noexcept;
1626 #define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
1627 template<typename... _Tp> \
1628 friend constexpr bool \
1629 operator __OP(const variant<_Tp...>& __lhs, \
1630 const variant<_Tp...>& __rhs);
1632 _VARIANT_RELATION_FUNCTION_TEMPLATE(<)
1633 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
1634 _VARIANT_RELATION_FUNCTION_TEMPLATE(==)
1635 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
1636 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
1637 _VARIANT_RELATION_FUNCTION_TEMPLATE(>)
1639 #undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1642 template<size_t _Np, typename... _Types>
1643 constexpr variant_alternative_t<_Np, variant<_Types...>>&
1644 get(variant<_Types...>& __v)
1646 static_assert(_Np < sizeof...(_Types),
1647 "The index must be in [0, number of alternatives)");
1648 if (__v.index() != _Np)
1649 __throw_bad_variant_access(__v.valueless_by_exception());
1650 return __detail::__variant::__get<_Np>(__v);
1653 template<size_t _Np, typename... _Types>
1654 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1655 get(variant<_Types...>&& __v)
1657 static_assert(_Np < sizeof...(_Types),
1658 "The index must be in [0, number of alternatives)");
1659 if (__v.index() != _Np)
1660 __throw_bad_variant_access(__v.valueless_by_exception());
1661 return __detail::__variant::__get<_Np>(std::move(__v));
1664 template<size_t _Np, typename... _Types>
1665 constexpr const variant_alternative_t<_Np, variant<_Types...>>&
1666 get(const variant<_Types...>& __v)
1668 static_assert(_Np < sizeof...(_Types),
1669 "The index must be in [0, number of alternatives)");
1670 if (__v.index() != _Np)
1671 __throw_bad_variant_access(__v.valueless_by_exception());
1672 return __detail::__variant::__get<_Np>(__v);
1675 template<size_t _Np, typename... _Types>
1676 constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
1677 get(const variant<_Types...>&& __v)
1679 static_assert(_Np < sizeof...(_Types),
1680 "The index must be in [0, number of alternatives)");
1681 if (__v.index() != _Np)
1682 __throw_bad_variant_access(__v.valueless_by_exception());
1683 return __detail::__variant::__get<_Np>(std::move(__v));
1686 template<typename _Result_type, typename _Visitor, typename... _Variants>
1687 constexpr decltype(auto)
1688 __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
1690 constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1691 _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1693 auto __func_ptr = __vtable._M_access(__variants.index()...);
1694 return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1695 std::forward<_Variants>(__variants)...);
1698 template<typename _Visitor, typename... _Variants>
1699 constexpr decltype(auto)
1700 visit(_Visitor&& __visitor, _Variants&&... __variants)
1702 if ((__variants.valueless_by_exception() || ...))
1703 __throw_bad_variant_access("std::visit: variant is valueless");
1705 using _Result_type = std::invoke_result_t<_Visitor,
1706 decltype(std::get<0>(std::declval<_Variants>()))...>;
1708 using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
1710 return std::__do_visit<_Tag>(std::forward<_Visitor>(__visitor),
1711 std::forward<_Variants>(__variants)...);
1714 #if __cplusplus > 201703L
1715 template<typename _Res, typename _Visitor, typename... _Variants>
1717 visit(_Visitor&& __visitor, _Variants&&... __variants)
1719 if ((__variants.valueless_by_exception() || ...))
1720 __throw_bad_variant_access("std::visit<R>: variant is valueless");
1722 return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
1723 std::forward<_Variants>(__variants)...);
1727 template<bool, typename... _Types>
1728 struct __variant_hash_call_base_impl
1731 operator()(const variant<_Types...>& __t) const
1732 noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
1735 __detail::__variant::__raw_visit(
1736 [&__t, &__ret](auto&& __t_mem) mutable
1738 using _Type = __remove_cvref_t<decltype(__t_mem)>;
1739 if constexpr (!is_same_v<_Type,
1740 __detail::__variant::__variant_cookie>)
1741 __ret = std::hash<size_t>{}(__t.index())
1742 + std::hash<_Type>{}(__t_mem);
1744 __ret = std::hash<size_t>{}(__t.index());
1750 template<typename... _Types>
1751 struct __variant_hash_call_base_impl<false, _Types...> {};
1753 template<typename... _Types>
1754 using __variant_hash_call_base =
1755 __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
1756 __enable_hash_call &&...), _Types...>;
1758 template<typename... _Types>
1759 struct hash<variant<_Types...>>
1760 : private __detail::__variant::_Variant_hash_base<
1761 variant<_Types...>, std::index_sequence_for<_Types...>>,
1762 public __variant_hash_call_base<_Types...>
1764 using result_type [[__deprecated__]] = size_t;
1765 using argument_type [[__deprecated__]] = variant<_Types...>;
1769 struct hash<monostate>
1771 using result_type [[__deprecated__]] = size_t;
1772 using argument_type [[__deprecated__]] = monostate;
1775 operator()(const monostate&) const noexcept
1777 constexpr size_t __magic_monostate_hash = -7777;
1778 return __magic_monostate_hash;
1782 template<typename... _Types>
1783 struct __is_fast_hash<hash<variant<_Types...>>>
1784 : bool_constant<(__is_fast_hash<_Types>::value && ...)>
1787 _GLIBCXX_END_NAMESPACE_VERSION
1792 #endif // _GLIBCXX_VARIANT