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