libstdc++
variant
Go to the documentation of this file.
1// <variant> -*- C++ -*-
2
3// Copyright (C) 2016-2024 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/** @file variant
26 * This is the `<variant>` C++ Library header.
27 */
28
29#ifndef _GLIBCXX_VARIANT
30#define _GLIBCXX_VARIANT 1
31
32#pragma GCC system_header
33
34#define __glibcxx_want_freestanding_variant
35#define __glibcxx_want_variant
36#define __glibcxx_want_constrained_equality
37#include <bits/version.h>
38
39#ifdef __cpp_lib_variant // C++ >= 17
40#include <initializer_list>
41#include <type_traits>
45#include <bits/invoke.h>
46#include <bits/parse_numbers.h> // _Select_int
48#include <bits/stl_construct.h>
49#include <bits/utility.h> // in_place_index_t
50#if __cplusplus >= 202002L
51# include <concepts>
52# include <compare>
53#endif
54
55// C++ < 20 || __cpp_concepts < 202002L || __cpp_constexpr < 201811L
56#if __cpp_lib_variant < 202106L
57# include <ext/aligned_buffer.h> // Use __aligned_membuf for storage.
58#endif
59
60namespace std _GLIBCXX_VISIBILITY(default)
61{
62_GLIBCXX_BEGIN_NAMESPACE_VERSION
63
64 template<typename... _Types> class tuple;
65 template<typename... _Types> class variant;
66 template <typename> struct hash;
67
68 template<typename _Variant>
69 struct variant_size;
70
71 template<typename _Variant>
72 struct variant_size<const _Variant> : variant_size<_Variant> {};
73
74 template<typename _Variant>
75 struct variant_size<volatile _Variant> : variant_size<_Variant> {};
76
77 template<typename _Variant>
78 struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
79
80 template<typename... _Types>
81 struct variant_size<variant<_Types...>>
82 : std::integral_constant<size_t, sizeof...(_Types)> {};
83
84 template<typename _Variant>
85 inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
86
87 template<typename... _Types>
88 inline constexpr size_t
89 variant_size_v<variant<_Types...>> = sizeof...(_Types);
90
91 template<typename... _Types>
92 inline constexpr size_t
93 variant_size_v<const variant<_Types...>> = sizeof...(_Types);
94
95 template<size_t _Np, typename _Variant>
96 struct variant_alternative;
97
98 template<size_t _Np, typename... _Types>
99 struct variant_alternative<_Np, variant<_Types...>>
100 {
101 static_assert(_Np < sizeof...(_Types));
102
103 using type = typename _Nth_type<_Np, _Types...>::type;
104 };
105
106 template<size_t _Np, typename _Variant>
107 using variant_alternative_t =
108 typename variant_alternative<_Np, _Variant>::type;
109
110 template<size_t _Np, typename _Variant>
111 struct variant_alternative<_Np, const _Variant>
112 { using type = const variant_alternative_t<_Np, _Variant>; };
113
114 template<size_t _Np, typename _Variant>
115 struct variant_alternative<_Np, volatile _Variant>
116 { using type = volatile variant_alternative_t<_Np, _Variant>; };
117
118 template<size_t _Np, typename _Variant>
119 struct variant_alternative<_Np, const volatile _Variant>
120 { using type = const volatile variant_alternative_t<_Np, _Variant>; };
121
122 inline constexpr size_t variant_npos = -1;
123
124 template<size_t _Np, typename... _Types>
125 constexpr variant_alternative_t<_Np, variant<_Types...>>&
126 get(variant<_Types...>&);
127
128 template<size_t _Np, typename... _Types>
129 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
130 get(variant<_Types...>&&);
131
132 template<size_t _Np, typename... _Types>
133 constexpr variant_alternative_t<_Np, variant<_Types...>> const&
134 get(const variant<_Types...>&);
135
136 template<size_t _Np, typename... _Types>
137 constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
138 get(const variant<_Types...>&&);
139
140 template<typename _Result_type, typename _Visitor, typename... _Variants>
141 constexpr decltype(auto)
142 __do_visit(_Visitor&& __visitor, _Variants&&... __variants);
143
144 template <typename... _Types, typename _Tp>
145 _GLIBCXX20_CONSTEXPR
146 decltype(auto)
147 __variant_cast(_Tp&& __rhs)
148 {
149 if constexpr (is_lvalue_reference_v<_Tp>)
150 {
151 if constexpr (is_const_v<remove_reference_t<_Tp>>)
152 return static_cast<const variant<_Types...>&>(__rhs);
153 else
154 return static_cast<variant<_Types...>&>(__rhs);
155 }
156 else
157 return static_cast<variant<_Types...>&&>(__rhs);
158 }
159
160namespace __detail
161{
162namespace __variant
163{
164 // used for raw visitation
165 struct __variant_cookie {};
166 // used for raw visitation with indices passed in
167 struct __variant_idx_cookie { using type = __variant_idx_cookie; };
168 // Used to enable deduction (and same-type checking) for std::visit:
169 template<typename _Tp> struct __deduce_visit_result { using type = _Tp; };
170
171 // Visit variants that might be valueless.
172 template<typename _Visitor, typename... _Variants>
173 constexpr void
174 __raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
175 {
176 std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
177 std::forward<_Variants>(__variants)...);
178 }
179
180 // Visit variants that might be valueless, passing indices to the visitor.
181 template<typename _Visitor, typename... _Variants>
182 constexpr void
183 __raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
184 {
185 std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
186 std::forward<_Variants>(__variants)...);
187 }
188
189 // The __as function templates implement the exposition-only "as-variant"
190
191 template<typename... _Types>
192 constexpr std::variant<_Types...>&
193 __as(std::variant<_Types...>& __v) noexcept
194 { return __v; }
195
196 template<typename... _Types>
197 constexpr const std::variant<_Types...>&
198 __as(const std::variant<_Types...>& __v) noexcept
199 { return __v; }
200
201 template<typename... _Types>
202 constexpr std::variant<_Types...>&&
203 __as(std::variant<_Types...>&& __v) noexcept
204 { return std::move(__v); }
205
206 template<typename... _Types>
207 constexpr const std::variant<_Types...>&&
208 __as(const std::variant<_Types...>&& __v) noexcept
209 { return std::move(__v); }
210
211#if __cpp_lib_variant < 202106L
212 template<typename _Type, bool = std::is_trivially_destructible_v<_Type>>
213 struct _Uninitialized;
214#else
215 template<typename _Type, bool /* unused */ = true>
216 struct _Uninitialized;
217#endif
218
219 // The primary template is used for trivially destructible types in C++17,
220 // and for all types in C++20.
221 template<typename _Type, bool>
222 struct _Uninitialized
223 {
224 template<typename... _Args>
225 constexpr
226 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
227 : _M_storage(std::forward<_Args>(__args)...)
228 { }
229
230#if __cpp_lib_variant < 202106L
231 constexpr const _Type& _M_get() const & noexcept
232 { return _M_storage; }
233
234 constexpr _Type& _M_get() & noexcept
235 { return _M_storage; }
236
237 constexpr const _Type&& _M_get() const && noexcept
238 { return std::move(_M_storage); }
239
240 constexpr _Type&& _M_get() && noexcept
241 { return std::move(_M_storage); }
242#endif
243
244 _Type _M_storage;
245 };
246
247#if __cpp_lib_variant < 202106L
248 // This partial specialization is used for non-trivially destructible types
249 // in C++17, so that _Uninitialized<T> is trivially destructible and can be
250 // used as a union member in _Variadic_union.
251 template<typename _Type>
252 struct _Uninitialized<_Type, false>
253 {
254 template<typename... _Args>
255 constexpr
256 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
257 {
258 ::new ((void*)std::addressof(_M_storage))
259 _Type(std::forward<_Args>(__args)...);
260 }
261
262 const _Type& _M_get() const & noexcept
263 { return *_M_storage._M_ptr(); }
264
265 _Type& _M_get() & noexcept
266 { return *_M_storage._M_ptr(); }
267
268 const _Type&& _M_get() const && noexcept
269 { return std::move(*_M_storage._M_ptr()); }
270
271 _Type&& _M_get() && noexcept
272 { return std::move(*_M_storage._M_ptr()); }
273
274 __gnu_cxx::__aligned_membuf<_Type> _M_storage;
275 };
276
277 template<size_t _Np, typename _Union>
278 constexpr decltype(auto)
279 __get_n(_Union&& __u) noexcept
280 {
281 if constexpr (_Np == 0)
282 return std::forward<_Union>(__u)._M_first._M_get();
283 else if constexpr (_Np == 1)
284 return std::forward<_Union>(__u)._M_rest._M_first._M_get();
285 else if constexpr (_Np == 2)
286 return std::forward<_Union>(__u)._M_rest._M_rest._M_first._M_get();
287 else
288 return __variant::__get_n<_Np - 3>(
289 std::forward<_Union>(__u)._M_rest._M_rest._M_rest);
290 }
291#else
292 template<size_t _Np, typename _Union>
293 constexpr auto&&
294 __get_n(_Union&& __u) noexcept
295 {
296 if constexpr (_Np == 0)
297 return std::forward<_Union>(__u)._M_first._M_storage;
298 else if constexpr (_Np == 1)
299 return std::forward<_Union>(__u)._M_rest._M_first._M_storage;
300 else if constexpr (_Np == 2)
301 return std::forward<_Union>(__u)._M_rest._M_rest._M_first._M_storage;
302 else
303 return __variant::__get_n<_Np - 3>(
304 std::forward<_Union>(__u)._M_rest._M_rest._M_rest);
305 }
306#endif
307
308 // Returns the typed storage for __v.
309 template<size_t _Np, typename _Variant>
310 constexpr decltype(auto)
311 __get(_Variant&& __v) noexcept
312 { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
313
314 // Gets the _Uninitialized to construct into for __u.
315 template<size_t _Np, typename _Union>
316 constexpr decltype(auto)
317 __construct_n(_Union& __u) noexcept
318 {
319 if constexpr (_Np == 0)
320 return &__u._M_first;
321 else if constexpr (_Np == 1)
322 {
323 std::_Construct(&__u._M_rest);
324 return &__u._M_rest._M_first;
325 }
326 else if constexpr (_Np == 2)
327 {
328 std::_Construct(&__u._M_rest);
329 std::_Construct(&__u._M_rest._M_rest);
330 return &__u._M_rest._M_rest._M_first;
331 }
332 else
333 {
334 std::_Construct(&__u._M_rest);
335 std::_Construct(&__u._M_rest._M_rest);
336 std::_Construct(&__u._M_rest._M_rest._M_rest);
337 return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest);
338 }
339 }
340
341 template<typename... _Types>
342 struct _Traits
343 {
344 static constexpr bool _S_default_ctor =
345 is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
346 static constexpr bool _S_copy_ctor =
347 (is_copy_constructible_v<_Types> && ...);
348 static constexpr bool _S_move_ctor =
349 (is_move_constructible_v<_Types> && ...);
350 static constexpr bool _S_copy_assign =
351 _S_copy_ctor
352 && (is_copy_assignable_v<_Types> && ...);
353 static constexpr bool _S_move_assign =
354 _S_move_ctor
355 && (is_move_assignable_v<_Types> && ...);
356
357 static constexpr bool _S_trivial_dtor =
358 (is_trivially_destructible_v<_Types> && ...);
359 static constexpr bool _S_trivial_copy_ctor =
360 (is_trivially_copy_constructible_v<_Types> && ...);
361 static constexpr bool _S_trivial_move_ctor =
362 (is_trivially_move_constructible_v<_Types> && ...);
363 static constexpr bool _S_trivial_copy_assign =
364 _S_trivial_dtor && _S_trivial_copy_ctor
365 && (is_trivially_copy_assignable_v<_Types> && ...);
366 static constexpr bool _S_trivial_move_assign =
367 _S_trivial_dtor && _S_trivial_move_ctor
368 && (is_trivially_move_assignable_v<_Types> && ...);
369
370 // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
371 // are always nothrow.
372 static constexpr bool _S_nothrow_default_ctor =
373 is_nothrow_default_constructible_v<
374 typename _Nth_type<0, _Types...>::type>;
375 static constexpr bool _S_nothrow_copy_ctor = false;
376 static constexpr bool _S_nothrow_move_ctor =
377 (is_nothrow_move_constructible_v<_Types> && ...);
378 static constexpr bool _S_nothrow_copy_assign = false;
379 static constexpr bool _S_nothrow_move_assign =
380 _S_nothrow_move_ctor
381 && (is_nothrow_move_assignable_v<_Types> && ...);
382 };
383
384 // Defines members and ctors.
385 template<bool __trivially_destructible, typename... _Types>
386 union _Variadic_union
387 {
388 _Variadic_union() = default;
389
390 template<size_t _Np, typename... _Args>
391 _Variadic_union(in_place_index_t<_Np>, _Args&&...) = delete;
392 };
393
394 template<bool __trivially_destructible, typename _First, typename... _Rest>
395 union _Variadic_union<__trivially_destructible, _First, _Rest...>
396 {
397 constexpr _Variadic_union() : _M_rest() { }
398
399 template<typename... _Args>
400 constexpr
401 _Variadic_union(in_place_index_t<0>, _Args&&... __args)
402 : _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
403 { }
404
405 template<size_t _Np, typename... _Args>
406 constexpr
407 _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
408 : _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
409 { }
410
411#if __cpp_lib_variant >= 202106L
412 _Variadic_union(const _Variadic_union&) = default;
413 _Variadic_union(_Variadic_union&&) = default;
414 _Variadic_union& operator=(const _Variadic_union&) = default;
415 _Variadic_union& operator=(_Variadic_union&&) = default;
416
417 ~_Variadic_union() = default;
418
419 // If any alternative type is not trivially destructible then we need a
420 // user-provided destructor that does nothing. The active alternative
421 // will be destroyed by _Variant_storage::_M_reset() instead of here.
422 constexpr ~_Variadic_union()
423 requires (!__trivially_destructible)
424 { }
425#endif
426
427 _Uninitialized<_First> _M_first;
428 _Variadic_union<__trivially_destructible, _Rest...> _M_rest;
429 };
430
431 // _Never_valueless_alt is true for variant alternatives that can
432 // always be placed in a variant without it becoming valueless.
433
434 // For suitably-small, trivially copyable types we can create temporaries
435 // on the stack and then memcpy them into place.
436 template<typename _Tp>
437 struct _Never_valueless_alt
438 : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
439 { };
440
441 // Specialize _Never_valueless_alt for other types which have a
442 // non-throwing and cheap move construction and move assignment operator,
443 // so that emplacing the type will provide the strong exception-safety
444 // guarantee, by creating and moving a temporary.
445 // Whether _Never_valueless_alt<T> is true or not affects the ABI of a
446 // variant using that alternative, so we can't change the value later!
447
448 // True if every alternative in _Types... can be emplaced in a variant
449 // without it becoming valueless. If this is true, variant<_Types...>
450 // can never be valueless, which enables some minor optimizations.
451 template <typename... _Types>
452 constexpr bool __never_valueless()
453 {
454 return _Traits<_Types...>::_S_move_assign
455 && (_Never_valueless_alt<_Types>::value && ...);
456 }
457
458 // Defines index and the dtor, possibly trivial.
459 template<bool __trivially_destructible, typename... _Types>
460 struct _Variant_storage;
461
462 template <typename... _Types>
463 using __select_index =
464 typename __select_int::_Select_int_base<sizeof...(_Types),
465 unsigned char,
466 unsigned short>::type::value_type;
467
468 template<typename... _Types>
469 struct _Variant_storage<false, _Types...>
470 {
471 constexpr
472 _Variant_storage()
473 : _M_index(static_cast<__index_type>(variant_npos))
474 { }
475
476 template<size_t _Np, typename... _Args>
477 constexpr
478 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
479 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
480 _M_index{_Np}
481 { }
482
483 constexpr void
484 _M_reset()
485 {
486 if (!_M_valid()) [[__unlikely__]]
487 return;
488
489 std::__do_visit<void>([](auto&& __this_mem) mutable
490 {
491 std::_Destroy(std::__addressof(__this_mem));
492 }, __variant_cast<_Types...>(*this));
493
494 _M_index = static_cast<__index_type>(variant_npos);
495 }
496
497 _GLIBCXX20_CONSTEXPR
498 ~_Variant_storage()
499 { _M_reset(); }
500
501 constexpr bool
502 _M_valid() const noexcept
503 {
504 if constexpr (__variant::__never_valueless<_Types...>())
505 return true;
506 return this->_M_index != __index_type(variant_npos);
507 }
508
509 _Variadic_union<false, _Types...> _M_u;
510 using __index_type = __select_index<_Types...>;
511 __index_type _M_index;
512 };
513
514 template<typename... _Types>
515 struct _Variant_storage<true, _Types...>
516 {
517 constexpr
518 _Variant_storage()
519 : _M_index(static_cast<__index_type>(variant_npos))
520 { }
521
522 template<size_t _Np, typename... _Args>
523 constexpr
524 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
525 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
526 _M_index{_Np}
527 { }
528
529 constexpr void
530 _M_reset() noexcept
531 { _M_index = static_cast<__index_type>(variant_npos); }
532
533 constexpr bool
534 _M_valid() const noexcept
535 {
536 if constexpr (__variant::__never_valueless<_Types...>())
537 return true;
538 // It would be nice if we could just return true for -fno-exceptions.
539 // It's possible (but inadvisable) that a std::variant could become
540 // valueless in a translation unit compiled with -fexceptions and then
541 // be passed to functions compiled with -fno-exceptions. We would need
542 // some #ifdef _GLIBCXX_NO_EXCEPTIONS_GLOBALLY property to elide all
543 // checks for valueless_by_exception().
544 return this->_M_index != static_cast<__index_type>(variant_npos);
545 }
546
547 _Variadic_union<true, _Types...> _M_u;
548 using __index_type = __select_index<_Types...>;
549 __index_type _M_index;
550 };
551
552 // Implementation of v.emplace<N>(args...).
553 template<size_t _Np, bool _Triv, typename... _Types, typename... _Args>
554 _GLIBCXX20_CONSTEXPR
555 inline void
556 __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
557 {
558 __v._M_reset();
559 auto* __addr = __variant::__construct_n<_Np>(__v._M_u);
560 std::_Construct(__addr, in_place_index<0>,
561 std::forward<_Args>(__args)...);
562 // Construction didn't throw, so can set the new index now:
563 __v._M_index = _Np;
564 }
565
566 template<typename... _Types>
567 using _Variant_storage_alias =
568 _Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
569
570 // The following are (Copy|Move) (ctor|assign) layers for forwarding
571 // triviality and handling non-trivial SMF behaviors.
572
573 template<bool, typename... _Types>
574 struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
575 {
576 using _Base = _Variant_storage_alias<_Types...>;
577 using _Base::_Base;
578
579 _GLIBCXX20_CONSTEXPR
580 _Copy_ctor_base(const _Copy_ctor_base& __rhs)
581 noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
582 {
583 __variant::__raw_idx_visit(
584 [this](auto&& __rhs_mem, auto __rhs_index) mutable
585 {
586 constexpr size_t __j = __rhs_index;
587 if constexpr (__j != variant_npos)
589 in_place_index<__j>, __rhs_mem);
590 }, __variant_cast<_Types...>(__rhs));
591 this->_M_index = __rhs._M_index;
592 }
593
594 _Copy_ctor_base(_Copy_ctor_base&&) = default;
595 _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
596 _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
597 };
598
599 template<typename... _Types>
600 struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
601 {
602 using _Base = _Variant_storage_alias<_Types...>;
603 using _Base::_Base;
604 };
605
606 template<typename... _Types>
607 using _Copy_ctor_alias =
608 _Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
609
610 template<bool, typename... _Types>
611 struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
612 {
613 using _Base = _Copy_ctor_alias<_Types...>;
614 using _Base::_Base;
615
616 _GLIBCXX20_CONSTEXPR
617 _Move_ctor_base(_Move_ctor_base&& __rhs)
618 noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
619 {
620 __variant::__raw_idx_visit(
621 [this](auto&& __rhs_mem, auto __rhs_index) mutable
622 {
623 constexpr size_t __j = __rhs_index;
624 if constexpr (__j != variant_npos)
626 in_place_index<__j>,
627 std::forward<decltype(__rhs_mem)>(__rhs_mem));
628 }, __variant_cast<_Types...>(std::move(__rhs)));
629 this->_M_index = __rhs._M_index;
630 }
631
632 _Move_ctor_base(const _Move_ctor_base&) = default;
633 _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
634 _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
635 };
636
637 template<typename... _Types>
638 struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
639 {
640 using _Base = _Copy_ctor_alias<_Types...>;
641 using _Base::_Base;
642 };
643
644 template<typename... _Types>
645 using _Move_ctor_alias =
646 _Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
647
648 template<bool, typename... _Types>
649 struct _Copy_assign_base : _Move_ctor_alias<_Types...>
650 {
651 using _Base = _Move_ctor_alias<_Types...>;
652 using _Base::_Base;
653
654 _GLIBCXX20_CONSTEXPR
655 _Copy_assign_base&
656 operator=(const _Copy_assign_base& __rhs)
657 noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
658 {
659 __variant::__raw_idx_visit(
660 [this](auto&& __rhs_mem, auto __rhs_index) mutable
661 {
662 constexpr size_t __j = __rhs_index;
663 if constexpr (__j == variant_npos)
664 this->_M_reset(); // Make *this valueless.
665 else if (this->_M_index == __j)
666 __variant::__get<__j>(*this) = __rhs_mem;
667 else
668 {
669 using _Tj = typename _Nth_type<__j, _Types...>::type;
670 if constexpr (is_nothrow_copy_constructible_v<_Tj>
671 || !is_nothrow_move_constructible_v<_Tj>)
672 __variant::__emplace<__j>(*this, __rhs_mem);
673 else
674 {
675 using _Variant = variant<_Types...>;
676 _Variant& __self = __variant_cast<_Types...>(*this);
677 __self = _Variant(in_place_index<__j>, __rhs_mem);
678 }
679 }
680 }, __variant_cast<_Types...>(__rhs));
681 return *this;
682 }
683
684 _Copy_assign_base(const _Copy_assign_base&) = default;
685 _Copy_assign_base(_Copy_assign_base&&) = default;
686 _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
687 };
688
689 template<typename... _Types>
690 struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
691 {
692 using _Base = _Move_ctor_alias<_Types...>;
693 using _Base::_Base;
694 };
695
696 template<typename... _Types>
697 using _Copy_assign_alias =
698 _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
699
700 template<bool, typename... _Types>
701 struct _Move_assign_base : _Copy_assign_alias<_Types...>
702 {
703 using _Base = _Copy_assign_alias<_Types...>;
704 using _Base::_Base;
705
706 _GLIBCXX20_CONSTEXPR
707 _Move_assign_base&
708 operator=(_Move_assign_base&& __rhs)
709 noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
710 {
711 __variant::__raw_idx_visit(
712 [this](auto&& __rhs_mem, auto __rhs_index) mutable
713 {
714 constexpr size_t __j = __rhs_index;
715 if constexpr (__j != variant_npos)
716 {
717 if (this->_M_index == __j)
718 __variant::__get<__j>(*this) = std::move(__rhs_mem);
719 else
720 {
721 using _Tj = typename _Nth_type<__j, _Types...>::type;
722 if constexpr (is_nothrow_move_constructible_v<_Tj>)
723 __variant::__emplace<__j>(*this, std::move(__rhs_mem));
724 else
725 {
726 using _Variant = variant<_Types...>;
727 _Variant& __self = __variant_cast<_Types...>(*this);
728 __self.template emplace<__j>(std::move(__rhs_mem));
729 }
730 }
731 }
732 else
733 this->_M_reset();
734 }, __variant_cast<_Types...>(__rhs));
735 return *this;
736 }
737
738 _Move_assign_base(const _Move_assign_base&) = default;
739 _Move_assign_base(_Move_assign_base&&) = default;
740 _Move_assign_base& operator=(const _Move_assign_base&) = default;
741 };
742
743 template<typename... _Types>
744 struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
745 {
746 using _Base = _Copy_assign_alias<_Types...>;
747 using _Base::_Base;
748 };
749
750 template<typename... _Types>
751 using _Move_assign_alias =
752 _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
753
754 template<typename... _Types>
755 struct _Variant_base : _Move_assign_alias<_Types...>
756 {
757 using _Base = _Move_assign_alias<_Types...>;
758
759 constexpr
760 _Variant_base() noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
761 : _Variant_base(in_place_index<0>) { }
762
763 template<size_t _Np, typename... _Args>
764 constexpr explicit
765 _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
766 : _Base(__i, std::forward<_Args>(__args)...)
767 { }
768
769 _Variant_base(const _Variant_base&) = default;
770 _Variant_base(_Variant_base&&) = default;
771 _Variant_base& operator=(const _Variant_base&) = default;
772 _Variant_base& operator=(_Variant_base&&) = default;
773 };
774
775 template<typename _Tp, typename... _Types>
776 inline constexpr bool __exactly_once
777 = std::__find_uniq_type_in_pack<_Tp, _Types...>() < sizeof...(_Types);
778
779 // Helper used to check for valid conversions that don't involve narrowing.
780 template<typename _Ti> struct _Arr { _Ti _M_x[1]; };
781
782 // "Build an imaginary function FUN(Ti) for each alternative type Ti"
783 template<size_t _Ind, typename _Tp, typename _Ti, typename = void>
784 struct _Build_FUN
785 {
786 // This function means 'using _Build_FUN<I, T, Ti>::_S_fun;' is valid,
787 // but only static functions will be considered in the call below.
788 void _S_fun() = delete;
789 };
790
791 // "... for which Ti x[] = {std::forward<T>(t)}; is well-formed."
792 template<size_t _Ind, typename _Tp, typename _Ti>
793 struct _Build_FUN<_Ind, _Tp, _Ti,
794 void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
795 {
796 // This is the FUN function for type _Ti, with index _Ind
797 static integral_constant<size_t, _Ind> _S_fun(_Ti);
798 };
799
800 template<typename _Tp, typename _Variant,
801 typename = make_index_sequence<variant_size_v<_Variant>>>
802 struct _Build_FUNs;
803
804 template<typename _Tp, typename... _Ti, size_t... _Ind>
805 struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
806 : _Build_FUN<_Ind, _Tp, _Ti>...
807 {
808 using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
809 };
810
811 // The index j of the overload FUN(Tj) selected by overload resolution
812 // for FUN(std::forward<_Tp>(t))
813 template<typename _Tp, typename _Variant>
814 using _FUN_type
815 = decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
816
817 // The index selected for FUN(std::forward<T>(t)), or variant_npos if none.
818 template<typename _Tp, typename _Variant, typename = void>
819 inline constexpr size_t
820 __accepted_index = variant_npos;
821
822 template<typename _Tp, typename _Variant>
823 inline constexpr size_t
824 __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
825 = _FUN_type<_Tp, _Variant>::value;
826
827 template<typename _Maybe_variant_cookie, typename _Variant,
828 typename = __remove_cvref_t<_Variant>>
829 inline constexpr bool
830 __extra_visit_slot_needed = false;
831
832 template<typename _Var, typename... _Types>
833 inline constexpr bool
834 __extra_visit_slot_needed<__variant_cookie, _Var, variant<_Types...>>
835 = !__variant::__never_valueless<_Types...>();
836
837 template<typename _Var, typename... _Types>
838 inline constexpr bool
839 __extra_visit_slot_needed<__variant_idx_cookie, _Var, variant<_Types...>>
840 = !__variant::__never_valueless<_Types...>();
841
842 // Used for storing a multi-dimensional vtable.
843 template<typename _Tp, size_t... _Dimensions>
844 struct _Multi_array;
845
846 // Partial specialization with rank zero, stores a single _Tp element.
847 template<typename _Tp>
848 struct _Multi_array<_Tp>
849 {
850 template<typename>
851 struct __untag_result
852 : false_type
853 { using element_type = _Tp; };
854
855#pragma GCC diagnostic push
856#pragma GCC diagnostic ignored "-Wignored-qualifiers"
857 template <typename... _Args>
858 struct __untag_result<const void(*)(_Args...)>
859 : false_type
860 { using element_type = void(*)(_Args...); };
861#pragma GCC diagnostic pop
862
863 template <typename... _Args>
864 struct __untag_result<__variant_cookie(*)(_Args...)>
865 : false_type
866 { using element_type = void(*)(_Args...); };
867
868 template <typename... _Args>
869 struct __untag_result<__variant_idx_cookie(*)(_Args...)>
870 : false_type
871 { using element_type = void(*)(_Args...); };
872
873 template <typename _Res, typename... _Args>
874 struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
875 : true_type
876 { using element_type = _Res(*)(_Args...); };
877
878 using __result_is_deduced = __untag_result<_Tp>;
879
880 constexpr const typename __untag_result<_Tp>::element_type&
881 _M_access() const
882 { return _M_data; }
883
884 typename __untag_result<_Tp>::element_type _M_data;
885 };
886
887 // Partial specialization with rank >= 1.
888 template<typename _Ret,
889 typename _Visitor,
890 typename... _Variants,
891 size_t __first, size_t... __rest>
892 struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
893 {
894 static constexpr size_t __index =
895 sizeof...(_Variants) - sizeof...(__rest) - 1;
896
897 using _Variant = typename _Nth_type<__index, _Variants...>::type;
898
899 static constexpr int __do_cookie =
900 __extra_visit_slot_needed<_Ret, _Variant> ? 1 : 0;
901
902 using _Tp = _Ret(*)(_Visitor, _Variants...);
903
904 template<typename... _Args>
905 constexpr decltype(auto)
906 _M_access(size_t __first_index, _Args... __rest_indices) const
907 {
908 return _M_arr[__first_index + __do_cookie]
909 ._M_access(__rest_indices...);
910 }
911
912 _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
913 };
914
915 // Creates a multi-dimensional vtable recursively.
916 //
917 // For example,
918 // visit([](auto, auto){},
919 // variant<int, char>(), // typedef'ed as V1
920 // variant<float, double, long double>()) // typedef'ed as V2
921 // will trigger instantiations of:
922 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
923 // tuple<V1&&, V2&&>, std::index_sequence<>>
924 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
925 // tuple<V1&&, V2&&>, std::index_sequence<0>>
926 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
927 // tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
928 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
929 // tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
930 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
931 // tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
932 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
933 // tuple<V1&&, V2&&>, std::index_sequence<1>>
934 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
935 // tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
936 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
937 // tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
938 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
939 // tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
940 // The returned multi-dimensional vtable can be fast accessed by the visitor
941 // using index calculation.
942 template<typename _Array_type, typename _Index_seq>
943 struct __gen_vtable_impl;
944
945 // Defines the _S_apply() member that returns a _Multi_array populated
946 // with function pointers that perform the visitation expressions e(m)
947 // for each valid pack of indexes into the variant types _Variants.
948 //
949 // This partial specialization builds up the index sequences by recursively
950 // calling _S_apply() on the next specialization of __gen_vtable_impl.
951 // The base case of the recursion defines the actual function pointers.
952 template<typename _Result_type, typename _Visitor, size_t... __dimensions,
953 typename... _Variants, size_t... __indices>
954 struct __gen_vtable_impl<
955 _Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
956 std::index_sequence<__indices...>>
957 {
958 using _Next =
959 remove_reference_t<typename _Nth_type<sizeof...(__indices),
960 _Variants...>::type>;
961 using _Array_type =
962 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
963 __dimensions...>;
964
965 static constexpr _Array_type
966 _S_apply()
967 {
968 _Array_type __vtable{};
969 _S_apply_all_alts(
970 __vtable, make_index_sequence<variant_size_v<_Next>>());
971 return __vtable;
972 }
973
974 template<size_t... __var_indices>
975 static constexpr void
976 _S_apply_all_alts(_Array_type& __vtable,
978 {
979 if constexpr (__extra_visit_slot_needed<_Result_type, _Next>)
980 (_S_apply_single_alt<true, __var_indices>(
981 __vtable._M_arr[__var_indices + 1],
982 &(__vtable._M_arr[0])), ...);
983 else
984 (_S_apply_single_alt<false, __var_indices>(
985 __vtable._M_arr[__var_indices]), ...);
986 }
987
988 template<bool __do_cookie, size_t __index, typename _Tp>
989 static constexpr void
990 _S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
991 {
992 if constexpr (__do_cookie)
993 {
994 __element = __gen_vtable_impl<
995 _Tp,
996 std::index_sequence<__indices..., __index>>::_S_apply();
997 *__cookie_element = __gen_vtable_impl<
998 _Tp,
999 std::index_sequence<__indices..., variant_npos>>::_S_apply();
1000 }
1001 else
1002 {
1003 auto __tmp_element = __gen_vtable_impl<
1004 remove_reference_t<decltype(__element)>,
1006 static_assert(is_same_v<_Tp, decltype(__tmp_element)>,
1007 "std::visit requires the visitor to have the same "
1008 "return type for all alternatives of a variant");
1009 __element = __tmp_element;
1010 }
1011 }
1012 };
1013
1014 // This partial specialization is the base case for the recursion.
1015 // It populates a _Multi_array element with the address of a function
1016 // that invokes the visitor with the alternatives specified by __indices.
1017 template<typename _Result_type, typename _Visitor, typename... _Variants,
1018 size_t... __indices>
1019 struct __gen_vtable_impl<
1020 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
1021 std::index_sequence<__indices...>>
1022 {
1023 using _Array_type =
1024 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
1025
1026 template<size_t __index, typename _Variant>
1027 static constexpr decltype(auto)
1028 __element_by_index_or_cookie(_Variant&& __var) noexcept
1029 {
1030 if constexpr (__index != variant_npos)
1031 return __variant::__get<__index>(std::forward<_Variant>(__var));
1032 else
1033 return __variant_cookie{};
1034 }
1035
1036 static constexpr decltype(auto)
1037 __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
1038 {
1039 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
1040 // For raw visitation using indices, pass the indices to the visitor
1041 // and discard the return value:
1042 std::__invoke(std::forward<_Visitor>(__visitor),
1043 __element_by_index_or_cookie<__indices>(
1044 std::forward<_Variants>(__vars))...,
1045 integral_constant<size_t, __indices>()...);
1046 else if constexpr (is_same_v<_Result_type, __variant_cookie>)
1047 // For raw visitation without indices, and discard the return value:
1048 std::__invoke(std::forward<_Visitor>(__visitor),
1049 __element_by_index_or_cookie<__indices>(
1050 std::forward<_Variants>(__vars))...);
1051 else if constexpr (_Array_type::__result_is_deduced::value)
1052 // For the usual std::visit case deduce the return value:
1053 return std::__invoke(std::forward<_Visitor>(__visitor),
1054 __element_by_index_or_cookie<__indices>(
1055 std::forward<_Variants>(__vars))...);
1056 else // for std::visit<R> use INVOKE<R>
1057 return std::__invoke_r<_Result_type>(
1058 std::forward<_Visitor>(__visitor),
1059 __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
1060 }
1061
1062 static constexpr auto
1063 _S_apply()
1064 {
1065 if constexpr (_Array_type::__result_is_deduced::value)
1066 {
1067 constexpr bool __visit_ret_type_mismatch =
1068 !is_same_v<typename _Result_type::type,
1069 decltype(__visit_invoke(std::declval<_Visitor>(),
1070 std::declval<_Variants>()...))>;
1071 if constexpr (__visit_ret_type_mismatch)
1072 {
1073 struct __cannot_match {};
1074 return __cannot_match{};
1075 }
1076 else
1077 return _Array_type{&__visit_invoke};
1078 }
1079 else
1080 return _Array_type{&__visit_invoke};
1081 }
1082 };
1083
1084 template<typename _Result_type, typename _Visitor, typename... _Variants>
1085 struct __gen_vtable
1086 {
1087 using _Array_type =
1088 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
1089 variant_size_v<remove_reference_t<_Variants>>...>;
1090
1091 static constexpr _Array_type _S_vtable
1092 = __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
1093 };
1094
1095 template<size_t _Np, typename _Tp>
1096 struct _Base_dedup : public _Tp { };
1097
1098 template<typename _Variant, typename __indices>
1099 struct _Variant_hash_base;
1100
1101 template<typename... _Types, size_t... __indices>
1102 struct _Variant_hash_base<variant<_Types...>,
1103 std::index_sequence<__indices...>>
1104 : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
1105
1106 // Equivalent to decltype(get<_Np>(as-variant(declval<_Variant>())))
1107 template<size_t _Np, typename _Variant,
1108 typename _AsV = decltype(__variant::__as(std::declval<_Variant>())),
1109 typename _Tp = variant_alternative_t<_Np, remove_reference_t<_AsV>>>
1110 using __get_t
1111 = __conditional_t<is_lvalue_reference_v<_Variant>, _Tp&, _Tp&&>;
1112
1113 // Return type of std::visit.
1114 template<typename _Visitor, typename... _Variants>
1115 using __visit_result_t
1116 = invoke_result_t<_Visitor, __get_t<0, _Variants>...>;
1117
1118 template<typename _Tp, typename... _Types>
1119 constexpr inline bool __same_types = (is_same_v<_Tp, _Types> && ...);
1120
1121 template <typename _Visitor, typename _Variant, size_t... _Idxs>
1122 constexpr bool __check_visitor_results(std::index_sequence<_Idxs...>)
1123 {
1124 return __same_types<
1125 invoke_result_t<_Visitor, __get_t<_Idxs, _Variant>>...
1126 >;
1127 }
1128
1129} // namespace __variant
1130} // namespace __detail
1131
1132 template<typename _Tp, typename... _Types>
1133 constexpr bool
1134 holds_alternative(const variant<_Types...>& __v) noexcept
1135 {
1136 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1137 "T must occur exactly once in alternatives");
1138 return __v.index() == std::__find_uniq_type_in_pack<_Tp, _Types...>();
1139 }
1140
1141 template<typename _Tp, typename... _Types>
1142 constexpr _Tp&
1143 get(variant<_Types...>& __v)
1144 {
1145 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1146 "T must occur exactly once in alternatives");
1147 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1148 return std::get<__n>(__v);
1149 }
1150
1151 template<typename _Tp, typename... _Types>
1152 constexpr _Tp&&
1153 get(variant<_Types...>&& __v)
1154 {
1155 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1156 "T must occur exactly once in alternatives");
1157 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1158 return std::get<__n>(std::move(__v));
1159 }
1160
1161 template<typename _Tp, typename... _Types>
1162 constexpr const _Tp&
1163 get(const variant<_Types...>& __v)
1164 {
1165 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1166 "T must occur exactly once in alternatives");
1167 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1168 return std::get<__n>(__v);
1169 }
1170
1171 template<typename _Tp, typename... _Types>
1172 constexpr const _Tp&&
1173 get(const variant<_Types...>&& __v)
1174 {
1175 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1176 "T must occur exactly once in alternatives");
1177 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1178 return std::get<__n>(std::move(__v));
1179 }
1180
1181 template<size_t _Np, typename... _Types>
1182 constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
1183 get_if(variant<_Types...>* __ptr) noexcept
1184 {
1185 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1186 static_assert(_Np < sizeof...(_Types),
1187 "The index must be in [0, number of alternatives)");
1188 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1189 if (__ptr && __ptr->index() == _Np)
1190 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1191 return nullptr;
1192 }
1193
1194 template<size_t _Np, typename... _Types>
1195 constexpr
1196 add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
1197 get_if(const variant<_Types...>* __ptr) noexcept
1198 {
1199 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1200 static_assert(_Np < sizeof...(_Types),
1201 "The index must be in [0, number of alternatives)");
1202 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1203 if (__ptr && __ptr->index() == _Np)
1204 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1205 return nullptr;
1206 }
1207
1208 template<typename _Tp, typename... _Types>
1209 constexpr add_pointer_t<_Tp>
1210 get_if(variant<_Types...>* __ptr) noexcept
1211 {
1212 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1213 "T must occur exactly once in alternatives");
1214 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1215 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1216 return std::get_if<__n>(__ptr);
1217 }
1218
1219 template<typename _Tp, typename... _Types>
1220 constexpr add_pointer_t<const _Tp>
1221 get_if(const variant<_Types...>* __ptr) noexcept
1222 {
1223 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1224 "T must occur exactly once in alternatives");
1225 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1226 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1227 return std::get_if<__n>(__ptr);
1228 }
1229
1230 struct monostate { };
1231
1232namespace __detail::__variant
1233{
1234 template<typename _Ret, typename _Vp, typename _Op>
1235 constexpr _Ret
1236 __compare(_Ret __ret, const _Vp& __lhs, const _Vp& __rhs, _Op __op)
1237 {
1238 __variant::__raw_idx_visit(
1239 [&__ret, &__lhs, __op] (auto&& __rhs_mem, auto __rhs_index) mutable
1240 {
1241 if constexpr (__rhs_index != variant_npos)
1242 {
1243 if (__lhs.index() == __rhs_index.value)
1244 {
1245 auto& __this_mem = std::get<__rhs_index>(__lhs);
1246 __ret = __op(__this_mem, __rhs_mem);
1247 return;
1248 }
1249 }
1250 __ret = __op(__lhs.index() + 1, __rhs_index + 1);
1251 }, __rhs);
1252 return __ret;
1253 }
1254} // namespace __detail::__variant
1255
1256 template<typename... _Types>
1257#if __cpp_lib_concepts
1258 requires ((requires (const _Types& __t) {
1259 { __t == __t } -> convertible_to<bool>; }) && ...)
1260#endif
1261 constexpr bool
1262 operator== [[nodiscard]] (const variant<_Types...>& __lhs,
1263 const variant<_Types...>& __rhs)
1264 {
1265 namespace __variant = __detail::__variant;
1266 return __variant::__compare(true, __lhs, __rhs,
1267 [](auto&& __l, auto&& __r) -> bool {
1268 return __l == __r;
1269 });
1270 }
1271
1272 template<typename... _Types>
1273#if __cpp_lib_concepts
1274 requires ((requires (const _Types& __t) {
1275 { __t != __t } -> convertible_to<bool>; }) && ...)
1276#endif
1277 constexpr bool
1278 operator!= [[nodiscard]] (const variant<_Types...>& __lhs,
1279 const variant<_Types...>& __rhs)
1280 {
1281 namespace __variant = __detail::__variant;
1282 return __variant::__compare(true, __lhs, __rhs,
1283 [](auto&& __l, auto&& __r) -> bool {
1284 return __l != __r;
1285 });
1286 }
1287
1288 template<typename... _Types>
1289#if __cpp_lib_concepts
1290 requires ((requires (const _Types& __t) {
1291 { __t < __t } -> convertible_to<bool>; }) && ...)
1292#endif
1293 constexpr bool
1294 operator< [[nodiscard]] (const variant<_Types...>& __lhs,
1295 const variant<_Types...>& __rhs)
1296 {
1297 namespace __variant = __detail::__variant;
1298 return __variant::__compare(true, __lhs, __rhs,
1299 [](auto&& __l, auto&& __r) -> bool {
1300 return __l < __r;
1301 });
1302 }
1303
1304 template<typename... _Types>
1305#if __cpp_lib_concepts
1306 requires ((requires (const _Types& __t) {
1307 { __t <= __t } -> convertible_to<bool>; }) && ...)
1308#endif
1309 constexpr bool
1310 operator<= [[nodiscard]] (const variant<_Types...>& __lhs,
1311 const variant<_Types...>& __rhs)
1312 {
1313 namespace __variant = __detail::__variant;
1314 return __variant::__compare(true, __lhs, __rhs,
1315 [](auto&& __l, auto&& __r) -> bool {
1316 return __l <= __r;
1317 });
1318 }
1319
1320 template<typename... _Types>
1321#if __cpp_lib_concepts
1322 requires ((requires (const _Types& __t) {
1323 { __t > __t } -> convertible_to<bool>; }) && ...)
1324#endif
1325 constexpr bool
1326 operator> [[nodiscard]] (const variant<_Types...>& __lhs,
1327 const variant<_Types...>& __rhs)
1328 {
1329 namespace __variant = __detail::__variant;
1330 return __variant::__compare(true, __lhs, __rhs,
1331 [](auto&& __l, auto&& __r) -> bool {
1332 return __l > __r;
1333 });
1334 }
1335
1336 template<typename... _Types>
1337#if __cpp_lib_concepts
1338 requires ((requires (const _Types& __t) {
1339 { __t >= __t } -> convertible_to<bool>; }) && ...)
1340#endif
1341 constexpr bool
1342 operator>= [[nodiscard]] (const variant<_Types...>& __lhs,
1343 const variant<_Types...>& __rhs)
1344 {
1345 namespace __variant = __detail::__variant;
1346 return __variant::__compare(true, __lhs, __rhs,
1347 [](auto&& __l, auto&& __r) -> bool {
1348 return __l >= __r;
1349 });
1350 }
1351
1352 constexpr bool operator==(monostate, monostate) noexcept { return true; }
1353
1354#ifdef __cpp_lib_three_way_comparison
1355 template<typename... _Types>
1356 requires (three_way_comparable<_Types> && ...)
1357 constexpr
1358 common_comparison_category_t<compare_three_way_result_t<_Types>...>
1359 operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
1360 {
1361 common_comparison_category_t<compare_three_way_result_t<_Types>...> __ret
1362 = strong_ordering::equal;
1363 namespace __variant = __detail::__variant;
1364 return __variant::__compare(__ret, __v, __w,
1365 [](auto&& __l, auto&& __r) {
1366 return __l <=> __r;
1367 });
1368 }
1369
1370 constexpr strong_ordering
1371 operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
1372#else
1373 constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1374 constexpr bool operator<(monostate, monostate) noexcept { return false; }
1375 constexpr bool operator>(monostate, monostate) noexcept { return false; }
1376 constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1377 constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1378#endif
1379
1380 template<typename _Visitor, typename... _Variants>
1381 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1382 visit(_Visitor&&, _Variants&&...);
1383
1384#if __cplusplus > 201703L
1385 template<typename _Res, typename _Visitor, typename... _Variants>
1386 constexpr _Res
1387 visit(_Visitor&&, _Variants&&...);
1388#endif
1389
1390 template<typename... _Types>
1391 _GLIBCXX20_CONSTEXPR
1392 inline enable_if_t<(is_move_constructible_v<_Types> && ...)
1393 && (is_swappable_v<_Types> && ...)>
1394 swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1395 noexcept(noexcept(__lhs.swap(__rhs)))
1396 { __lhs.swap(__rhs); }
1397
1398 template<typename... _Types>
1399 enable_if_t<!((is_move_constructible_v<_Types> && ...)
1400 && (is_swappable_v<_Types> && ...))>
1401 swap(variant<_Types...>&, variant<_Types...>&) = delete;
1402
1403 class bad_variant_access : public exception
1404 {
1405 public:
1406 bad_variant_access() noexcept { }
1407
1408 const char* what() const noexcept override
1409 { return _M_reason; }
1410
1411 private:
1412 bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
1413
1414 // Must point to a string with static storage duration:
1415 const char* _M_reason = "bad variant access";
1416
1417 friend void __throw_bad_variant_access(const char* __what);
1418 };
1419
1420 // Must only be called with a string literal
1421 inline void
1422 __throw_bad_variant_access(const char* __what)
1423 { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
1424
1425 inline void
1426 __throw_bad_variant_access(bool __valueless)
1427 {
1428 if (__valueless) [[__unlikely__]]
1429 __throw_bad_variant_access("std::get: variant is valueless");
1430 else
1431 __throw_bad_variant_access("std::get: wrong index for variant");
1432 }
1433
1434 template<typename... _Types>
1435 class variant
1436 : private __detail::__variant::_Variant_base<_Types...>,
1437 private _Enable_copy_move<
1438 __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1439 __detail::__variant::_Traits<_Types...>::_S_copy_assign,
1440 __detail::__variant::_Traits<_Types...>::_S_move_ctor,
1441 __detail::__variant::_Traits<_Types...>::_S_move_assign,
1442 variant<_Types...>>
1443 {
1444 private:
1445 template <typename... _UTypes, typename _Tp>
1446 friend _GLIBCXX20_CONSTEXPR decltype(auto)
1447 __variant_cast(_Tp&&);
1448
1449 static_assert(sizeof...(_Types) > 0,
1450 "variant must have at least one alternative");
1451 static_assert(((std::is_object_v<_Types> && !is_array_v<_Types>) && ...),
1452 "variant alternatives must be non-array object types");
1453
1454 using _Base = __detail::__variant::_Variant_base<_Types...>;
1455
1456 template<typename _Tp>
1457 static constexpr bool __not_self
1458 = !is_same_v<__remove_cvref_t<_Tp>, variant>;
1459
1460 template<typename _Tp>
1461 static constexpr bool
1462 __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1463
1464 template<typename _Tp>
1465 static constexpr size_t __accepted_index
1466 = __detail::__variant::__accepted_index<_Tp, variant>;
1467
1468 template<size_t _Np, typename = enable_if_t<(_Np < sizeof...(_Types))>>
1469 using __to_type = typename _Nth_type<_Np, _Types...>::type;
1470
1471 template<typename _Tp, typename = enable_if_t<__not_self<_Tp>>>
1472 using __accepted_type = __to_type<__accepted_index<_Tp>>;
1473
1474 template<typename _Tp>
1475 static constexpr size_t __index_of
1476 = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1477
1478 using _Traits = __detail::__variant::_Traits<_Types...>;
1479
1480 template<typename _Tp>
1481 static constexpr bool __not_in_place_tag
1482 = !__is_in_place_type_v<__remove_cvref_t<_Tp>>
1483 && !__is_in_place_index_v<__remove_cvref_t<_Tp>>;
1484
1485 public:
1486#if __cpp_concepts
1487 variant() requires is_default_constructible_v<__to_type<0>> = default;
1488#else
1489 template<typename _Tp0 = __to_type<0>,
1490 typename = enable_if_t<is_default_constructible_v<_Tp0>>>
1491 constexpr
1492 variant() noexcept(is_nothrow_default_constructible_v<__to_type<0>>)
1493 { }
1494#endif
1495
1496 variant(const variant& __rhs) = default;
1497 variant(variant&&) = default;
1498 variant& operator=(const variant&) = default;
1499 variant& operator=(variant&&) = default;
1500 _GLIBCXX20_CONSTEXPR ~variant() = default;
1501
1502 template<typename _Tp,
1503 typename = enable_if_t<sizeof...(_Types) != 0>,
1504 typename = enable_if_t<__not_in_place_tag<_Tp>>,
1505 typename _Tj = __accepted_type<_Tp&&>,
1506 typename = enable_if_t<__exactly_once<_Tj>
1507 && is_constructible_v<_Tj, _Tp>>>
1508 constexpr
1509 variant(_Tp&& __t)
1510 noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
1511 : variant(in_place_index<__accepted_index<_Tp>>,
1512 std::forward<_Tp>(__t))
1513 { }
1514
1515 template<typename _Tp, typename... _Args,
1516 typename = enable_if_t<__exactly_once<_Tp>
1517 && is_constructible_v<_Tp, _Args...>>>
1518 constexpr explicit
1519 variant(in_place_type_t<_Tp>, _Args&&... __args)
1520 : variant(in_place_index<__index_of<_Tp>>,
1521 std::forward<_Args>(__args)...)
1522 { }
1523
1524 template<typename _Tp, typename _Up, typename... _Args,
1525 typename = enable_if_t<__exactly_once<_Tp>
1526 && is_constructible_v<_Tp,
1527 initializer_list<_Up>&, _Args...>>>
1528 constexpr explicit
1529 variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1530 _Args&&... __args)
1531 : variant(in_place_index<__index_of<_Tp>>, __il,
1532 std::forward<_Args>(__args)...)
1533 { }
1534
1535 template<size_t _Np, typename... _Args,
1536 typename _Tp = __to_type<_Np>,
1537 typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
1538 constexpr explicit
1539 variant(in_place_index_t<_Np>, _Args&&... __args)
1540 : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...)
1541 { }
1542
1543 template<size_t _Np, typename _Up, typename... _Args,
1544 typename _Tp = __to_type<_Np>,
1545 typename = enable_if_t<is_constructible_v<_Tp,
1546 initializer_list<_Up>&,
1547 _Args...>>>
1548 constexpr explicit
1549 variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1550 _Args&&... __args)
1551 : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...)
1552 { }
1553
1554 template<typename _Tp>
1555 _GLIBCXX20_CONSTEXPR
1556 enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1557 && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
1558 && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
1559 variant&>
1560 operator=(_Tp&& __rhs)
1561 noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
1562 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
1563 {
1564 constexpr auto __index = __accepted_index<_Tp>;
1565 if (index() == __index)
1566 std::get<__index>(*this) = std::forward<_Tp>(__rhs);
1567 else
1568 {
1569 using _Tj = __accepted_type<_Tp&&>;
1570 if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
1571 || !is_nothrow_move_constructible_v<_Tj>)
1572 this->emplace<__index>(std::forward<_Tp>(__rhs));
1573 else
1574 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1575 // 3585. converting assignment with immovable alternative
1576 this->emplace<__index>(_Tj(std::forward<_Tp>(__rhs)));
1577 }
1578 return *this;
1579 }
1580
1581 template<typename _Tp, typename... _Args>
1582 _GLIBCXX20_CONSTEXPR
1583 enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1584 _Tp&>
1585 emplace(_Args&&... __args)
1586 {
1587 constexpr size_t __index = __index_of<_Tp>;
1588 return this->emplace<__index>(std::forward<_Args>(__args)...);
1589 }
1590
1591 template<typename _Tp, typename _Up, typename... _Args>
1592 _GLIBCXX20_CONSTEXPR
1593 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1594 && __exactly_once<_Tp>,
1595 _Tp&>
1596 emplace(initializer_list<_Up> __il, _Args&&... __args)
1597 {
1598 constexpr size_t __index = __index_of<_Tp>;
1599 return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
1600 }
1601
1602 template<size_t _Np, typename... _Args>
1603 _GLIBCXX20_CONSTEXPR
1604 enable_if_t<is_constructible_v<__to_type<_Np>, _Args...>,
1605 __to_type<_Np>&>
1606 emplace(_Args&&... __args)
1607 {
1608 namespace __variant = std::__detail::__variant;
1609 using type = typename _Nth_type<_Np, _Types...>::type;
1610 // Provide the strong exception-safety guarantee when possible,
1611 // to avoid becoming valueless.
1612 if constexpr (is_nothrow_constructible_v<type, _Args...>)
1613 {
1614 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1615 }
1616 else if constexpr (is_scalar_v<type>)
1617 {
1618 // This might invoke a potentially-throwing conversion operator:
1619 const type __tmp(std::forward<_Args>(__args)...);
1620 // But this won't throw:
1621 __variant::__emplace<_Np>(*this, __tmp);
1622 }
1623 else if constexpr (__variant::_Never_valueless_alt<type>()
1624 && _Traits::_S_move_assign)
1625 {
1626 // This construction might throw:
1627 variant __tmp(in_place_index<_Np>,
1628 std::forward<_Args>(__args)...);
1629 // But _Never_valueless_alt<type> means this won't:
1630 *this = std::move(__tmp);
1631 }
1632 else
1633 {
1634 // This case only provides the basic exception-safety guarantee,
1635 // i.e. the variant can become valueless.
1636 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1637 }
1638 return std::get<_Np>(*this);
1639 }
1640
1641 template<size_t _Np, typename _Up, typename... _Args>
1642 _GLIBCXX20_CONSTEXPR
1643 enable_if_t<is_constructible_v<__to_type<_Np>,
1644 initializer_list<_Up>&, _Args...>,
1645 __to_type<_Np>&>
1646 emplace(initializer_list<_Up> __il, _Args&&... __args)
1647 {
1648 namespace __variant = std::__detail::__variant;
1649 using type = typename _Nth_type<_Np, _Types...>::type;
1650 // Provide the strong exception-safety guarantee when possible,
1651 // to avoid becoming valueless.
1652 if constexpr (is_nothrow_constructible_v<type,
1653 initializer_list<_Up>&,
1654 _Args...>)
1655 {
1656 __variant::__emplace<_Np>(*this, __il,
1657 std::forward<_Args>(__args)...);
1658 }
1659 else if constexpr (__variant::_Never_valueless_alt<type>()
1660 && _Traits::_S_move_assign)
1661 {
1662 // This construction might throw:
1663 variant __tmp(in_place_index<_Np>, __il,
1664 std::forward<_Args>(__args)...);
1665 // But _Never_valueless_alt<type> means this won't:
1666 *this = std::move(__tmp);
1667 }
1668 else
1669 {
1670 // This case only provides the basic exception-safety guarantee,
1671 // i.e. the variant can become valueless.
1672 __variant::__emplace<_Np>(*this, __il,
1673 std::forward<_Args>(__args)...);
1674 }
1675 return std::get<_Np>(*this);
1676 }
1677
1678 template<size_t _Np, typename... _Args>
1679 enable_if_t<!(_Np < sizeof...(_Types))> emplace(_Args&&...) = delete;
1680
1681 template<typename _Tp, typename... _Args>
1682 enable_if_t<!__exactly_once<_Tp>> emplace(_Args&&...) = delete;
1683
1684 constexpr bool valueless_by_exception() const noexcept
1685 { return !this->_M_valid(); }
1686
1687 constexpr size_t index() const noexcept
1688 {
1689 using __index_type = typename _Base::__index_type;
1690 if constexpr (__detail::__variant::__never_valueless<_Types...>())
1691 return this->_M_index;
1692 else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
1693 return make_signed_t<__index_type>(this->_M_index);
1694 else
1695 return size_t(__index_type(this->_M_index + 1)) - 1;
1696 }
1697
1698 _GLIBCXX20_CONSTEXPR
1699 void
1700 swap(variant& __rhs)
1701 noexcept((__is_nothrow_swappable<_Types>::value && ...)
1702 && is_nothrow_move_constructible_v<variant>)
1703 {
1704 static_assert((is_move_constructible_v<_Types> && ...));
1705
1706 // Handle this here to simplify the visitation.
1707 if (__rhs.valueless_by_exception()) [[__unlikely__]]
1708 {
1709 if (!this->valueless_by_exception()) [[__likely__]]
1710 __rhs.swap(*this);
1711 return;
1712 }
1713
1714 namespace __variant = __detail::__variant;
1715
1716 __variant::__raw_idx_visit(
1717 [this, &__rhs](auto&& __rhs_mem, auto __rhs_index) mutable
1718 {
1719 constexpr size_t __j = __rhs_index;
1720 if constexpr (__j != variant_npos)
1721 {
1722 if (this->index() == __j)
1723 {
1724 using std::swap;
1725 swap(std::get<__j>(*this), __rhs_mem);
1726 }
1727 else
1728 {
1729 auto __tmp(std::move(__rhs_mem));
1730
1731 if constexpr (_Traits::_S_trivial_move_assign)
1732 __rhs = std::move(*this);
1733 else
1734 __variant::__raw_idx_visit(
1735 [&__rhs](auto&& __this_mem, auto __this_index) mutable
1736 {
1737 constexpr size_t __k = __this_index;
1738 if constexpr (__k != variant_npos)
1739 __variant::__emplace<__k>(__rhs,
1740 std::move(__this_mem));
1741 }, *this);
1742
1743 __variant::__emplace<__j>(*this, std::move(__tmp));
1744 }
1745 }
1746 }, __rhs);
1747 }
1748
1749#if __cpp_lib_variant >= 202306L // >= C++26
1750 // [variant.visit], visitation
1751
1752 /** Simple visitation for a single variant
1753 *
1754 * To visit a single variant you can use `var.visit(visitor)`
1755 * instead of `std::visit(visitor, var)`.
1756 *
1757 * @since C++26
1758 */
1759 template<int = 0, typename _Self, typename _Visitor>
1760 constexpr decltype(auto)
1761 visit(this _Self&& __self, _Visitor&& __vis)
1762 {
1763 using _CVar = __conditional_t<is_const_v<remove_reference_t<_Self>>,
1764 const variant, variant>;
1765 using _Var = __conditional_t<is_rvalue_reference_v<_Self&&>,
1766 _CVar&&, _CVar&>;
1767 return std::visit(std::forward<_Visitor>(__vis), (_Var)__self);
1768 }
1769
1770 /** Simple visitation for a single variant, with explicit return type
1771 *
1772 * To visit a single variant you can use `var.visit<R>(visitor)`
1773 * instead of `std::visit<R>(visitor, var)`.
1774 *
1775 * @since C++26
1776 */
1777 template<typename _Res, typename _Self, typename _Visitor>
1778 constexpr _Res
1779 visit(this _Self&& __self, _Visitor&& __vis)
1780 {
1781 using _CVar = __conditional_t<is_const_v<remove_reference_t<_Self>>,
1782 const variant, variant>;
1783 using _Var = __conditional_t<is_rvalue_reference_v<_Self&&>,
1784 _CVar&&, _CVar&>;
1785 return std::visit<_Res>(std::forward<_Visitor>(__vis), (_Var)__self);
1786 }
1787#endif
1788
1789 private:
1790 template<size_t _Np, typename _Vp>
1791 friend constexpr decltype(auto)
1792 __detail::__variant::__get(_Vp&& __v) noexcept;
1793 };
1794
1795 template<size_t _Np, typename... _Types>
1796 constexpr variant_alternative_t<_Np, variant<_Types...>>&
1797 get(variant<_Types...>& __v)
1798 {
1799 static_assert(_Np < sizeof...(_Types),
1800 "The index must be in [0, number of alternatives)");
1801 if (__v.index() != _Np)
1802 __throw_bad_variant_access(__v.valueless_by_exception());
1803 return __detail::__variant::__get<_Np>(__v);
1804 }
1805
1806 template<size_t _Np, typename... _Types>
1807 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1808 get(variant<_Types...>&& __v)
1809 {
1810 static_assert(_Np < sizeof...(_Types),
1811 "The index must be in [0, number of alternatives)");
1812 if (__v.index() != _Np)
1813 __throw_bad_variant_access(__v.valueless_by_exception());
1814 return __detail::__variant::__get<_Np>(std::move(__v));
1815 }
1816
1817 template<size_t _Np, typename... _Types>
1818 constexpr const variant_alternative_t<_Np, variant<_Types...>>&
1819 get(const variant<_Types...>& __v)
1820 {
1821 static_assert(_Np < sizeof...(_Types),
1822 "The index must be in [0, number of alternatives)");
1823 if (__v.index() != _Np)
1824 __throw_bad_variant_access(__v.valueless_by_exception());
1825 return __detail::__variant::__get<_Np>(__v);
1826 }
1827
1828 template<size_t _Np, typename... _Types>
1829 constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
1830 get(const variant<_Types...>&& __v)
1831 {
1832 static_assert(_Np < sizeof...(_Types),
1833 "The index must be in [0, number of alternatives)");
1834 if (__v.index() != _Np)
1835 __throw_bad_variant_access(__v.valueless_by_exception());
1836 return __detail::__variant::__get<_Np>(std::move(__v));
1837 }
1838
1839 /// @cond undocumented
1840 template<typename _Result_type, typename _Visitor, typename... _Variants>
1841 constexpr decltype(auto)
1842 __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
1843 {
1844 // Get the silly case of visiting no variants out of the way first.
1845 if constexpr (sizeof...(_Variants) == 0)
1846 {
1847 if constexpr (is_void_v<_Result_type>)
1848 return (void) std::forward<_Visitor>(__visitor)();
1849 else
1850 return std::forward<_Visitor>(__visitor)();
1851 }
1852 else
1853 {
1854 constexpr size_t __max = 11; // "These go to eleven."
1855
1856 // The type of the first variant in the pack.
1857 using _V0 = typename _Nth_type<0, _Variants...>::type;
1858 // The number of alternatives in that first variant.
1859 constexpr auto __n = variant_size_v<remove_reference_t<_V0>>;
1860
1861 if constexpr (sizeof...(_Variants) > 1 || __n > __max)
1862 {
1863 // Use a jump table for the general case.
1864 constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1865 _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1866
1867 auto __func_ptr = __vtable._M_access(__variants.index()...);
1868 return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1869 std::forward<_Variants>(__variants)...);
1870 }
1871 else // We have a single variant with a small number of alternatives.
1872 {
1873 // A name for the first variant in the pack.
1874 _V0& __v0
1875 = [](_V0& __v, ...) -> _V0& { return __v; }(__variants...);
1876
1877 using __detail::__variant::_Multi_array;
1878 using __detail::__variant::__gen_vtable_impl;
1879 using _Ma = _Multi_array<_Result_type (*)(_Visitor&&, _V0&&)>;
1880
1881#ifdef _GLIBCXX_DEBUG
1882# define _GLIBCXX_VISIT_UNREACHABLE __builtin_trap
1883#else
1884# define _GLIBCXX_VISIT_UNREACHABLE __builtin_unreachable
1885#endif
1886
1887#define _GLIBCXX_VISIT_CASE(N) \
1888 case N: \
1889 { \
1890 if constexpr (N < __n) \
1891 { \
1892 return __gen_vtable_impl<_Ma, index_sequence<N>>:: \
1893 __visit_invoke(std::forward<_Visitor>(__visitor), \
1894 std::forward<_V0>(__v0)); \
1895 } \
1896 else _GLIBCXX_VISIT_UNREACHABLE(); \
1897 }
1898
1899 switch (__v0.index())
1900 {
1901 _GLIBCXX_VISIT_CASE(0)
1902 _GLIBCXX_VISIT_CASE(1)
1903 _GLIBCXX_VISIT_CASE(2)
1904 _GLIBCXX_VISIT_CASE(3)
1905 _GLIBCXX_VISIT_CASE(4)
1906 _GLIBCXX_VISIT_CASE(5)
1907 _GLIBCXX_VISIT_CASE(6)
1908 _GLIBCXX_VISIT_CASE(7)
1909 _GLIBCXX_VISIT_CASE(8)
1910 _GLIBCXX_VISIT_CASE(9)
1911 _GLIBCXX_VISIT_CASE(10)
1912 case variant_npos:
1913 using __detail::__variant::__variant_idx_cookie;
1914 using __detail::__variant::__variant_cookie;
1915 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>
1916 || is_same_v<_Result_type, __variant_cookie>)
1917 {
1918 using _Npos = index_sequence<variant_npos>;
1919 return __gen_vtable_impl<_Ma, _Npos>::
1920 __visit_invoke(std::forward<_Visitor>(__visitor),
1921 std::forward<_V0>(__v0));
1922 }
1923 else
1924 _GLIBCXX_VISIT_UNREACHABLE();
1925 default:
1926 _GLIBCXX_VISIT_UNREACHABLE();
1927 }
1928#undef _GLIBCXX_VISIT_CASE
1929#undef _GLIBCXX_VISIT_UNREACHABLE
1930 }
1931 }
1932 }
1933 /// @endcond
1934
1935 template<typename _Visitor, typename... _Variants>
1936 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1937 visit(_Visitor&& __visitor, _Variants&&... __variants)
1938 {
1939 namespace __variant = std::__detail::__variant;
1940
1941 if ((__variant::__as(__variants).valueless_by_exception() || ...))
1942 __throw_bad_variant_access("std::visit: variant is valueless");
1943
1944 using _Result_type
1945 = __detail::__variant::__visit_result_t<_Visitor, _Variants...>;
1946
1947 using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
1948
1949 if constexpr (sizeof...(_Variants) == 1)
1950 {
1951 using _Vp = decltype(__variant::__as(std::declval<_Variants>()...));
1952
1953 constexpr bool __visit_rettypes_match = __detail::__variant::
1954 __check_visitor_results<_Visitor, _Vp>(
1955 make_index_sequence<variant_size_v<remove_reference_t<_Vp>>>());
1956 if constexpr (!__visit_rettypes_match)
1957 {
1958 static_assert(__visit_rettypes_match,
1959 "std::visit requires the visitor to have the same "
1960 "return type for all alternatives of a variant");
1961 return;
1962 }
1963 else
1964 return std::__do_visit<_Tag>(
1965 std::forward<_Visitor>(__visitor),
1966 static_cast<_Vp>(__variants)...);
1967 }
1968 else
1969 return std::__do_visit<_Tag>(
1970 std::forward<_Visitor>(__visitor),
1971 __variant::__as(std::forward<_Variants>(__variants))...);
1972 }
1973
1974#if __cplusplus > 201703L
1975 template<typename _Res, typename _Visitor, typename... _Variants>
1976 constexpr _Res
1977 visit(_Visitor&& __visitor, _Variants&&... __variants)
1978 {
1979 namespace __variant = std::__detail::__variant;
1980
1981 if ((__variant::__as(__variants).valueless_by_exception() || ...))
1982 __throw_bad_variant_access("std::visit<R>: variant is valueless");
1983
1984 return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
1985 __variant::__as(std::forward<_Variants>(__variants))...);
1986 }
1987#endif
1988
1989 /// @cond undocumented
1990 template<bool, typename... _Types>
1991 struct __variant_hash_call_base_impl
1992 {
1993 size_t
1994 operator()(const variant<_Types...>& __t) const
1995 noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
1996 {
1997 size_t __ret;
1998 __detail::__variant::__raw_visit(
1999 [&__t, &__ret](auto&& __t_mem) mutable
2000 {
2001 using _Type = __remove_cvref_t<decltype(__t_mem)>;
2002 if constexpr (!is_same_v<_Type,
2003 __detail::__variant::__variant_cookie>)
2004 __ret = std::hash<size_t>{}(__t.index())
2005 + std::hash<_Type>{}(__t_mem);
2006 else
2007 __ret = std::hash<size_t>{}(__t.index());
2008 }, __t);
2009 return __ret;
2010 }
2011 };
2012
2013 template<typename... _Types>
2014 struct __variant_hash_call_base_impl<false, _Types...> {};
2015
2016 template<typename... _Types>
2017 using __variant_hash_call_base =
2018 __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
2019 __enable_hash_call &&...), _Types...>;
2020 /// @endcond
2021
2022 template<typename... _Types>
2023 struct hash<variant<_Types...>>
2024 : private __detail::__variant::_Variant_hash_base<
2025 variant<_Types...>, std::index_sequence_for<_Types...>>,
2026 public __variant_hash_call_base<_Types...>
2027 {
2028 using result_type [[__deprecated__]] = size_t;
2029 using argument_type [[__deprecated__]] = variant<_Types...>;
2030 };
2031
2032 template<>
2033 struct hash<monostate>
2034 {
2035 using result_type [[__deprecated__]] = size_t;
2036 using argument_type [[__deprecated__]] = monostate;
2037
2038 size_t
2039 operator()(const monostate&) const noexcept
2040 {
2041 constexpr size_t __magic_monostate_hash = -7777;
2042 return __magic_monostate_hash;
2043 }
2044 };
2045
2046 template<typename... _Types>
2047 struct __is_fast_hash<hash<variant<_Types...>>>
2048 : bool_constant<(__is_fast_hash<_Types>::value && ...)>
2049 { };
2050
2051_GLIBCXX_END_NAMESPACE_VERSION
2052} // namespace std
2053
2054#endif // __cpp_lib_variant
2055#endif // _GLIBCXX_VARIANT
constexpr bool operator<=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:855
constexpr bool operator>=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:869
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:822
constexpr bool operator>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:862
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition type_traits:1779
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:113
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:116
typename add_pointer< _Tp >::type add_pointer_t
Alias template for add_pointer.
Definition type_traits:2232
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition type_traits:2781
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
Definition move.h:163
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:127
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition invoke.h:90
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:51
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:70
void void_t
A metafunction that always yields void, used for detecting valid types.
ISO C++ entities toplevel namespace is std.
make_integer_sequence< size_t, _Num > make_index_sequence
Alias template make_index_sequence.
Definition utility.h:186
constexpr void _Construct(_Tp *__p, _Args &&... __args)
integer_sequence< size_t, _Idx... > index_sequence
Alias template index_sequence.
Definition utility.h:182
Primary class template hash.
integral_constant
Definition type_traits:90
Class template integer_sequence.
Definition utility.h:163