forked from jacobdufault/cquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variant.hpp
2457 lines (2015 loc) · 83.6 KB
/
variant.hpp
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
// MPark.Variant
//
// Copyright Michael Park, 2015-2017
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#ifndef MPARK_VARIANT_HPP
#define MPARK_VARIANT_HPP
/*
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&);
variant(variant&&) noexcept(see below);
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&);
variant& operator=(variant&&) noexcept(see below);
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<I, variant>& emplace(Args&&...);
template <size_t I, class U, class... Args>
variant_alternative<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>
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...>>;
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&&...);
// 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 <cstddef>
#include <exception>
#include <functional>
#include <initializer_list>
#include <new>
#include <type_traits>
#include <utility>
// MPark.Variant
//
// Copyright Michael Park, 2015-2017
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#ifndef MPARK_CONFIG_HPP
#define MPARK_CONFIG_HPP
// MSVC 2015 Update 3.
#if __cplusplus < 201103L && (!defined(_MSC_VER) || _MSC_FULL_VER < 190024210)
#error "MPark.Variant requires C++11 support."
#endif
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
#ifndef __has_include
#define __has_include(x) 0
#endif
#ifndef __has_feature
#define __has_feature(x) 0
#endif
#if __has_builtin(__builtin_addressof) || \
(defined(__GNUC__) && __GNUC__ >= 7) || defined(_MSC_VER)
#define MPARK_BUILTIN_ADDRESSOF
#endif
#if __has_builtin(__builtin_unreachable)
#define MPARK_BUILTIN_UNREACHABLE
#endif
#if __has_builtin(__type_pack_element)
#define MPARK_TYPE_PACK_ELEMENT
#endif
#if defined(__cpp_constexpr) && __cpp_constexpr >= 201304
#define MPARK_CPP14_CONSTEXPR
#endif
#if __has_feature(cxx_exceptions) || defined(__cpp_exceptions) || \
(defined(_MSC_VER) && defined(_CPPUNWIND))
#define MPARK_EXCEPTIONS
#endif
#if defined(__cpp_generic_lambdas) || defined(_MSC_VER)
#define MPARK_GENERIC_LAMBDAS
#endif
#if defined(__cpp_lib_integer_sequence)
#define MPARK_INTEGER_SEQUENCE
#endif
#if defined(__cpp_return_type_deduction) || defined(_MSC_VER)
#define MPARK_RETURN_TYPE_DEDUCTION
#endif
#if defined(__cpp_lib_transparent_operators) || defined(_MSC_VER)
#define MPARK_TRANSPARENT_OPERATORS
#endif
#if defined(__cpp_variable_templates) || defined(_MSC_VER)
#define MPARK_VARIABLE_TEMPLATES
#endif
#if !defined(__GLIBCXX__) || __has_include(<codecvt>) // >= libstdc++-5
#define MPARK_TRIVIALITY_TYPE_TRAITS
#endif
#endif // MPARK_CONFIG_HPP
// MPark.Variant
//
// Copyright Michael Park, 2015-2017
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#ifndef MPARK_IN_PLACE_HPP
#define MPARK_IN_PLACE_HPP
#include <cstddef>
namespace mpark {
struct in_place_t { explicit in_place_t() = default; };
template <std::size_t I>
struct in_place_index_t { explicit in_place_index_t() = default; };
template <typename T>
struct in_place_type_t { explicit in_place_type_t() = default; };
#ifdef MPARK_VARIABLE_TEMPLATES
constexpr in_place_t in_place{};
template <std::size_t I> constexpr in_place_index_t<I> in_place_index{};
template <typename T> constexpr in_place_type_t<T> in_place_type{};
#endif
} // namespace mpark
#endif // MPARK_IN_PLACE_HPP
// MPark.Variant
//
// Copyright Michael Park, 2015-2017
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#ifndef MPARK_LIB_HPP
#define MPARK_LIB_HPP
#include <memory>
#include <functional>
#include <type_traits>
#include <utility>
#define RETURN(...) \
noexcept(noexcept(__VA_ARGS__)) -> decltype(__VA_ARGS__) { \
return __VA_ARGS__; \
}
namespace mpark {
namespace lib {
template <typename T>
struct identity { using type = T; };
inline namespace cpp14 {
template <typename T, std::size_t N>
struct array {
constexpr const T &operator[](std::size_t index) const {
return data[index];
}
T data[N == 0 ? 1 : N];
};
template <typename T>
using add_pointer_t = typename std::add_pointer<T>::type;
template <typename... Ts>
using common_type_t = typename std::common_type<Ts...>::type;
template <typename T>
using decay_t = typename std::decay<T>::type;
template <bool B, typename T = void>
using enable_if_t = typename std::enable_if<B, T>::type;
template <typename T>
using remove_const_t = typename std::remove_const<T>::type;
template <typename T>
using remove_reference_t = typename std::remove_reference<T>::type;
template <typename T>
inline constexpr T &&forward(remove_reference_t<T> &t) noexcept {
return static_cast<T &&>(t);
}
template <typename T>
inline constexpr T &&forward(remove_reference_t<T> &&t) noexcept {
static_assert(!std::is_lvalue_reference<T>::value,
"can not forward an rvalue as an lvalue");
return static_cast<T &&>(t);
}
template <typename T>
inline constexpr remove_reference_t<T> &&move(T &&t) noexcept {
return static_cast<remove_reference_t<T> &&>(t);
}
#ifdef MPARK_INTEGER_SEQUENCE
using std::integer_sequence;
using std::index_sequence;
using std::make_index_sequence;
using std::index_sequence_for;
#else
template <typename T, T... Is>
struct integer_sequence {
using value_type = T;
static constexpr std::size_t size() noexcept { return sizeof...(Is); }
};
template <std::size_t... Is>
using index_sequence = integer_sequence<std::size_t, Is...>;
template <typename Lhs, typename Rhs>
struct make_index_sequence_concat;
template <std::size_t... Lhs, std::size_t... Rhs>
struct make_index_sequence_concat<index_sequence<Lhs...>,
index_sequence<Rhs...>>
: identity<index_sequence<Lhs..., (sizeof...(Lhs) + Rhs)...>> {};
template <std::size_t N>
struct make_index_sequence_impl;
template <std::size_t N>
using make_index_sequence = typename make_index_sequence_impl<N>::type;
template <std::size_t N>
struct make_index_sequence_impl
: make_index_sequence_concat<make_index_sequence<N / 2>,
make_index_sequence<N - (N / 2)>> {};
template <>
struct make_index_sequence_impl<0> : identity<index_sequence<>> {};
template <>
struct make_index_sequence_impl<1> : identity<index_sequence<0>> {};
template <typename... Ts>
using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
#endif
// <functional>
#ifdef MPARK_TRANSPARENT_OPERATORS
using equal_to = std::equal_to<>;
#else
struct equal_to {
template <typename Lhs, typename Rhs>
inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
RETURN(lib::forward<Lhs>(lhs) == lib::forward<Rhs>(rhs))
};
#endif
#ifdef MPARK_TRANSPARENT_OPERATORS
using not_equal_to = std::not_equal_to<>;
#else
struct not_equal_to {
template <typename Lhs, typename Rhs>
inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
RETURN(lib::forward<Lhs>(lhs) != lib::forward<Rhs>(rhs))
};
#endif
#ifdef MPARK_TRANSPARENT_OPERATORS
using less = std::less<>;
#else
struct less {
template <typename Lhs, typename Rhs>
inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
RETURN(lib::forward<Lhs>(lhs) < lib::forward<Rhs>(rhs))
};
#endif
#ifdef MPARK_TRANSPARENT_OPERATORS
using greater = std::greater<>;
#else
struct greater {
template <typename Lhs, typename Rhs>
inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
RETURN(lib::forward<Lhs>(lhs) > lib::forward<Rhs>(rhs))
};
#endif
#ifdef MPARK_TRANSPARENT_OPERATORS
using less_equal = std::less_equal<>;
#else
struct less_equal {
template <typename Lhs, typename Rhs>
inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
RETURN(lib::forward<Lhs>(lhs) <= lib::forward<Rhs>(rhs))
};
#endif
#ifdef MPARK_TRANSPARENT_OPERATORS
using greater_equal = std::greater_equal<>;
#else
struct greater_equal {
template <typename Lhs, typename Rhs>
inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
RETURN(lib::forward<Lhs>(lhs) >= lib::forward<Rhs>(rhs))
};
#endif
} // namespace cpp14
inline namespace cpp17 {
// <type_traits>
template <bool B>
using bool_constant = std::integral_constant<bool, B>;
template <typename...>
struct voider : identity<void> {};
template <typename... Ts>
using void_t = typename voider<Ts...>::type;
namespace detail {
namespace swappable {
using std::swap;
template <typename T>
struct is_swappable {
private:
template <typename U,
typename = decltype(swap(std::declval<U &>(),
std::declval<U &>()))>
inline static std::true_type test(int);
template <typename U>
inline static std::false_type test(...);
public:
static constexpr bool value = decltype(test<T>(0))::value;
};
template <typename T, bool = is_swappable<T>::value>
struct is_nothrow_swappable {
static constexpr bool value =
noexcept(swap(std::declval<T &>(), std::declval<T &>()));
};
template <typename T>
struct is_nothrow_swappable<T, false> : std::false_type {};
} // namespace swappable
} // namespace detail
using detail::swappable::is_swappable;
using detail::swappable::is_nothrow_swappable;
// <functional>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4100)
#endif
template <typename F, typename... As>
inline constexpr auto invoke(F &&f, As &&... as)
RETURN(lib::forward<F>(f)(lib::forward<As>(as)...))
#ifdef _MSC_VER
#pragma warning(pop)
#endif
template <typename B, typename T, typename D>
inline constexpr auto invoke(T B::*pmv, D &&d)
RETURN(lib::forward<D>(d).*pmv)
template <typename Pmv, typename Ptr>
inline constexpr auto invoke(Pmv pmv, Ptr &&ptr)
RETURN((*lib::forward<Ptr>(ptr)).*pmv)
template <typename B, typename T, typename D, typename... As>
inline constexpr auto invoke(T B::*pmf, D &&d, As &&... as)
RETURN((lib::forward<D>(d).*pmf)(lib::forward<As>(as)...))
template <typename Pmf, typename Ptr, typename... As>
inline constexpr auto invoke(Pmf pmf, Ptr &&ptr, As &&... as)
RETURN(((*lib::forward<Ptr>(ptr)).*pmf)(lib::forward<As>(as)...))
namespace detail {
template <typename Void, typename, typename...>
struct invoke_result {};
template <typename F, typename... Args>
struct invoke_result<void_t<decltype(lib::invoke(
std::declval<F>(), std::declval<Args>()...))>,
F,
Args...>
: identity<decltype(
lib::invoke(std::declval<F>(), std::declval<Args>()...))> {};
} // namespace detail
template <typename F, typename... Args>
using invoke_result = detail::invoke_result<void, F, Args...>;
template <typename F, typename... Args>
using invoke_result_t = typename invoke_result<F, Args...>::type;
namespace detail {
template <typename Void, typename, typename...>
struct is_invocable : std::false_type {};
template <typename F, typename... Args>
struct is_invocable<void_t<invoke_result_t<F, Args...>>, F, Args...>
: std::true_type {};
template <typename Void, typename, typename, typename...>
struct is_invocable_r : std::false_type {};
template <typename R, typename F, typename... Args>
struct is_invocable_r<void_t<invoke_result_t<F, Args...>>,
R,
F,
Args...>
: std::is_convertible<invoke_result_t<F, Args...>, R> {};
} // namespace detail
template <typename F, typename... Args>
using is_invocable = detail::is_invocable<void, F, Args...>;
template <typename R, typename F, typename... Args>
using is_invocable_r = detail::is_invocable_r<void, R, F, Args...>;
// <memory>
#ifdef MPARK_BUILTIN_ADDRESSOF
template <typename T>
inline constexpr T *addressof(T &arg) {
return __builtin_addressof(arg);
}
#else
namespace detail {
namespace has_addressof_impl {
struct fail;
template <typename T>
inline fail operator&(T &&);
template <typename T>
inline static constexpr bool impl() {
return (std::is_class<T>::value || std::is_union<T>::value) &&
!std::is_same<decltype(&std::declval<T &>()), fail>::value;
}
} // namespace has_addressof_impl
template <typename T>
using has_addressof = bool_constant<has_addressof_impl::impl<T>()>;
template <typename T>
inline constexpr T *addressof(T &arg, std::true_type) {
return std::addressof(arg);
}
template <typename T>
inline constexpr T *addressof(T &arg, std::false_type) {
return &arg;
}
} // namespace detail
template <typename T>
inline constexpr T *addressof(T &arg) {
return detail::addressof(arg, detail::has_addressof<T>{});
}
#endif
template <typename T>
inline constexpr T *addressof(const T &&) = delete;
} // namespace cpp17
template <typename T>
struct remove_all_extents : identity<T> {};
template <typename T, std::size_t N>
struct remove_all_extents<array<T, N>> : remove_all_extents<T> {};
template <typename T>
using remove_all_extents_t = typename remove_all_extents<T>::type;
template <std::size_t N>
using size_constant = std::integral_constant<std::size_t, N>;
template <std::size_t I, typename T>
struct indexed_type : size_constant<I>, identity<T> {};
template <bool... Bs>
using all = std::is_same<integer_sequence<bool, true, Bs...>,
integer_sequence<bool, Bs..., true>>;
#ifdef MPARK_TYPE_PACK_ELEMENT
template <std::size_t I, typename... Ts>
using type_pack_element_t = __type_pack_element<I, Ts...>;
#else
template <std::size_t I, typename... Ts>
struct type_pack_element_impl {
private:
template <typename>
struct set;
template <std::size_t... Is>
struct set<index_sequence<Is...>> : indexed_type<Is, Ts>... {};
template <typename T>
inline static std::enable_if<true, T> impl(indexed_type<I, T>);
inline static std::enable_if<false> impl(...);
public:
using type = decltype(impl(set<index_sequence_for<Ts...>>{}));
};
template <std::size_t I, typename... Ts>
using type_pack_element = typename type_pack_element_impl<I, Ts...>::type;
template <std::size_t I, typename... Ts>
using type_pack_element_t = typename type_pack_element<I, Ts...>::type;
#endif
#ifdef MPARK_TRIVIALITY_TYPE_TRAITS
using std::is_trivially_copy_constructible;
using std::is_trivially_move_constructible;
using std::is_trivially_copy_assignable;
using std::is_trivially_move_assignable;
#else
template <typename T>
struct is_trivially_copy_constructible
: bool_constant<
std::is_copy_constructible<T>::value && __has_trivial_copy(T)> {};
template <typename T>
struct is_trivially_move_constructible : bool_constant<__is_trivial(T)> {};
template <typename T>
struct is_trivially_copy_assignable
: bool_constant<
std::is_copy_assignable<T>::value && __has_trivial_assign(T)> {};
template <typename T>
struct is_trivially_move_assignable : bool_constant<__is_trivial(T)> {};
#endif
template <typename T, bool>
struct dependent_type : T {};
template <typename Is, std::size_t J>
struct push_back;
template <typename Is, std::size_t J>
using push_back_t = typename push_back<Is, J>::type;
template <std::size_t... Is, std::size_t J>
struct push_back<index_sequence<Is...>, J> {
using type = index_sequence<Is..., J>;
};
} // namespace lib
} // namespace mpark
#undef RETURN
#endif // MPARK_LIB_HPP
namespace mpark {
#ifdef MPARK_RETURN_TYPE_DEDUCTION
#define AUTO auto
#define AUTO_RETURN(...) { return __VA_ARGS__; }
#define AUTO_REFREF auto &&
#define AUTO_REFREF_RETURN(...) { return __VA_ARGS__; }
#define DECLTYPE_AUTO decltype(auto)
#define DECLTYPE_AUTO_RETURN(...) { return __VA_ARGS__; }
#else
#define AUTO auto
#define AUTO_RETURN(...) \
-> lib::decay_t<decltype(__VA_ARGS__)> { return __VA_ARGS__; }
#define AUTO_REFREF auto
#define AUTO_REFREF_RETURN(...) \
-> decltype((__VA_ARGS__)) { \
static_assert(std::is_reference<decltype((__VA_ARGS__))>::value, ""); \
return __VA_ARGS__; \
}
#define DECLTYPE_AUTO auto
#define DECLTYPE_AUTO_RETURN(...) \
-> decltype(__VA_ARGS__) { return __VA_ARGS__; }
#endif
class bad_variant_access : public std::exception {
public:
virtual const char *what() const noexcept { return "bad_variant_access"; }
};
[[noreturn]] inline void throw_bad_variant_access() {
#ifdef MPARK_EXCEPTIONS
throw bad_variant_access{};
#else
std::terminate();
#ifdef MPARK_BUILTIN_UNREACHABLE
__builtin_unreachable();
#endif
#endif
}
template <typename... Ts>
class variant;
template <typename T>
struct variant_size;
#ifdef MPARK_VARIABLE_TEMPLATES
template <typename T>
constexpr std::size_t variant_size_v = variant_size<T>::value;
#endif
template <typename T>
struct variant_size<const T> : variant_size<T> {};
template <typename T>
struct variant_size<volatile T> : variant_size<T> {};
template <typename T>
struct variant_size<const volatile T> : variant_size<T> {};
template <typename... Ts>
struct variant_size<variant<Ts...>> : lib::size_constant<sizeof...(Ts)> {};
template <std::size_t I, typename T>
struct variant_alternative;
template <std::size_t I, typename T>
using variant_alternative_t = typename variant_alternative<I, T>::type;
template <std::size_t I, typename T>
struct variant_alternative<I, const T>
: std::add_const<variant_alternative_t<I, T>> {};
template <std::size_t I, typename T>
struct variant_alternative<I, volatile T>
: std::add_volatile<variant_alternative_t<I, T>> {};
template <std::size_t I, typename T>
struct variant_alternative<I, const volatile T>
: std::add_cv<variant_alternative_t<I, T>> {};
template <std::size_t I, typename... Ts>
struct variant_alternative<I, variant<Ts...>> {
static_assert(I < sizeof...(Ts),
"Index out of bounds in std::variant_alternative<>");
using type = lib::type_pack_element_t<I, Ts...>;
};
constexpr std::size_t variant_npos = static_cast<std::size_t>(-1);
namespace detail {
constexpr std::size_t not_found = static_cast<std::size_t>(-1);
constexpr std::size_t ambiguous = static_cast<std::size_t>(-2);
#ifdef MPARK_CPP14_CONSTEXPR
template <typename T, typename... Ts>
inline constexpr std::size_t find_index() {
constexpr lib::array<bool, sizeof...(Ts)> matches = {
{std::is_same<T, Ts>::value...}
};
std::size_t result = not_found;
for (std::size_t i = 0; i < sizeof...(Ts); ++i) {
if (matches[i]) {
if (result != not_found) {
return ambiguous;
}
result = i;
}
}
return result;
}
#else
inline constexpr std::size_t find_index_impl(std::size_t result,
std::size_t) {
return result;
}
template <typename... Bs>
inline constexpr std::size_t find_index_impl(std::size_t result,
std::size_t idx,
bool b,
Bs... bs) {
return b ? (result != not_found ? ambiguous
: find_index_impl(idx, idx + 1, bs...))
: find_index_impl(result, idx + 1, bs...);
}
template <typename T, typename... Ts>
inline constexpr std::size_t find_index() {
return find_index_impl(not_found, 0, std::is_same<T, Ts>::value...);
}
#endif
template <std::size_t I>
using find_index_sfinae_impl =
lib::enable_if_t<I != not_found && I != ambiguous,
lib::size_constant<I>>;
template <typename T, typename... Ts>
using find_index_sfinae = find_index_sfinae_impl<find_index<T, Ts...>()>;
template <std::size_t I>
struct find_index_checked_impl : lib::size_constant<I> {
static_assert(I != not_found, "the specified type is not found.");
static_assert(I != ambiguous, "the specified type is ambiguous.");
};
template <typename T, typename... Ts>
using find_index_checked = find_index_checked_impl<find_index<T, Ts...>()>;
struct valueless_t {};
enum class Trait { TriviallyAvailable, Available, Unavailable };
template <typename T,
template <typename> class IsTriviallyAvailable,
template <typename> class IsAvailable>
inline constexpr Trait trait() {
return IsTriviallyAvailable<T>::value
? Trait::TriviallyAvailable
: IsAvailable<T>::value ? Trait::Available
: Trait::Unavailable;
}
#ifdef MPARK_CPP14_CONSTEXPR
template <typename... Traits>
inline constexpr Trait common_trait(Traits... traits) {
Trait result = Trait::TriviallyAvailable;
for (Trait t : {traits...}) {
if (static_cast<int>(t) > static_cast<int>(result)) {
result = t;
}
}
return result;
}
#else
inline constexpr Trait common_trait_impl(Trait result) { return result; }
template <typename... Traits>
inline constexpr Trait common_trait_impl(Trait result,
Trait t,
Traits... ts) {
return static_cast<int>(t) > static_cast<int>(result)
? common_trait_impl(t, ts...)
: common_trait_impl(result, ts...);
}
template <typename... Traits>
inline constexpr Trait common_trait(Traits... ts) {
return common_trait_impl(Trait::TriviallyAvailable, ts...);
}
#endif
template <typename... Ts>
struct traits {
static constexpr Trait copy_constructible_trait =
common_trait(trait<Ts,
lib::is_trivially_copy_constructible,
std::is_copy_constructible>()...);
static constexpr Trait move_constructible_trait =
common_trait(trait<Ts,
lib::is_trivially_move_constructible,
std::is_move_constructible>()...);
static constexpr Trait copy_assignable_trait =
common_trait(copy_constructible_trait,
trait<Ts,
lib::is_trivially_copy_assignable,
std::is_copy_assignable>()...);
static constexpr Trait move_assignable_trait =
common_trait(move_constructible_trait,
trait<Ts,
lib::is_trivially_move_assignable,
std::is_move_assignable>()...);
static constexpr Trait destructible_trait =
common_trait(trait<Ts,
std::is_trivially_destructible,
std::is_destructible>()...);
};
namespace access {
struct recursive_union {
#ifdef MPARK_RETURN_TYPE_DEDUCTION
template <typename V>
inline static constexpr auto &&get_alt(V &&v, in_place_index_t<0>) {
return lib::forward<V>(v).head_;
}
template <typename V, std::size_t I>
inline static constexpr auto &&get_alt(V &&v, in_place_index_t<I>) {