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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
|
// -*- C++ -*-
//===------------------------------ variant -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP_VARIANT
#define _LIBCPP_VARIANT
/*
variant synopsis
namespace std {
// 20.7.2, class template variant
template <class... Types>
class variant {
public:
// 20.7.2.1, constructors
constexpr variant() noexcept(see below);
variant(const variant&); // constexpr in C++20
variant(variant&&) noexcept(see below); // constexpr in C++20
template <class T> constexpr variant(T&&) noexcept(see below);
template <class T, class... Args>
constexpr explicit variant(in_place_type_t<T>, Args&&...);
template <class T, class U, class... Args>
constexpr explicit variant(
in_place_type_t<T>, initializer_list<U>, Args&&...);
template <size_t I, class... Args>
constexpr explicit variant(in_place_index_t<I>, Args&&...);
template <size_t I, class U, class... Args>
constexpr explicit variant(
in_place_index_t<I>, initializer_list<U>, Args&&...);
// 20.7.2.2, destructor
~variant();
// 20.7.2.3, assignment
variant& operator=(const variant&); // constexpr in C++20
variant& operator=(variant&&) noexcept(see below); // constexpr in C++20
template <class T> variant& operator=(T&&) noexcept(see below);
// 20.7.2.4, modifiers
template <class T, class... Args>
T& emplace(Args&&...);
template <class T, class U, class... Args>
T& emplace(initializer_list<U>, Args&&...);
template <size_t I, class... Args>
variant_alternative_t<I, variant>& emplace(Args&&...);
template <size_t I, class U, class... Args>
variant_alternative_t<I, variant>& emplace(initializer_list<U>, Args&&...);
// 20.7.2.5, value status
constexpr bool valueless_by_exception() const noexcept;
constexpr size_t index() const noexcept;
// 20.7.2.6, swap
void swap(variant&) noexcept(see below);
};
// 20.7.3, variant helper classes
template <class T> struct variant_size; // undefined
template <class T>
inline constexpr size_t variant_size_v = variant_size<T>::value;
template <class T> struct variant_size<const T>;
template <class T> struct variant_size<volatile T>;
template <class T> struct variant_size<const volatile T>;
template <class... Types>
struct variant_size<variant<Types...>>;
template <size_t I, class T> struct variant_alternative; // undefined
template <size_t I, class T>
using variant_alternative_t = typename variant_alternative<I, T>::type;
template <size_t I, class T> struct variant_alternative<I, const T>;
template <size_t I, class T> struct variant_alternative<I, volatile T>;
template <size_t I, class T> struct variant_alternative<I, const volatile T>;
template <size_t I, class... Types>
struct variant_alternative<I, variant<Types...>>;
inline constexpr size_t variant_npos = -1;
// 20.7.4, value access
template <class T, class... Types>
constexpr bool holds_alternative(const variant<Types...>&) noexcept;
template <size_t I, class... Types>
constexpr variant_alternative_t<I, variant<Types...>>&
get(variant<Types...>&);
template <size_t I, class... Types>
constexpr variant_alternative_t<I, variant<Types...>>&&
get(variant<Types...>&&);
template <size_t I, class... Types>
constexpr variant_alternative_t<I, variant<Types...>> const&
get(const variant<Types...>&);
template <size_t I, class... Types>
constexpr variant_alternative_t<I, variant<Types...>> const&&
get(const variant<Types...>&&);
template <class T, class... Types>
constexpr T& get(variant<Types...>&);
template <class T, class... Types>
constexpr T&& get(variant<Types...>&&);
template <class T, class... Types>
constexpr const T& get(const variant<Types...>&);
template <class T, class... Types>
constexpr const T&& get(const variant<Types...>&&);
template <size_t I, class... Types>
constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
get_if(variant<Types...>*) noexcept;
template <size_t I, class... Types>
constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
get_if(const variant<Types...>*) noexcept;
template <class T, class... Types>
constexpr add_pointer_t<T>
get_if(variant<Types...>*) noexcept;
template <class T, class... Types>
constexpr add_pointer_t<const T>
get_if(const variant<Types...>*) noexcept;
// 20.7.5, relational operators
template <class... Types>
constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
template <class... Types>
constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
template <class... Types>
constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
template <class... Types>
constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
template <class... Types>
constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
template <class... Types>
constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
// 20.7.6, visitation
template <class Visitor, class... Variants>
constexpr see below visit(Visitor&&, Variants&&...);
template <class R, class Visitor, class... Variants>
constexpr R visit(Visitor&&, Variants&&...); // since C++20
// 20.7.7, class monostate
struct monostate;
// 20.7.8, monostate relational operators
constexpr bool operator<(monostate, monostate) noexcept;
constexpr bool operator>(monostate, monostate) noexcept;
constexpr bool operator<=(monostate, monostate) noexcept;
constexpr bool operator>=(monostate, monostate) noexcept;
constexpr bool operator==(monostate, monostate) noexcept;
constexpr bool operator!=(monostate, monostate) noexcept;
// 20.7.9, specialized algorithms
template <class... Types>
void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
// 20.7.10, class bad_variant_access
class bad_variant_access;
// 20.7.11, hash support
template <class T> struct hash;
template <class... Types> struct hash<variant<Types...>>;
template <> struct hash<monostate>;
} // namespace std
*/
#include <__availability>
#include <__config>
#include <__functional/hash.h>
#include <__tuple>
#include <__utility/forward.h>
#include <__variant/monostate.h>
#include <compare>
#include <exception>
#include <initializer_list>
#include <limits>
#include <new>
#include <tuple>
#include <type_traits>
#include <utility>
#include <version>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif
_LIBCPP_PUSH_MACROS
#include <__undef_macros>
namespace std { // explicitly not using versioning namespace
class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
public:
virtual const char* what() const _NOEXCEPT;
};
} // namespace std
_LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER > 14
// Light N-dimensional array of function pointers. Used in place of std::array to avoid
// adding a dependency.
template<class _Tp, size_t _Size>
struct __farray {
static_assert(_Size > 0, "N-dimensional array should never be empty in std::visit");
_Tp __buf_[_Size] = {};
_LIBCPP_INLINE_VISIBILITY constexpr
const _Tp &operator[](size_t __n) const noexcept {
return __buf_[__n];
}
};
_LIBCPP_NORETURN
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
void __throw_bad_variant_access() {
#ifndef _LIBCPP_NO_EXCEPTIONS
throw bad_variant_access();
#else
_VSTD::abort();
#endif
}
template <class... _Types>
class _LIBCPP_TEMPLATE_VIS variant;
template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS variant_size;
template <class _Tp>
inline constexpr size_t variant_size_v = variant_size<_Tp>::value;
template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS variant_size<volatile _Tp> : variant_size<_Tp> {};
template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp>
: variant_size<_Tp> {};
template <class... _Types>
struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>>
: integral_constant<size_t, sizeof...(_Types)> {};
template <size_t _Ip, class _Tp>
struct _LIBCPP_TEMPLATE_VIS variant_alternative;
template <size_t _Ip, class _Tp>
using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
template <size_t _Ip, class _Tp>
struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp>
: add_const<variant_alternative_t<_Ip, _Tp>> {};
template <size_t _Ip, class _Tp>
struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, volatile _Tp>
: add_volatile<variant_alternative_t<_Ip, _Tp>> {};
template <size_t _Ip, class _Tp>
struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp>
: add_cv<variant_alternative_t<_Ip, _Tp>> {};
template <size_t _Ip, class... _Types>
struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>");
using type = __type_pack_element<_Ip, _Types...>;
};
inline constexpr size_t variant_npos = static_cast<size_t>(-1);
constexpr int __choose_index_type(unsigned int __num_elem) {
if (__num_elem < numeric_limits<unsigned char>::max())
return 0;
if (__num_elem < numeric_limits<unsigned short>::max())
return 1;
return 2;
}
template <size_t _NumAlts>
using __variant_index_t =
#ifndef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
unsigned int;
#else
std::tuple_element_t<
__choose_index_type(_NumAlts),
std::tuple<unsigned char, unsigned short, unsigned int>
>;
#endif
template <class _IndexType>
constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
template <class... _Types>
class _LIBCPP_TEMPLATE_VIS variant;
template <class... _Types>
_LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&
__as_variant(variant<_Types...>& __vs) noexcept {
return __vs;
}
template <class... _Types>
_LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&
__as_variant(const variant<_Types...>& __vs) noexcept {
return __vs;
}
template <class... _Types>
_LIBCPP_INLINE_VISIBILITY constexpr variant<_Types...>&&
__as_variant(variant<_Types...>&& __vs) noexcept {
return _VSTD::move(__vs);
}
template <class... _Types>
_LIBCPP_INLINE_VISIBILITY constexpr const variant<_Types...>&&
__as_variant(const variant<_Types...>&& __vs) noexcept {
return _VSTD::move(__vs);
}
namespace __find_detail {
template <class _Tp, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr size_t __find_index() {
constexpr bool __matches[] = {is_same_v<_Tp, _Types>...};
size_t __result = __not_found;
for (size_t __i = 0; __i < sizeof...(_Types); ++__i) {
if (__matches[__i]) {
if (__result != __not_found) {
return __ambiguous;
}
__result = __i;
}
}
return __result;
}
template <size_t _Index>
struct __find_unambiguous_index_sfinae_impl
: integral_constant<size_t, _Index> {};
template <>
struct __find_unambiguous_index_sfinae_impl<__not_found> {};
template <>
struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
template <class _Tp, class... _Types>
struct __find_unambiguous_index_sfinae
: __find_unambiguous_index_sfinae_impl<__find_index<_Tp, _Types...>()> {};
} // namespace __find_detail
namespace __variant_detail {
struct __valueless_t {};
enum class _Trait { _TriviallyAvailable, _Available, _Unavailable };
template <typename _Tp,
template <typename> class _IsTriviallyAvailable,
template <typename> class _IsAvailable>
constexpr _Trait __trait =
_IsTriviallyAvailable<_Tp>::value
? _Trait::_TriviallyAvailable
: _IsAvailable<_Tp>::value ? _Trait::_Available : _Trait::_Unavailable;
inline _LIBCPP_INLINE_VISIBILITY
constexpr _Trait __common_trait(initializer_list<_Trait> __traits) {
_Trait __result = _Trait::_TriviallyAvailable;
for (_Trait __t : __traits) {
if (static_cast<int>(__t) > static_cast<int>(__result)) {
__result = __t;
}
}
return __result;
}
template <typename... _Types>
struct __traits {
static constexpr _Trait __copy_constructible_trait =
__common_trait({__trait<_Types,
is_trivially_copy_constructible,
is_copy_constructible>...});
static constexpr _Trait __move_constructible_trait =
__common_trait({__trait<_Types,
is_trivially_move_constructible,
is_move_constructible>...});
static constexpr _Trait __copy_assignable_trait = __common_trait(
{__copy_constructible_trait,
__trait<_Types, is_trivially_copy_assignable, is_copy_assignable>...});
static constexpr _Trait __move_assignable_trait = __common_trait(
{__move_constructible_trait,
__trait<_Types, is_trivially_move_assignable, is_move_assignable>...});
static constexpr _Trait __destructible_trait = __common_trait(
{__trait<_Types, is_trivially_destructible, is_destructible>...});
};
namespace __access {
struct __union {
template <class _Vp>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<0>) {
return _VSTD::forward<_Vp>(__v).__head;
}
template <class _Vp, size_t _Ip>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto&& __get_alt(_Vp&& __v, in_place_index_t<_Ip>) {
return __get_alt(_VSTD::forward<_Vp>(__v).__tail, in_place_index<_Ip - 1>);
}
};
struct __base {
template <size_t _Ip, class _Vp>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto&& __get_alt(_Vp&& __v) {
return __union::__get_alt(_VSTD::forward<_Vp>(__v).__data,
in_place_index<_Ip>);
}
};
struct __variant {
template <size_t _Ip, class _Vp>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto&& __get_alt(_Vp&& __v) {
return __base::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v).__impl);
}
};
} // namespace __access
namespace __visitation {
struct __base {
template <class _Visitor, class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr decltype(auto)
__visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
constexpr auto __fdiagonal =
__make_fdiagonal<_Visitor&&,
decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
return __fdiagonal[__index](_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Vs>(__vs).__as_base()...);
}
template <class _Visitor, class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
_Vs&&... __vs) {
constexpr auto __fmatrix =
__make_fmatrix<_Visitor&&,
decltype(_VSTD::forward<_Vs>(__vs).__as_base())...>();
return __at(__fmatrix, __vs.index()...)(
_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Vs>(__vs).__as_base()...);
}
private:
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr const _Tp& __at(const _Tp& __elem) { return __elem; }
template <class _Tp, size_t _Np, typename... _Indices>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto&& __at(const __farray<_Tp, _Np>& __elems,
size_t __index, _Indices... __indices) {
return __at(__elems[__index], __indices...);
}
template <class _Fp, class... _Fs>
static constexpr void __std_visit_visitor_return_type_check() {
static_assert(
__all<is_same_v<_Fp, _Fs>...>::value,
"`std::visit` requires the visitor to have a single return type.");
}
template <class... _Fs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto __make_farray(_Fs&&... __fs) {
__std_visit_visitor_return_type_check<__uncvref_t<_Fs>...>();
using __result = __farray<common_type_t<__uncvref_t<_Fs>...>, sizeof...(_Fs)>;
return __result{{_VSTD::forward<_Fs>(__fs)...}};
}
template <size_t... _Is>
struct __dispatcher {
template <class _Fp, class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
return _VSTD::__invoke_constexpr(
static_cast<_Fp>(__f),
__access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
}
};
template <class _Fp, class... _Vs, size_t... _Is>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto __make_dispatch(index_sequence<_Is...>) {
return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;
}
template <size_t _Ip, class _Fp, class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto __make_fdiagonal_impl() {
return __make_dispatch<_Fp, _Vs...>(
index_sequence<((void)__identity<_Vs>{}, _Ip)...>{});
}
template <class _Fp, class... _Vs, size_t... _Is>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto __make_fdiagonal_impl(index_sequence<_Is...>) {
return __base::__make_farray(__make_fdiagonal_impl<_Is, _Fp, _Vs...>()...);
}
template <class _Fp, class _Vp, class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto __make_fdiagonal() {
constexpr size_t _Np = __uncvref_t<_Vp>::__size();
static_assert(__all<(_Np == __uncvref_t<_Vs>::__size())...>::value);
return __make_fdiagonal_impl<_Fp, _Vp, _Vs...>(make_index_sequence<_Np>{});
}
template <class _Fp, class... _Vs, size_t... _Is>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto __make_fmatrix_impl(index_sequence<_Is...> __is) {
return __make_dispatch<_Fp, _Vs...>(__is);
}
template <class _Fp, class... _Vs, size_t... _Is, size_t... _Js, class... _Ls>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto __make_fmatrix_impl(index_sequence<_Is...>,
index_sequence<_Js...>,
_Ls... __ls) {
return __base::__make_farray(__make_fmatrix_impl<_Fp, _Vs...>(
index_sequence<_Is..., _Js>{}, __ls...)...);
}
template <class _Fp, class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto __make_fmatrix() {
return __make_fmatrix_impl<_Fp, _Vs...>(
index_sequence<>{}, make_index_sequence<__uncvref_t<_Vs>::__size()>{}...);
}
};
struct __variant {
template <class _Visitor, class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr decltype(auto)
__visit_alt_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
return __base::__visit_alt_at(__index,
_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Vs>(__vs).__impl...);
}
template <class _Visitor, class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr decltype(auto) __visit_alt(_Visitor&& __visitor,
_Vs&&... __vs) {
return __base::__visit_alt(
_VSTD::forward<_Visitor>(__visitor),
_VSTD::__as_variant(_VSTD::forward<_Vs>(__vs)).__impl...);
}
template <class _Visitor, class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr decltype(auto)
__visit_value_at(size_t __index, _Visitor&& __visitor, _Vs&&... __vs) {
return __visit_alt_at(
__index,
__make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
_VSTD::forward<_Vs>(__vs)...);
}
template <class _Visitor, class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr decltype(auto) __visit_value(_Visitor&& __visitor,
_Vs&&... __vs) {
return __visit_alt(
__make_value_visitor(_VSTD::forward<_Visitor>(__visitor)),
_VSTD::forward<_Vs>(__vs)...);
}
#if _LIBCPP_STD_VER > 17
template <class _Rp, class _Visitor, class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr _Rp __visit_value(_Visitor&& __visitor,
_Vs&&... __vs) {
return __visit_alt(
__make_value_visitor<_Rp>(_VSTD::forward<_Visitor>(__visitor)),
_VSTD::forward<_Vs>(__vs)...);
}
#endif
private:
template <class _Visitor, class... _Values>
static constexpr void __std_visit_exhaustive_visitor_check() {
static_assert(is_invocable_v<_Visitor, _Values...>,
"`std::visit` requires the visitor to be exhaustive.");
}
template <class _Visitor>
struct __value_visitor {
template <class... _Alts>
inline _LIBCPP_INLINE_VISIBILITY
constexpr decltype(auto) operator()(_Alts&&... __alts) const {
__std_visit_exhaustive_visitor_check<
_Visitor,
decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
return _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Alts>(__alts).__value...);
}
_Visitor&& __visitor;
};
#if _LIBCPP_STD_VER > 17
template <class _Rp, class _Visitor>
struct __value_visitor_return_type {
template <class... _Alts>
inline _LIBCPP_INLINE_VISIBILITY
constexpr _Rp operator()(_Alts&&... __alts) const {
__std_visit_exhaustive_visitor_check<
_Visitor,
decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
if constexpr (is_void_v<_Rp>) {
_VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Alts>(__alts).__value...);
}
else {
return _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Alts>(__alts).__value...);
}
}
_Visitor&& __visitor;
};
#endif
template <class _Visitor>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
return __value_visitor<_Visitor>{_VSTD::forward<_Visitor>(__visitor)};
}
#if _LIBCPP_STD_VER > 17
template <class _Rp, class _Visitor>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr auto __make_value_visitor(_Visitor&& __visitor) {
return __value_visitor_return_type<_Rp, _Visitor>{_VSTD::forward<_Visitor>(__visitor)};
}
#endif
};
} // namespace __visitation
template <size_t _Index, class _Tp>
struct _LIBCPP_TEMPLATE_VIS __alt {
using __value_type = _Tp;
template <class... _Args>
inline _LIBCPP_INLINE_VISIBILITY
explicit constexpr __alt(in_place_t, _Args&&... __args)
: __value(_VSTD::forward<_Args>(__args)...) {}
__value_type __value;
};
template <_Trait _DestructibleTrait, size_t _Index, class... _Types>
union _LIBCPP_TEMPLATE_VIS __union;
template <_Trait _DestructibleTrait, size_t _Index>
union _LIBCPP_TEMPLATE_VIS __union<_DestructibleTrait, _Index> {};
#define _LIBCPP_VARIANT_UNION(destructible_trait, destructor) \
template <size_t _Index, class _Tp, class... _Types> \
union _LIBCPP_TEMPLATE_VIS __union<destructible_trait, \
_Index, \
_Tp, \
_Types...> { \
public: \
inline _LIBCPP_INLINE_VISIBILITY \
explicit constexpr __union(__valueless_t) noexcept : __dummy{} {} \
\
template <class... _Args> \
inline _LIBCPP_INLINE_VISIBILITY \
explicit constexpr __union(in_place_index_t<0>, _Args&&... __args) \
: __head(in_place, _VSTD::forward<_Args>(__args)...) {} \
\
template <size_t _Ip, class... _Args> \
inline _LIBCPP_INLINE_VISIBILITY \
explicit constexpr __union(in_place_index_t<_Ip>, _Args&&... __args) \
: __tail(in_place_index<_Ip - 1>, _VSTD::forward<_Args>(__args)...) {} \
\
__union(const __union&) = default; \
__union(__union&&) = default; \
\
destructor \
\
__union& operator=(const __union&) = default; \
__union& operator=(__union&&) = default; \
\
private: \
char __dummy; \
__alt<_Index, _Tp> __head; \
__union<destructible_trait, _Index + 1, _Types...> __tail; \
\
friend struct __access::__union; \
}
_LIBCPP_VARIANT_UNION(_Trait::_TriviallyAvailable, ~__union() = default;);
_LIBCPP_VARIANT_UNION(_Trait::_Available, ~__union() {});
_LIBCPP_VARIANT_UNION(_Trait::_Unavailable, ~__union() = delete;);
#undef _LIBCPP_VARIANT_UNION
template <_Trait _DestructibleTrait, class... _Types>
class _LIBCPP_TEMPLATE_VIS __base {
public:
using __index_t = __variant_index_t<sizeof...(_Types)>;
inline _LIBCPP_INLINE_VISIBILITY
explicit constexpr __base(__valueless_t tag) noexcept
: __data(tag), __index(__variant_npos<__index_t>) {}
template <size_t _Ip, class... _Args>
inline _LIBCPP_INLINE_VISIBILITY
explicit constexpr __base(in_place_index_t<_Ip>, _Args&&... __args)
:
__data(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...),
__index(_Ip) {}
inline _LIBCPP_INLINE_VISIBILITY
constexpr bool valueless_by_exception() const noexcept {
return index() == variant_npos;
}
inline _LIBCPP_INLINE_VISIBILITY
constexpr size_t index() const noexcept {
return __index == __variant_npos<__index_t> ? variant_npos : __index;
}
protected:
inline _LIBCPP_INLINE_VISIBILITY
constexpr auto&& __as_base() & { return *this; }
inline _LIBCPP_INLINE_VISIBILITY
constexpr auto&& __as_base() && { return _VSTD::move(*this); }
inline _LIBCPP_INLINE_VISIBILITY
constexpr auto&& __as_base() const & { return *this; }
inline _LIBCPP_INLINE_VISIBILITY
constexpr auto&& __as_base() const && { return _VSTD::move(*this); }
inline _LIBCPP_INLINE_VISIBILITY
static constexpr size_t __size() { return sizeof...(_Types); }
__union<_DestructibleTrait, 0, _Types...> __data;
__index_t __index;
friend struct __access::__base;
friend struct __visitation::__base;
};
template <class _Traits, _Trait = _Traits::__destructible_trait>
class _LIBCPP_TEMPLATE_VIS __dtor;
#define _LIBCPP_VARIANT_DESTRUCTOR(destructible_trait, destructor, destroy) \
template <class... _Types> \
class _LIBCPP_TEMPLATE_VIS __dtor<__traits<_Types...>, \
destructible_trait> \
: public __base<destructible_trait, _Types...> { \
using __base_type = __base<destructible_trait, _Types...>; \
using __index_t = typename __base_type::__index_t; \
\
public: \
using __base_type::__base_type; \
using __base_type::operator=; \
\
__dtor(const __dtor&) = default; \
__dtor(__dtor&&) = default; \
destructor \
__dtor& operator=(const __dtor&) = default; \
__dtor& operator=(__dtor&&) = default; \
\
protected: \
inline _LIBCPP_INLINE_VISIBILITY \
destroy \
}
_LIBCPP_VARIANT_DESTRUCTOR(
_Trait::_TriviallyAvailable,
~__dtor() = default;,
void __destroy() noexcept { this->__index = __variant_npos<__index_t>; });
_LIBCPP_VARIANT_DESTRUCTOR(
_Trait::_Available,
~__dtor() { __destroy(); },
void __destroy() noexcept {
if (!this->valueless_by_exception()) {
__visitation::__base::__visit_alt(
[](auto& __alt) noexcept {
using __alt_type = __uncvref_t<decltype(__alt)>;
__alt.~__alt_type();
},
*this);
}
this->__index = __variant_npos<__index_t>;
});
_LIBCPP_VARIANT_DESTRUCTOR(
_Trait::_Unavailable,
~__dtor() = delete;,
void __destroy() noexcept = delete;);
#undef _LIBCPP_VARIANT_DESTRUCTOR
template <class _Traits>
class _LIBCPP_TEMPLATE_VIS __ctor : public __dtor<_Traits> {
using __base_type = __dtor<_Traits>;
public:
using __base_type::__base_type;
using __base_type::operator=;
protected:
template <size_t _Ip, class _Tp, class... _Args>
inline _LIBCPP_INLINE_VISIBILITY
static _Tp& __construct_alt(__alt<_Ip, _Tp>& __a, _Args&&... __args) {
::new ((void*)_VSTD::addressof(__a))
__alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
return __a.__value;
}
template <class _Rhs>
inline _LIBCPP_INLINE_VISIBILITY
static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
__lhs.__destroy();
if (!__rhs.valueless_by_exception()) {
__visitation::__base::__visit_alt_at(
__rhs.index(),
[](auto& __lhs_alt, auto&& __rhs_alt) {
__construct_alt(
__lhs_alt,
_VSTD::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
},
__lhs, _VSTD::forward<_Rhs>(__rhs));
__lhs.__index = __rhs.index();
}
}
};
template <class _Traits, _Trait = _Traits::__move_constructible_trait>
class _LIBCPP_TEMPLATE_VIS __move_constructor;
#define _LIBCPP_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, \
move_constructor) \
template <class... _Types> \
class _LIBCPP_TEMPLATE_VIS __move_constructor<__traits<_Types...>, \
move_constructible_trait> \
: public __ctor<__traits<_Types...>> { \
using __base_type = __ctor<__traits<_Types...>>; \
\
public: \
using __base_type::__base_type; \
using __base_type::operator=; \
\
__move_constructor(const __move_constructor&) = default; \
move_constructor \
~__move_constructor() = default; \
__move_constructor& operator=(const __move_constructor&) = default; \
__move_constructor& operator=(__move_constructor&&) = default; \
}
_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
_Trait::_TriviallyAvailable,
__move_constructor(__move_constructor&& __that) = default;);
_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
_Trait::_Available,
__move_constructor(__move_constructor&& __that) noexcept(
__all<is_nothrow_move_constructible_v<_Types>...>::value)
: __move_constructor(__valueless_t{}) {
this->__generic_construct(*this, _VSTD::move(__that));
});
_LIBCPP_VARIANT_MOVE_CONSTRUCTOR(
_Trait::_Unavailable,
__move_constructor(__move_constructor&&) = delete;);
#undef _LIBCPP_VARIANT_MOVE_CONSTRUCTOR
template <class _Traits, _Trait = _Traits::__copy_constructible_trait>
class _LIBCPP_TEMPLATE_VIS __copy_constructor;
#define _LIBCPP_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, \
copy_constructor) \
template <class... _Types> \
class _LIBCPP_TEMPLATE_VIS __copy_constructor<__traits<_Types...>, \
copy_constructible_trait> \
: public __move_constructor<__traits<_Types...>> { \
using __base_type = __move_constructor<__traits<_Types...>>; \
\
public: \
using __base_type::__base_type; \
using __base_type::operator=; \
\
copy_constructor \
__copy_constructor(__copy_constructor&&) = default; \
~__copy_constructor() = default; \
__copy_constructor& operator=(const __copy_constructor&) = default; \
__copy_constructor& operator=(__copy_constructor&&) = default; \
}
_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
_Trait::_TriviallyAvailable,
__copy_constructor(const __copy_constructor& __that) = default;);
_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
_Trait::_Available,
__copy_constructor(const __copy_constructor& __that)
: __copy_constructor(__valueless_t{}) {
this->__generic_construct(*this, __that);
});
_LIBCPP_VARIANT_COPY_CONSTRUCTOR(
_Trait::_Unavailable,
__copy_constructor(const __copy_constructor&) = delete;);
#undef _LIBCPP_VARIANT_COPY_CONSTRUCTOR
template <class _Traits>
class _LIBCPP_TEMPLATE_VIS __assignment : public __copy_constructor<_Traits> {
using __base_type = __copy_constructor<_Traits>;
public:
using __base_type::__base_type;
using __base_type::operator=;
template <size_t _Ip, class... _Args>
inline _LIBCPP_INLINE_VISIBILITY
auto& __emplace(_Args&&... __args) {
this->__destroy();
auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
_VSTD::forward<_Args>(__args)...);
this->__index = _Ip;
return __res;
}
protected:
template <size_t _Ip, class _Tp, class _Arg>
inline _LIBCPP_INLINE_VISIBILITY
void __assign_alt(__alt<_Ip, _Tp>& __a, _Arg&& __arg) {
if (this->index() == _Ip) {
__a.__value = _VSTD::forward<_Arg>(__arg);
} else {
struct {
void operator()(true_type) const {
__this->__emplace<_Ip>(_VSTD::forward<_Arg>(__arg));
}
void operator()(false_type) const {
__this->__emplace<_Ip>(_Tp(_VSTD::forward<_Arg>(__arg)));
}
__assignment* __this;
_Arg&& __arg;
} __impl{this, _VSTD::forward<_Arg>(__arg)};
__impl(bool_constant<is_nothrow_constructible_v<_Tp, _Arg> ||
!is_nothrow_move_constructible_v<_Tp>>{});
}
}
template <class _That>
inline _LIBCPP_INLINE_VISIBILITY
void __generic_assign(_That&& __that) {
if (this->valueless_by_exception() && __that.valueless_by_exception()) {
// do nothing.
} else if (__that.valueless_by_exception()) {
this->__destroy();
} else {
__visitation::__base::__visit_alt_at(
__that.index(),
[this](auto& __this_alt, auto&& __that_alt) {
this->__assign_alt(
__this_alt,
_VSTD::forward<decltype(__that_alt)>(__that_alt).__value);
},
*this, _VSTD::forward<_That>(__that));
}
}
};
template <class _Traits, _Trait = _Traits::__move_assignable_trait>
class _LIBCPP_TEMPLATE_VIS __move_assignment;
#define _LIBCPP_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, \
move_assignment) \
template <class... _Types> \
class _LIBCPP_TEMPLATE_VIS __move_assignment<__traits<_Types...>, \
move_assignable_trait> \
: public __assignment<__traits<_Types...>> { \
using __base_type = __assignment<__traits<_Types...>>; \
\
public: \
using __base_type::__base_type; \
using __base_type::operator=; \
\
__move_assignment(const __move_assignment&) = default; \
__move_assignment(__move_assignment&&) = default; \
~__move_assignment() = default; \
__move_assignment& operator=(const __move_assignment&) = default; \
move_assignment \
}
_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
_Trait::_TriviallyAvailable,
__move_assignment& operator=(__move_assignment&& __that) = default;);
_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
_Trait::_Available,
__move_assignment& operator=(__move_assignment&& __that) noexcept(
__all<(is_nothrow_move_constructible_v<_Types> &&
is_nothrow_move_assignable_v<_Types>)...>::value) {
this->__generic_assign(_VSTD::move(__that));
return *this;
});
_LIBCPP_VARIANT_MOVE_ASSIGNMENT(
_Trait::_Unavailable,
__move_assignment& operator=(__move_assignment&&) = delete;);
#undef _LIBCPP_VARIANT_MOVE_ASSIGNMENT
template <class _Traits, _Trait = _Traits::__copy_assignable_trait>
class _LIBCPP_TEMPLATE_VIS __copy_assignment;
#define _LIBCPP_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, \
copy_assignment) \
template <class... _Types> \
class _LIBCPP_TEMPLATE_VIS __copy_assignment<__traits<_Types...>, \
copy_assignable_trait> \
: public __move_assignment<__traits<_Types...>> { \
using __base_type = __move_assignment<__traits<_Types...>>; \
\
public: \
using __base_type::__base_type; \
using __base_type::operator=; \
\
__copy_assignment(const __copy_assignment&) = default; \
__copy_assignment(__copy_assignment&&) = default; \
~__copy_assignment() = default; \
copy_assignment \
__copy_assignment& operator=(__copy_assignment&&) = default; \
}
_LIBCPP_VARIANT_COPY_ASSIGNMENT(
_Trait::_TriviallyAvailable,
__copy_assignment& operator=(const __copy_assignment& __that) = default;);
_LIBCPP_VARIANT_COPY_ASSIGNMENT(
_Trait::_Available,
__copy_assignment& operator=(const __copy_assignment& __that) {
this->__generic_assign(__that);
return *this;
});
_LIBCPP_VARIANT_COPY_ASSIGNMENT(
_Trait::_Unavailable,
__copy_assignment& operator=(const __copy_assignment&) = delete;);
#undef _LIBCPP_VARIANT_COPY_ASSIGNMENT
template <class... _Types>
class _LIBCPP_TEMPLATE_VIS __impl
: public __copy_assignment<__traits<_Types...>> {
using __base_type = __copy_assignment<__traits<_Types...>>;
public:
using __base_type::__base_type;
using __base_type::operator=;
template <size_t _Ip, class _Arg>
inline _LIBCPP_INLINE_VISIBILITY
void __assign(_Arg&& __arg) {
this->__assign_alt(__access::__base::__get_alt<_Ip>(*this),
_VSTD::forward<_Arg>(__arg));
}
inline _LIBCPP_INLINE_VISIBILITY
void __swap(__impl& __that) {
if (this->valueless_by_exception() && __that.valueless_by_exception()) {
// do nothing.
} else if (this->index() == __that.index()) {
__visitation::__base::__visit_alt_at(
this->index(),
[](auto& __this_alt, auto& __that_alt) {
using _VSTD::swap;
swap(__this_alt.__value, __that_alt.__value);
},
*this,
__that);
} else {
__impl* __lhs = this;
__impl* __rhs = _VSTD::addressof(__that);
if (__lhs->__move_nothrow() && !__rhs->__move_nothrow()) {
_VSTD::swap(__lhs, __rhs);
}
__impl __tmp(_VSTD::move(*__rhs));
#ifndef _LIBCPP_NO_EXCEPTIONS
if constexpr (__all<is_nothrow_move_constructible_v<_Types>...>::value) {
this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
} else {
// EXTENSION: When the move construction of `__lhs` into `__rhs` throws
// and `__tmp` is nothrow move constructible then we move `__tmp` back
// into `__rhs` and provide the strong exception safety guarantee.
try {
this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
} catch (...) {
if (__tmp.__move_nothrow()) {
this->__generic_construct(*__rhs, _VSTD::move(__tmp));
}
throw;
}
}
#else
// this isn't consolidated with the `if constexpr` branch above due to
// `throw` being ill-formed with exceptions disabled even when discarded.
this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
#endif
this->__generic_construct(*__lhs, _VSTD::move(__tmp));
}
}
private:
inline _LIBCPP_INLINE_VISIBILITY
bool __move_nothrow() const {
constexpr bool __results[] = {is_nothrow_move_constructible_v<_Types>...};
return this->valueless_by_exception() || __results[this->index()];
}
};
struct __no_narrowing_check {
template <class _Dest, class _Source>
using _Apply = __identity<_Dest>;
};
struct __narrowing_check {
template <class _Dest>
static auto __test_impl(_Dest (&&)[1]) -> __identity<_Dest>;
template <class _Dest, class _Source>
using _Apply _LIBCPP_NODEBUG = decltype(__test_impl<_Dest>({declval<_Source>()}));
};
template <class _Dest, class _Source>
using __check_for_narrowing _LIBCPP_NODEBUG =
typename _If<
#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
false &&
#endif
is_arithmetic<_Dest>::value,
__narrowing_check,
__no_narrowing_check
>::template _Apply<_Dest, _Source>;
template <class _Tp, size_t _Idx>
struct __overload {
template <class _Up>
auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
};
template <class _Tp, size_t>
struct __overload_bool {
template <class _Up, class _Ap = __uncvref_t<_Up>>
auto operator()(bool, _Up&&) const
-> enable_if_t<is_same_v<_Ap, bool>, __identity<_Tp>>;
};
template <size_t _Idx>
struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
template <size_t _Idx>
struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
template <size_t _Idx>
struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
template <size_t _Idx>
struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
template <class ..._Bases>
struct __all_overloads : _Bases... {
void operator()() const;
using _Bases::operator()...;
};
// Change metaprogramming to satisfy MSVC. It doesn't like expression
// __overload<_Types, _Idx>... when _Types comes from using parameter but
// _Idx comes from structure template parameter. So I made them both
// structure template parameters.
template <class _IdxSeq, class ..._Types>
struct __make_overloads_imp;
template <size_t ..._Idx, class ..._Types>
struct __make_overloads_imp<__tuple_indices<_Idx...>, _Types... > {
using _Apply _LIBCPP_NODEBUG = __all_overloads<__overload<_Types, _Idx>...>;
};
template <class ..._Types>
using _MakeOverloads _LIBCPP_NODEBUG = typename __make_overloads_imp<
__make_indices_imp<_Sizeof<_Types...>::value, 0>, _Types...>::_Apply;
template <class _Tp, class... _Types>
using __best_match_t =
typename invoke_result_t<_MakeOverloads<_Types...>, _Tp, _Tp>::type;
} // __variant_detail
template <class... _Types>
class _LIBCPP_TEMPLATE_VIS variant
: private __sfinae_ctor_base<
__all<is_copy_constructible_v<_Types>...>::value,
__all<is_move_constructible_v<_Types>...>::value>,
private __sfinae_assign_base<
__all<(is_copy_constructible_v<_Types> &&
is_copy_assignable_v<_Types>)...>::value,
__all<(is_move_constructible_v<_Types> &&
is_move_assignable_v<_Types>)...>::value> {
static_assert(0 < sizeof...(_Types),
"variant must consist of at least one alternative.");
static_assert(__all<!is_array_v<_Types>...>::value,
"variant can not have an array type as an alternative.");
static_assert(__all<!is_reference_v<_Types>...>::value,
"variant can not have a reference type as an alternative.");
static_assert(__all<!is_void_v<_Types>...>::value,
"variant can not have a void type as an alternative.");
using __first_type = variant_alternative_t<0, variant>;
public:
template <bool _Dummy = true,
enable_if_t<__dependent_type<is_default_constructible<__first_type>,
_Dummy>::value,
int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)
: __impl(in_place_index<0>) {}
variant(const variant&) = default;
variant(variant&&) = default;
template <
class _Arg,
enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
enable_if_t<!__is_inplace_type<__uncvref_t<_Arg>>::value, int> = 0,
enable_if_t<!__is_inplace_index<__uncvref_t<_Arg>>::value, int> = 0,
class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
size_t _Ip =
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
enable_if_t<is_constructible_v<_Tp, _Arg>, int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
constexpr variant(_Arg&& __arg) noexcept(
is_nothrow_constructible_v<_Tp, _Arg>)
: __impl(in_place_index<_Ip>, _VSTD::forward<_Arg>(__arg)) {}
template <size_t _Ip, class... _Args,
class = enable_if_t<(_Ip < sizeof...(_Types)), int>,
class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
explicit constexpr variant(
in_place_index_t<_Ip>,
_Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
: __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
template <
size_t _Ip,
class _Up,
class... _Args,
enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
explicit constexpr variant(
in_place_index_t<_Ip>,
initializer_list<_Up> __il,
_Args&&... __args) noexcept(
is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
: __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
template <
class _Tp,
class... _Args,
size_t _Ip =
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
explicit constexpr variant(in_place_type_t<_Tp>, _Args&&... __args) noexcept(
is_nothrow_constructible_v<_Tp, _Args...>)
: __impl(in_place_index<_Ip>, _VSTD::forward<_Args>(__args)...) {}
template <
class _Tp,
class _Up,
class... _Args,
size_t _Ip =
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
explicit constexpr variant(
in_place_type_t<_Tp>,
initializer_list<_Up> __il,
_Args&&... __args) noexcept(
is_nothrow_constructible_v<_Tp, initializer_list< _Up>&, _Args...>)
: __impl(in_place_index<_Ip>, __il, _VSTD::forward<_Args>(__args)...) {}
~variant() = default;
variant& operator=(const variant&) = default;
variant& operator=(variant&&) = default;
template <
class _Arg,
enable_if_t<!is_same_v<__uncvref_t<_Arg>, variant>, int> = 0,
class _Tp = __variant_detail::__best_match_t<_Arg, _Types...>,
size_t _Ip =
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
enable_if_t<is_assignable_v<_Tp&, _Arg> && is_constructible_v<_Tp, _Arg>,
int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
variant& operator=(_Arg&& __arg) noexcept(
is_nothrow_assignable_v<_Tp&, _Arg> &&
is_nothrow_constructible_v<_Tp, _Arg>) {
__impl.template __assign<_Ip>(_VSTD::forward<_Arg>(__arg));
return *this;
}
template <
size_t _Ip,
class... _Args,
enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
_Tp& emplace(_Args&&... __args) {
return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
}
template <
size_t _Ip,
class _Up,
class... _Args,
enable_if_t<(_Ip < sizeof...(_Types)), int> = 0,
class _Tp = variant_alternative_t<_Ip, variant<_Types...>>,
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
_Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
}
template <
class _Tp,
class... _Args,
size_t _Ip =
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
_Tp& emplace(_Args&&... __args) {
return __impl.template __emplace<_Ip>(_VSTD::forward<_Args>(__args)...);
}
template <
class _Tp,
class _Up,
class... _Args,
size_t _Ip =
__find_detail::__find_unambiguous_index_sfinae<_Tp, _Types...>::value,
enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
_Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
return __impl.template __emplace<_Ip>(__il, _VSTD::forward<_Args>(__args)...);
}
inline _LIBCPP_INLINE_VISIBILITY
constexpr bool valueless_by_exception() const noexcept {
return __impl.valueless_by_exception();
}
inline _LIBCPP_INLINE_VISIBILITY
constexpr size_t index() const noexcept { return __impl.index(); }
template <
bool _Dummy = true,
enable_if_t<
__all<(
__dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
__dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,
int> = 0>
inline _LIBCPP_INLINE_VISIBILITY
void swap(variant& __that) noexcept(
__all<(is_nothrow_move_constructible_v<_Types> &&
is_nothrow_swappable_v<_Types>)...>::value) {
__impl.__swap(__that.__impl);
}
private:
__variant_detail::__impl<_Types...> __impl;
friend struct __variant_detail::__access::__variant;
friend struct __variant_detail::__visitation::__variant;
};
template <size_t _Ip, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr bool __holds_alternative(const variant<_Types...>& __v) noexcept {
return __v.index() == _Ip;
}
template <class _Tp, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
return __holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
template <size_t _Ip, class _Vp>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
constexpr auto&& __generic_get(_Vp&& __v) {
using __variant_detail::__access::__variant;
if (!__holds_alternative<_Ip>(__v)) {
__throw_bad_variant_access();
}
return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
}
template <size_t _Ip, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
variant<_Types...>& __v) {
static_assert(_Ip < sizeof...(_Types));
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
return __generic_get<_Ip>(__v);
}
template <size_t _Ip, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
variant<_Types...>&& __v) {
static_assert(_Ip < sizeof...(_Types));
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
return __generic_get<_Ip>(_VSTD::move(__v));
}
template <size_t _Ip, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
const variant<_Types...>& __v) {
static_assert(_Ip < sizeof...(_Types));
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
return __generic_get<_Ip>(__v);
}
template <size_t _Ip, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
const variant<_Types...>&& __v) {
static_assert(_Ip < sizeof...(_Types));
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
return __generic_get<_Ip>(_VSTD::move(__v));
}
template <class _Tp, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
constexpr _Tp& get(variant<_Types...>& __v) {
static_assert(!is_void_v<_Tp>);
return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
template <class _Tp, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
constexpr _Tp&& get(variant<_Types...>&& __v) {
static_assert(!is_void_v<_Tp>);
return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
_VSTD::move(__v));
}
template <class _Tp, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
constexpr const _Tp& get(const variant<_Types...>& __v) {
static_assert(!is_void_v<_Tp>);
return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
template <class _Tp, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
constexpr const _Tp&& get(const variant<_Types...>&& __v) {
static_assert(!is_void_v<_Tp>);
return _VSTD::get<__find_exactly_one_t<_Tp, _Types...>::value>(
_VSTD::move(__v));
}
template <size_t _Ip, class _Vp>
inline _LIBCPP_INLINE_VISIBILITY
constexpr auto* __generic_get_if(_Vp* __v) noexcept {
using __variant_detail::__access::__variant;
return __v && __holds_alternative<_Ip>(*__v)
? _VSTD::addressof(__variant::__get_alt<_Ip>(*__v).__value)
: nullptr;
}
template <size_t _Ip, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
get_if(variant<_Types...>* __v) noexcept {
static_assert(_Ip < sizeof...(_Types));
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
return __generic_get_if<_Ip>(__v);
}
template <size_t _Ip, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
get_if(const variant<_Types...>* __v) noexcept {
static_assert(_Ip < sizeof...(_Types));
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
return __generic_get_if<_Ip>(__v);
}
template <class _Tp, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr add_pointer_t<_Tp>
get_if(variant<_Types...>* __v) noexcept {
static_assert(!is_void_v<_Tp>);
return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
template <class _Tp, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr add_pointer_t<const _Tp>
get_if(const variant<_Types...>* __v) noexcept {
static_assert(!is_void_v<_Tp>);
return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
template <class _Operator>
struct __convert_to_bool {
template <class _T1, class _T2>
_LIBCPP_INLINE_VISIBILITY constexpr bool operator()(_T1 && __t1, _T2&& __t2) const {
static_assert(is_convertible<decltype(_Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2))), bool>::value,
"the relational operator does not return a type which is implicitly convertible to bool");
return _Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
}
};
template <class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr bool operator==(const variant<_Types...>& __lhs,
const variant<_Types...>& __rhs) {
using __variant_detail::__visitation::__variant;
if (__lhs.index() != __rhs.index()) return false;
if (__lhs.valueless_by_exception()) return true;
return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
}
template <class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr bool operator!=(const variant<_Types...>& __lhs,
const variant<_Types...>& __rhs) {
using __variant_detail::__visitation::__variant;
if (__lhs.index() != __rhs.index()) return true;
if (__lhs.valueless_by_exception()) return false;
return __variant::__visit_value_at(
__lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
}
template <class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr bool operator<(const variant<_Types...>& __lhs,
const variant<_Types...>& __rhs) {
using __variant_detail::__visitation::__variant;
if (__rhs.valueless_by_exception()) return false;
if (__lhs.valueless_by_exception()) return true;
if (__lhs.index() < __rhs.index()) return true;
if (__lhs.index() > __rhs.index()) return false;
return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
}
template <class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr bool operator>(const variant<_Types...>& __lhs,
const variant<_Types...>& __rhs) {
using __variant_detail::__visitation::__variant;
if (__lhs.valueless_by_exception()) return false;
if (__rhs.valueless_by_exception()) return true;
if (__lhs.index() > __rhs.index()) return true;
if (__lhs.index() < __rhs.index()) return false;
return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
}
template <class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr bool operator<=(const variant<_Types...>& __lhs,
const variant<_Types...>& __rhs) {
using __variant_detail::__visitation::__variant;
if (__lhs.valueless_by_exception()) return true;
if (__rhs.valueless_by_exception()) return false;
if (__lhs.index() < __rhs.index()) return true;
if (__lhs.index() > __rhs.index()) return false;
return __variant::__visit_value_at(
__lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
}
template <class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr bool operator>=(const variant<_Types...>& __lhs,
const variant<_Types...>& __rhs) {
using __variant_detail::__visitation::__variant;
if (__rhs.valueless_by_exception()) return true;
if (__lhs.valueless_by_exception()) return false;
if (__lhs.index() > __rhs.index()) return true;
if (__lhs.index() < __rhs.index()) return false;
return __variant::__visit_value_at(
__lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
}
template <class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr void
__throw_if_valueless(_Vs&&... __vs) {
const bool __valueless =
(... || _VSTD::__as_variant(__vs).valueless_by_exception());
if (__valueless) {
__throw_bad_variant_access();
}
}
template <
class _Visitor, class... _Vs,
typename = void_t<decltype(_VSTD::__as_variant(declval<_Vs>()))...> >
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr
decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
using __variant_detail::__visitation::__variant;
_VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
return __variant::__visit_value(_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Vs>(__vs)...);
}
#if _LIBCPP_STD_VER > 17
template <
class _Rp, class _Visitor, class... _Vs,
typename = void_t<decltype(_VSTD::__as_variant(declval<_Vs>()))...> >
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Rp
visit(_Visitor&& __visitor, _Vs&&... __vs) {
using __variant_detail::__visitation::__variant;
_VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
return __variant::__visit_value<_Rp>(_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Vs>(__vs)...);
}
#endif
template <class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
auto swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
noexcept(noexcept(__lhs.swap(__rhs)))
-> decltype( __lhs.swap(__rhs))
{ return __lhs.swap(__rhs); }
template <class... _Types>
struct _LIBCPP_TEMPLATE_VIS hash<
__enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
using argument_type = variant<_Types...>;
using result_type = size_t;
inline _LIBCPP_INLINE_VISIBILITY
result_type operator()(const argument_type& __v) const {
using __variant_detail::__visitation::__variant;
size_t __res =
__v.valueless_by_exception()
? 299792458 // Random value chosen by the universe upon creation
: __variant::__visit_alt(
[](const auto& __alt) {
using __alt_type = __uncvref_t<decltype(__alt)>;
using __value_type = remove_const_t<
typename __alt_type::__value_type>;
return hash<__value_type>{}(__alt.__value);
},
__v);
return __hash_combine(__res, hash<size_t>{}(__v.index()));
}
};
// __unchecked_get is the same as std::get, except, it is UB to use it with the wrong
// type whereas std::get will throw or returning nullptr. This makes it faster than
// std::get.
template <size_t _Ip, class _Vp>
inline _LIBCPP_INLINE_VISIBILITY
constexpr auto&& __unchecked_get(_Vp&& __v) noexcept {
using __variant_detail::__access::__variant;
return __variant::__get_alt<_Ip>(_VSTD::forward<_Vp>(__v)).__value;
}
template <class _Tp, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr auto&& __unchecked_get(const variant<_Types...>& __v) noexcept {
return __unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
template <class _Tp, class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr auto&& __unchecked_get(variant<_Types...>& __v) noexcept {
return __unchecked_get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
#endif // _LIBCPP_STD_VER > 14
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
#endif // _LIBCPP_VARIANT
|