-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathvariant
1775 lines (1531 loc) · 58.4 KB
/
variant
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// <variant> -*- C++ -*-
// Copyright (C) 2016-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file variant
* This is the <variant> C++ Library header.
*/
#ifndef _GLIBCXX_VARIANT
#define _GLIBCXX_VARIANT 1
#pragma GCC system_header
#if __cplusplus >= 201703L
#include <type_traits>
#include <utility>
#include <bits/enable_special_members.h>
#include <bits/functexcept.h>
#include <bits/move.h>
#include <bits/functional_hash.h>
#include <bits/invoke.h>
#include <ext/aligned_buffer.h>
#include <bits/parse_numbers.h>
#include <bits/stl_iterator_base_types.h>
#include <bits/stl_iterator_base_funcs.h>
#include <bits/stl_construct.h>
#if __cplusplus > 201703L
# include <compare>
#endif
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
namespace __detail
{
namespace __variant
{
template<size_t _Np, typename... _Types>
struct _Nth_type;
template<size_t _Np, typename _First, typename... _Rest>
struct _Nth_type<_Np, _First, _Rest...>
: _Nth_type<_Np-1, _Rest...> { };
template<typename _First, typename... _Rest>
struct _Nth_type<0, _First, _Rest...>
{ using type = _First; };
} // namespace __variant
} // namespace __detail
#define __cpp_lib_variant 201606L
template<typename... _Types> class tuple;
template<typename... _Types> class variant;
template <typename> struct hash;
template<typename _Variant>
struct variant_size;
template<typename _Variant>
struct variant_size<const _Variant> : variant_size<_Variant> {};
template<typename _Variant>
struct variant_size<volatile _Variant> : variant_size<_Variant> {};
template<typename _Variant>
struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
template<typename... _Types>
struct variant_size<variant<_Types...>>
: std::integral_constant<size_t, sizeof...(_Types)> {};
template<typename _Variant>
inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
template<size_t _Np, typename _Variant>
struct variant_alternative;
template<size_t _Np, typename _First, typename... _Rest>
struct variant_alternative<_Np, variant<_First, _Rest...>>
: variant_alternative<_Np-1, variant<_Rest...>> {};
template<typename _First, typename... _Rest>
struct variant_alternative<0, variant<_First, _Rest...>>
{ using type = _First; };
template<size_t _Np, typename _Variant>
using variant_alternative_t =
typename variant_alternative<_Np, _Variant>::type;
template<size_t _Np, typename _Variant>
struct variant_alternative<_Np, const _Variant>
{ using type = add_const_t<variant_alternative_t<_Np, _Variant>>; };
template<size_t _Np, typename _Variant>
struct variant_alternative<_Np, volatile _Variant>
{ using type = add_volatile_t<variant_alternative_t<_Np, _Variant>>; };
template<size_t _Np, typename _Variant>
struct variant_alternative<_Np, const volatile _Variant>
{ using type = add_cv_t<variant_alternative_t<_Np, _Variant>>; };
inline constexpr size_t variant_npos = -1;
template<size_t _Np, typename... _Types>
constexpr variant_alternative_t<_Np, variant<_Types...>>&
get(variant<_Types...>&);
template<size_t _Np, typename... _Types>
constexpr variant_alternative_t<_Np, variant<_Types...>>&&
get(variant<_Types...>&&);
template<size_t _Np, typename... _Types>
constexpr variant_alternative_t<_Np, variant<_Types...>> const&
get(const variant<_Types...>&);
template<size_t _Np, typename... _Types>
constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
get(const variant<_Types...>&&);
template<typename _Result_type, typename _Visitor, typename... _Variants>
constexpr decltype(auto)
__do_visit(_Visitor&& __visitor, _Variants&&... __variants);
template <typename... _Types, typename _Tp>
decltype(auto)
__variant_cast(_Tp&& __rhs)
{
if constexpr (is_lvalue_reference_v<_Tp>)
{
if constexpr (is_const_v<remove_reference_t<_Tp>>)
return static_cast<const variant<_Types...>&>(__rhs);
else
return static_cast<variant<_Types...>&>(__rhs);
}
else
return static_cast<variant<_Types...>&&>(__rhs);
}
namespace __detail
{
namespace __variant
{
// Returns the first appearence of _Tp in _Types.
// Returns sizeof...(_Types) if _Tp is not in _Types.
template<typename _Tp, typename... _Types>
struct __index_of : std::integral_constant<size_t, 0> {};
template<typename _Tp, typename... _Types>
inline constexpr size_t __index_of_v = __index_of<_Tp, _Types...>::value;
template<typename _Tp, typename _First, typename... _Rest>
struct __index_of<_Tp, _First, _Rest...> :
std::integral_constant<size_t, is_same_v<_Tp, _First>
? 0 : __index_of_v<_Tp, _Rest...> + 1> {};
// used for raw visitation
struct __variant_cookie {};
// used for raw visitation with indices passed in
struct __variant_idx_cookie { using type = __variant_idx_cookie; };
// Used to enable deduction (and same-type checking) for std::visit:
template<typename> struct __deduce_visit_result { };
// Visit variants that might be valueless.
template<typename _Visitor, typename... _Variants>
constexpr void
__raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
{
std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
std::forward<_Variants>(__variants)...);
}
// Visit variants that might be valueless, passing indices to the visitor.
template<typename _Visitor, typename... _Variants>
constexpr void
__raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
{
std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
std::forward<_Variants>(__variants)...);
}
// _Uninitialized<T> is guaranteed to be a trivially destructible type,
// even if T is not.
template<typename _Type, bool = std::is_trivially_destructible_v<_Type>>
struct _Uninitialized;
template<typename _Type>
struct _Uninitialized<_Type, true>
{
template<typename... _Args>
constexpr
_Uninitialized(in_place_index_t<0>, _Args&&... __args)
: _M_storage(std::forward<_Args>(__args)...)
{ }
constexpr const _Type& _M_get() const & noexcept
{ return _M_storage; }
constexpr _Type& _M_get() & noexcept
{ return _M_storage; }
constexpr const _Type&& _M_get() const && noexcept
{ return std::move(_M_storage); }
constexpr _Type&& _M_get() && noexcept
{ return std::move(_M_storage); }
_Type _M_storage;
};
template<typename _Type>
struct _Uninitialized<_Type, false>
{
template<typename... _Args>
constexpr
_Uninitialized(in_place_index_t<0>, _Args&&... __args)
{
::new ((void*)std::addressof(_M_storage))
_Type(std::forward<_Args>(__args)...);
}
const _Type& _M_get() const & noexcept
{ return *_M_storage._M_ptr(); }
_Type& _M_get() & noexcept
{ return *_M_storage._M_ptr(); }
const _Type&& _M_get() const && noexcept
{ return std::move(*_M_storage._M_ptr()); }
_Type&& _M_get() && noexcept
{ return std::move(*_M_storage._M_ptr()); }
__gnu_cxx::__aligned_membuf<_Type> _M_storage;
};
template<typename _Union>
constexpr decltype(auto)
__get(in_place_index_t<0>, _Union&& __u) noexcept
{ return std::forward<_Union>(__u)._M_first._M_get(); }
template<size_t _Np, typename _Union>
constexpr decltype(auto)
__get(in_place_index_t<_Np>, _Union&& __u) noexcept
{
return __variant::__get(in_place_index<_Np-1>,
std::forward<_Union>(__u)._M_rest);
}
// Returns the typed storage for __v.
template<size_t _Np, typename _Variant>
constexpr decltype(auto)
__get(_Variant&& __v) noexcept
{
return __variant::__get(std::in_place_index<_Np>,
std::forward<_Variant>(__v)._M_u);
}
template<typename... _Types>
struct _Traits
{
static constexpr bool _S_default_ctor =
is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
static constexpr bool _S_copy_ctor =
(is_copy_constructible_v<_Types> && ...);
static constexpr bool _S_move_ctor =
(is_move_constructible_v<_Types> && ...);
static constexpr bool _S_copy_assign =
_S_copy_ctor
&& (is_copy_assignable_v<_Types> && ...);
static constexpr bool _S_move_assign =
_S_move_ctor
&& (is_move_assignable_v<_Types> && ...);
static constexpr bool _S_trivial_dtor =
(is_trivially_destructible_v<_Types> && ...);
static constexpr bool _S_trivial_copy_ctor =
(is_trivially_copy_constructible_v<_Types> && ...);
static constexpr bool _S_trivial_move_ctor =
(is_trivially_move_constructible_v<_Types> && ...);
static constexpr bool _S_trivial_copy_assign =
_S_trivial_dtor && _S_trivial_copy_ctor
&& (is_trivially_copy_assignable_v<_Types> && ...);
static constexpr bool _S_trivial_move_assign =
_S_trivial_dtor && _S_trivial_move_ctor
&& (is_trivially_move_assignable_v<_Types> && ...);
// The following nothrow traits are for non-trivial SMFs. Trivial SMFs
// are always nothrow.
static constexpr bool _S_nothrow_default_ctor =
is_nothrow_default_constructible_v<
typename _Nth_type<0, _Types...>::type>;
static constexpr bool _S_nothrow_copy_ctor = false;
static constexpr bool _S_nothrow_move_ctor =
(is_nothrow_move_constructible_v<_Types> && ...);
static constexpr bool _S_nothrow_copy_assign = false;
static constexpr bool _S_nothrow_move_assign =
_S_nothrow_move_ctor
&& (is_nothrow_move_assignable_v<_Types> && ...);
};
// Defines members and ctors.
template<typename... _Types>
union _Variadic_union { };
template<typename _First, typename... _Rest>
union _Variadic_union<_First, _Rest...>
{
constexpr _Variadic_union() : _M_rest() { }
template<typename... _Args>
constexpr _Variadic_union(in_place_index_t<0>, _Args&&... __args)
: _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
{ }
template<size_t _Np, typename... _Args>
constexpr _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
: _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
{ }
_Uninitialized<_First> _M_first;
_Variadic_union<_Rest...> _M_rest;
};
// _Never_valueless_alt is true for variant alternatives that can
// always be placed in a variant without it becoming valueless.
// For suitably-small, trivially copyable types we can create temporaries
// on the stack and then memcpy them into place.
template<typename _Tp>
struct _Never_valueless_alt
: __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
{ };
// Specialize _Never_valueless_alt for other types which have a
// non-throwing and cheap move construction and move assignment operator,
// so that emplacing the type will provide the strong exception-safety
// guarantee, by creating and moving a temporary.
// Whether _Never_valueless_alt<T> is true or not affects the ABI of a
// variant using that alternative, so we can't change the value later!
// True if every alternative in _Types... can be emplaced in a variant
// without it becoming valueless. If this is true, variant<_Types...>
// can never be valueless, which enables some minor optimizations.
template <typename... _Types>
constexpr bool __never_valueless()
{
return _Traits<_Types...>::_S_move_assign
&& (_Never_valueless_alt<_Types>::value && ...);
}
// Defines index and the dtor, possibly trivial.
template<bool __trivially_destructible, typename... _Types>
struct _Variant_storage;
template <typename... _Types>
using __select_index =
typename __select_int::_Select_int_base<sizeof...(_Types),
unsigned char,
unsigned short>::type::value_type;
template<typename... _Types>
struct _Variant_storage<false, _Types...>
{
constexpr
_Variant_storage()
: _M_index(static_cast<__index_type>(variant_npos))
{ }
template<size_t _Np, typename... _Args>
constexpr
_Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
: _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
_M_index{_Np}
{ }
void _M_reset()
{
if (!_M_valid()) [[unlikely]]
return;
std::__do_visit<void>([](auto&& __this_mem) mutable
{
std::_Destroy(std::__addressof(__this_mem));
}, __variant_cast<_Types...>(*this));
_M_index = static_cast<__index_type>(variant_npos);
}
~_Variant_storage()
{ _M_reset(); }
void*
_M_storage() const noexcept
{
return const_cast<void*>(static_cast<const void*>(
std::addressof(_M_u)));
}
constexpr bool
_M_valid() const noexcept
{
if constexpr (__variant::__never_valueless<_Types...>())
return true;
return this->_M_index != __index_type(variant_npos);
}
_Variadic_union<_Types...> _M_u;
using __index_type = __select_index<_Types...>;
__index_type _M_index;
};
template<typename... _Types>
struct _Variant_storage<true, _Types...>
{
constexpr
_Variant_storage()
: _M_index(static_cast<__index_type>(variant_npos))
{ }
template<size_t _Np, typename... _Args>
constexpr
_Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
: _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
_M_index{_Np}
{ }
void _M_reset() noexcept
{ _M_index = static_cast<__index_type>(variant_npos); }
void*
_M_storage() const noexcept
{
return const_cast<void*>(static_cast<const void*>(
std::addressof(_M_u)));
}
constexpr bool
_M_valid() const noexcept
{
if constexpr (__variant::__never_valueless<_Types...>())
return true;
return this->_M_index != static_cast<__index_type>(variant_npos);
}
_Variadic_union<_Types...> _M_u;
using __index_type = __select_index<_Types...>;
__index_type _M_index;
};
template<typename... _Types>
using _Variant_storage_alias =
_Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
template<typename _Tp, typename _Up>
void __variant_construct_single(_Tp&& __lhs, _Up&& __rhs_mem)
{
void* __storage = std::addressof(__lhs._M_u);
using _Type = remove_reference_t<decltype(__rhs_mem)>;
if constexpr (!is_same_v<_Type, __variant_cookie>)
::new (__storage)
_Type(std::forward<decltype(__rhs_mem)>(__rhs_mem));
}
template<typename... _Types, typename _Tp, typename _Up>
void __variant_construct(_Tp&& __lhs, _Up&& __rhs)
{
__lhs._M_index = __rhs._M_index;
__variant::__raw_visit([&__lhs](auto&& __rhs_mem) mutable
{
__variant_construct_single(std::forward<_Tp>(__lhs),
std::forward<decltype(__rhs_mem)>(__rhs_mem));
}, __variant_cast<_Types...>(std::forward<_Up>(__rhs)));
}
// The following are (Copy|Move) (ctor|assign) layers for forwarding
// triviality and handling non-trivial SMF behaviors.
template<bool, typename... _Types>
struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
{
using _Base = _Variant_storage_alias<_Types...>;
using _Base::_Base;
_Copy_ctor_base(const _Copy_ctor_base& __rhs)
noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
{
__variant_construct<_Types...>(*this, __rhs);
}
_Copy_ctor_base(_Copy_ctor_base&&) = default;
_Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
_Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
};
template<typename... _Types>
struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
{
using _Base = _Variant_storage_alias<_Types...>;
using _Base::_Base;
};
template<typename... _Types>
using _Copy_ctor_alias =
_Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
template<bool, typename... _Types>
struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
{
using _Base = _Copy_ctor_alias<_Types...>;
using _Base::_Base;
_Move_ctor_base(_Move_ctor_base&& __rhs)
noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
{
__variant_construct<_Types...>(*this, std::move(__rhs));
}
template<typename _Up>
void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
{
this->_M_reset();
__variant_construct_single(*this, std::forward<_Up>(__rhs));
this->_M_index = __rhs_index;
}
template<typename _Up>
void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
{
this->_M_reset();
__variant_construct_single(*this, __rhs);
this->_M_index = __rhs_index;
}
_Move_ctor_base(const _Move_ctor_base&) = default;
_Move_ctor_base& operator=(const _Move_ctor_base&) = default;
_Move_ctor_base& operator=(_Move_ctor_base&&) = default;
};
template<typename... _Types>
struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
{
using _Base = _Copy_ctor_alias<_Types...>;
using _Base::_Base;
template<typename _Up>
void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
{
this->_M_reset();
__variant_construct_single(*this, std::forward<_Up>(__rhs));
this->_M_index = __rhs_index;
}
template<typename _Up>
void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
{
this->_M_reset();
__variant_construct_single(*this, __rhs);
this->_M_index = __rhs_index;
}
};
template<typename... _Types>
using _Move_ctor_alias =
_Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
template<bool, typename... _Types>
struct _Copy_assign_base : _Move_ctor_alias<_Types...>
{
using _Base = _Move_ctor_alias<_Types...>;
using _Base::_Base;
_Copy_assign_base&
operator=(const _Copy_assign_base& __rhs)
noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
{
__variant::__raw_idx_visit(
[this](auto&& __rhs_mem, auto __rhs_index) mutable
{
if constexpr (__rhs_index != variant_npos)
{
if (this->_M_index == __rhs_index)
__variant::__get<__rhs_index>(*this) = __rhs_mem;
else
{
using __rhs_type = __remove_cvref_t<decltype(__rhs_mem)>;
if constexpr (is_nothrow_copy_constructible_v<__rhs_type>
|| !is_nothrow_move_constructible_v<__rhs_type>)
// The standard says this->emplace<__rhs_type>(__rhs_mem)
// should be used here, but _M_destructive_copy is
// equivalent in this case. Either copy construction
// doesn't throw, so _M_destructive_copy gives strong
// exception safety guarantee, or both copy construction
// and move construction can throw, so emplace only gives
// basic exception safety anyway.
this->_M_destructive_copy(__rhs_index, __rhs_mem);
else
__variant_cast<_Types...>(*this)
= variant<_Types...>(std::in_place_index<__rhs_index>,
__rhs_mem);
}
}
else
this->_M_reset();
}, __variant_cast<_Types...>(__rhs));
return *this;
}
_Copy_assign_base(const _Copy_assign_base&) = default;
_Copy_assign_base(_Copy_assign_base&&) = default;
_Copy_assign_base& operator=(_Copy_assign_base&&) = default;
};
template<typename... _Types>
struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
{
using _Base = _Move_ctor_alias<_Types...>;
using _Base::_Base;
};
template<typename... _Types>
using _Copy_assign_alias =
_Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
template<bool, typename... _Types>
struct _Move_assign_base : _Copy_assign_alias<_Types...>
{
using _Base = _Copy_assign_alias<_Types...>;
using _Base::_Base;
_Move_assign_base&
operator=(_Move_assign_base&& __rhs)
noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
{
__variant::__raw_idx_visit(
[this](auto&& __rhs_mem, auto __rhs_index) mutable
{
if constexpr (__rhs_index != variant_npos)
{
if (this->_M_index == __rhs_index)
__variant::__get<__rhs_index>(*this) = std::move(__rhs_mem);
else
__variant_cast<_Types...>(*this)
.template emplace<__rhs_index>(std::move(__rhs_mem));
}
else
this->_M_reset();
}, __variant_cast<_Types...>(__rhs));
return *this;
}
_Move_assign_base(const _Move_assign_base&) = default;
_Move_assign_base(_Move_assign_base&&) = default;
_Move_assign_base& operator=(const _Move_assign_base&) = default;
};
template<typename... _Types>
struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
{
using _Base = _Copy_assign_alias<_Types...>;
using _Base::_Base;
};
template<typename... _Types>
using _Move_assign_alias =
_Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
template<typename... _Types>
struct _Variant_base : _Move_assign_alias<_Types...>
{
using _Base = _Move_assign_alias<_Types...>;
constexpr
_Variant_base()
noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
: _Variant_base(in_place_index<0>) { }
template<size_t _Np, typename... _Args>
constexpr explicit
_Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
: _Base(__i, std::forward<_Args>(__args)...)
{ }
_Variant_base(const _Variant_base&) = default;
_Variant_base(_Variant_base&&) = default;
_Variant_base& operator=(const _Variant_base&) = default;
_Variant_base& operator=(_Variant_base&&) = default;
};
// For how many times does _Tp appear in _Tuple?
template<typename _Tp, typename _Tuple>
struct __tuple_count;
template<typename _Tp, typename _Tuple>
inline constexpr size_t __tuple_count_v =
__tuple_count<_Tp, _Tuple>::value;
template<typename _Tp, typename... _Types>
struct __tuple_count<_Tp, tuple<_Types...>>
: integral_constant<size_t, 0> { };
template<typename _Tp, typename _First, typename... _Rest>
struct __tuple_count<_Tp, tuple<_First, _Rest...>>
: integral_constant<
size_t,
__tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { };
// TODO: Reuse this in <tuple> ?
template<typename _Tp, typename... _Types>
inline constexpr bool __exactly_once =
__tuple_count_v<_Tp, tuple<_Types...>> == 1;
// Helper used to check for valid conversions that don't involve narrowing.
template<typename _Ti> struct _Arr { _Ti _M_x[1]; };
// Build an imaginary function FUN(Ti) for each alternative type Ti
template<size_t _Ind, typename _Tp, typename _Ti,
bool _Ti_is_cv_bool = is_same_v<remove_cv_t<_Ti>, bool>,
typename = void>
struct _Build_FUN
{
// This function means 'using _Build_FUN<I, T, Ti>::_S_fun;' is valid,
// but only static functions will be considered in the call below.
void _S_fun();
};
// ... for which Ti x[] = {std::forward<T>(t)}; is well-formed,
template<size_t _Ind, typename _Tp, typename _Ti>
struct _Build_FUN<_Ind, _Tp, _Ti, false,
void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
{
// This is the FUN function for type _Ti, with index _Ind
static integral_constant<size_t, _Ind> _S_fun(_Ti);
};
// ... and if Ti is cv bool, remove_cvref_t<T> is bool.
template<size_t _Ind, typename _Tp, typename _Ti>
struct _Build_FUN<_Ind, _Tp, _Ti, true,
enable_if_t<is_same_v<__remove_cvref_t<_Tp>, bool>>>
{
// This is the FUN function for when _Ti is cv bool, with index _Ind
static integral_constant<size_t, _Ind> _S_fun(_Ti);
};
template<typename _Tp, typename _Variant,
typename = make_index_sequence<variant_size_v<_Variant>>>
struct _Build_FUNs;
template<typename _Tp, typename... _Ti, size_t... _Ind>
struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
: _Build_FUN<_Ind, _Tp, _Ti>...
{
using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
};
// The index j of the overload FUN(Tj) selected by overload resolution
// for FUN(std::forward<_Tp>(t))
template<typename _Tp, typename _Variant>
using _FUN_type
= decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
// The index selected for FUN(std::forward<T>(t)), or variant_npos if none.
template<typename _Tp, typename _Variant, typename = void>
struct __accepted_index
: integral_constant<size_t, variant_npos>
{ };
template<typename _Tp, typename _Variant>
struct __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
: _FUN_type<_Tp, _Variant>
{ };
// Returns the raw storage for __v.
template<typename _Variant>
void* __get_storage(_Variant&& __v) noexcept
{ return __v._M_storage(); }
template <typename _Maybe_variant_cookie, typename _Variant>
struct _Extra_visit_slot_needed
{
template <typename> struct _Variant_never_valueless;
template <typename... _Types>
struct _Variant_never_valueless<variant<_Types...>>
: bool_constant<__variant::__never_valueless<_Types...>()> {};
static constexpr bool value =
(is_same_v<_Maybe_variant_cookie, __variant_cookie>
|| is_same_v<_Maybe_variant_cookie, __variant_idx_cookie>)
&& !_Variant_never_valueless<__remove_cvref_t<_Variant>>::value;
};
// Used for storing a multi-dimensional vtable.
template<typename _Tp, size_t... _Dimensions>
struct _Multi_array;
// Partial specialization with rank zero, stores a single _Tp element.
template<typename _Tp>
struct _Multi_array<_Tp>
{
template<typename>
struct __untag_result
: false_type
{ using element_type = _Tp; };
template <typename... _Args>
struct __untag_result<const void(*)(_Args...)>
: false_type
{ using element_type = void(*)(_Args...); };
template <typename... _Args>
struct __untag_result<__variant_cookie(*)(_Args...)>
: false_type
{ using element_type = void(*)(_Args...); };
template <typename... _Args>
struct __untag_result<__variant_idx_cookie(*)(_Args...)>
: false_type
{ using element_type = void(*)(_Args...); };
template <typename _Res, typename... _Args>
struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
: true_type
{ using element_type = _Res(*)(_Args...); };
using __result_is_deduced = __untag_result<_Tp>;
constexpr const typename __untag_result<_Tp>::element_type&
_M_access() const
{ return _M_data; }
typename __untag_result<_Tp>::element_type _M_data;
};
// Partial specialization with rank >= 1.
template<typename _Ret,
typename _Visitor,
typename... _Variants,
size_t __first, size_t... __rest>
struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
{
static constexpr size_t __index =
sizeof...(_Variants) - sizeof...(__rest) - 1;
using _Variant = typename _Nth_type<__index, _Variants...>::type;
static constexpr int __do_cookie =
_Extra_visit_slot_needed<_Ret, _Variant>::value ? 1 : 0;
using _Tp = _Ret(*)(_Visitor, _Variants...);
template<typename... _Args>
constexpr decltype(auto)
_M_access(size_t __first_index, _Args... __rest_indices) const
{
return _M_arr[__first_index + __do_cookie]
._M_access(__rest_indices...);
}
_Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
};
// Creates a multi-dimensional vtable recursively.
//
// For example,
// visit([](auto, auto){},
// variant<int, char>(), // typedef'ed as V1
// variant<float, double, long double>()) // typedef'ed as V2
// will trigger instantiations of:
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
// tuple<V1&&, V2&&>, std::index_sequence<>>
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
// tuple<V1&&, V2&&>, std::index_sequence<0>>
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
// tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
// tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
// tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
// tuple<V1&&, V2&&>, std::index_sequence<1>>
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
// tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
// tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
// __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
// tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
// The returned multi-dimensional vtable can be fast accessed by the visitor
// using index calculation.
template<typename _Array_type, typename _Index_seq>
struct __gen_vtable_impl;
// Defines the _S_apply() member that returns a _Multi_array populated
// with function pointers that perform the visitation expressions e(m)
// for each valid pack of indexes into the variant types _Variants.
//
// This partial specialization builds up the index sequences by recursively
// calling _S_apply() on the next specialization of __gen_vtable_impl.
// The base case of the recursion defines the actual function pointers.
template<typename _Result_type, typename _Visitor, size_t... __dimensions,
typename... _Variants, size_t... __indices>
struct __gen_vtable_impl<
_Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
std::index_sequence<__indices...>>
{
using _Next =
remove_reference_t<typename _Nth_type<sizeof...(__indices),
_Variants...>::type>;
using _Array_type =
_Multi_array<_Result_type (*)(_Visitor, _Variants...),
__dimensions...>;
static constexpr _Array_type
_S_apply()
{
_Array_type __vtable{};
_S_apply_all_alts(
__vtable, make_index_sequence<variant_size_v<_Next>>());
return __vtable;
}
template<size_t... __var_indices>
static constexpr void
_S_apply_all_alts(_Array_type& __vtable,
std::index_sequence<__var_indices...>)
{
if constexpr (_Extra_visit_slot_needed<_Result_type, _Next>::value)
(_S_apply_single_alt<true, __var_indices>(
__vtable._M_arr[__var_indices + 1],
&(__vtable._M_arr[0])), ...);
else
(_S_apply_single_alt<false, __var_indices>(
__vtable._M_arr[__var_indices]), ...);
}
template<bool __do_cookie, size_t __index, typename _Tp>
static constexpr void
_S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
{
if constexpr (__do_cookie)
{
__element = __gen_vtable_impl<
_Tp,
std::index_sequence<__indices..., __index>>::_S_apply();
*__cookie_element = __gen_vtable_impl<
_Tp,
std::index_sequence<__indices..., variant_npos>>::_S_apply();
}
else
{
__element = __gen_vtable_impl<
remove_reference_t<decltype(__element)>,
std::index_sequence<__indices..., __index>>::_S_apply();
}
}
};
// This partial specialization is the base case for the recursion.
// It populates a _Multi_array element with the address of a function
// that invokes the visitor with the alternatives specified by __indices.
template<typename _Result_type, typename _Visitor, typename... _Variants,
size_t... __indices>
struct __gen_vtable_impl<
_Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
std::index_sequence<__indices...>>
{
using _Array_type =
_Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
template<size_t __index, typename _Variant>
static constexpr decltype(auto)
__element_by_index_or_cookie(_Variant&& __var) noexcept
{
if constexpr (__index != variant_npos)
return __variant::__get<__index>(std::forward<_Variant>(__var));
else
return __variant_cookie{};