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