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