-
Notifications
You must be signed in to change notification settings - Fork 28
/
variant.hpp
2743 lines (2193 loc) · 99.5 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
// Copyright 2016-2018 by Martin Moene
//
// https://github.com/martinmoene/variant-lite
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#ifndef NONSTD_VARIANT_LITE_HPP
#define NONSTD_VARIANT_LITE_HPP
#define variant_lite_MAJOR 2
#define variant_lite_MINOR 0
#define variant_lite_PATCH 0
#define variant_lite_VERSION variant_STRINGIFY(variant_lite_MAJOR) "." variant_STRINGIFY(variant_lite_MINOR) "." variant_STRINGIFY(variant_lite_PATCH)
#define variant_STRINGIFY( x ) variant_STRINGIFY_( x )
#define variant_STRINGIFY_( x ) #x
// variant-lite configuration:
#define variant_VARIANT_DEFAULT 0
#define variant_VARIANT_NONSTD 1
#define variant_VARIANT_STD 2
// tweak header support:
#ifdef __has_include
# if __has_include(<nonstd/variant.tweak.hpp>)
# include <nonstd/variant.tweak.hpp>
# endif
#define variant_HAVE_TWEAK_HEADER 1
#else
#define variant_HAVE_TWEAK_HEADER 0
//# pragma message("variant.hpp: Note: Tweak header not supported.")
#endif
// variant selection and configuration:
#ifndef variant_CONFIG_OMIT_VARIANT_SIZE_V_MACRO
# define variant_CONFIG_OMIT_VARIANT_SIZE_V_MACRO 0
#endif
#ifndef variant_CONFIG_OMIT_VARIANT_ALTERNATIVE_T_MACRO
# define variant_CONFIG_OMIT_VARIANT_ALTERNATIVE_T_MACRO 0
#endif
// Control presence of exception handling (try and auto discover):
#ifndef variant_CONFIG_NO_EXCEPTIONS
# if defined(_MSC_VER)
# include <cstddef> // for _HAS_EXCEPTIONS
# endif
# if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS)
# define variant_CONFIG_NO_EXCEPTIONS 0
# else
# define variant_CONFIG_NO_EXCEPTIONS 1
# endif
#endif
// C++ language version detection (C++23 is speculative):
// Note: VC14.0/1900 (VS2015) lacks too much from C++14.
#ifndef variant_CPLUSPLUS
# if defined(_MSVC_LANG ) && !defined(__clang__)
# define variant_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG )
# else
# define variant_CPLUSPLUS __cplusplus
# endif
#endif
#define variant_CPP98_OR_GREATER ( variant_CPLUSPLUS >= 199711L )
#define variant_CPP11_OR_GREATER ( variant_CPLUSPLUS >= 201103L )
#define variant_CPP11_OR_GREATER_ ( variant_CPLUSPLUS >= 201103L )
#define variant_CPP14_OR_GREATER ( variant_CPLUSPLUS >= 201402L )
#define variant_CPP17_OR_GREATER ( variant_CPLUSPLUS >= 201703L )
#define variant_CPP20_OR_GREATER ( variant_CPLUSPLUS >= 202002L )
#define variant_CPP23_OR_GREATER ( variant_CPLUSPLUS >= 202300L )
// Use C++17 std::variant if available and requested:
#if variant_CPP17_OR_GREATER && defined(__has_include )
# if __has_include( <variant> )
# define variant_HAVE_STD_VARIANT 1
# else
# define variant_HAVE_STD_VARIANT 0
# endif
#else
# define variant_HAVE_STD_VARIANT 0
#endif
#if !defined( variant_CONFIG_SELECT_VARIANT )
# define variant_CONFIG_SELECT_VARIANT ( variant_HAVE_STD_VARIANT ? variant_VARIANT_STD : variant_VARIANT_NONSTD )
#endif
#define variant_USES_STD_VARIANT ( (variant_CONFIG_SELECT_VARIANT == variant_VARIANT_STD) || ((variant_CONFIG_SELECT_VARIANT == variant_VARIANT_DEFAULT) && variant_HAVE_STD_VARIANT) )
//
// in_place: code duplicated in any-lite, expected-lite, optional-lite, value-ptr-lite, variant-lite:
//
#ifndef nonstd_lite_HAVE_IN_PLACE_TYPES
#define nonstd_lite_HAVE_IN_PLACE_TYPES 1
// C++17 std::in_place in <utility>:
#if variant_CPP17_OR_GREATER
#include <utility>
namespace nonstd {
using std::in_place;
using std::in_place_type;
using std::in_place_index;
using std::in_place_t;
using std::in_place_type_t;
using std::in_place_index_t;
#define nonstd_lite_in_place_t( T) std::in_place_t
#define nonstd_lite_in_place_type_t( T) std::in_place_type_t<T>
#define nonstd_lite_in_place_index_t(K) std::in_place_index_t<K>
#define nonstd_lite_in_place( T) std::in_place_t{}
#define nonstd_lite_in_place_type( T) std::in_place_type_t<T>{}
#define nonstd_lite_in_place_index(K) std::in_place_index_t<K>{}
} // namespace nonstd
#else // variant_CPP17_OR_GREATER
#include <cstddef>
namespace nonstd {
namespace detail {
template< class T >
struct in_place_type_tag {};
template< std::size_t K >
struct in_place_index_tag {};
} // namespace detail
struct in_place_t {};
template< class T >
inline in_place_t in_place( detail::in_place_type_tag<T> = detail::in_place_type_tag<T>() )
{
return in_place_t();
}
template< std::size_t K >
inline in_place_t in_place( detail::in_place_index_tag<K> = detail::in_place_index_tag<K>() )
{
return in_place_t();
}
template< class T >
inline in_place_t in_place_type( detail::in_place_type_tag<T> = detail::in_place_type_tag<T>() )
{
return in_place_t();
}
template< std::size_t K >
inline in_place_t in_place_index( detail::in_place_index_tag<K> = detail::in_place_index_tag<K>() )
{
return in_place_t();
}
// mimic templated typedef:
#define nonstd_lite_in_place_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag<T> )
#define nonstd_lite_in_place_type_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag<T> )
#define nonstd_lite_in_place_index_t(K) nonstd::in_place_t(&)( nonstd::detail::in_place_index_tag<K> )
#define nonstd_lite_in_place( T) nonstd::in_place_type<T>
#define nonstd_lite_in_place_type( T) nonstd::in_place_type<T>
#define nonstd_lite_in_place_index(K) nonstd::in_place_index<K>
} // namespace nonstd
#endif // variant_CPP17_OR_GREATER
#endif // nonstd_lite_HAVE_IN_PLACE_TYPES
// in_place_index-like disambiguation tag identical for all C++ versions:
namespace nonstd {
namespace variants {
namespace detail {
template< std::size_t K >
struct index_tag_t {};
template< std::size_t K >
inline void index_tag ( index_tag_t<K> = index_tag_t<K>() ) { }
#define variant_index_tag_t(K) void(&)( nonstd::variants::detail::index_tag_t<K> )
#define variant_index_tag(K) nonstd::variants::detail::index_tag<K>
} // namespace detail
} // namespace variants
} // namespace nonstd
//
// Use C++17 std::variant:
//
#if variant_USES_STD_VARIANT
#include <functional> // std::hash<>
#include <variant>
#if ! variant_CONFIG_OMIT_VARIANT_SIZE_V_MACRO
# define variant_size_V(T) nonstd::variant_size<T>::value
#endif
#if ! variant_CONFIG_OMIT_VARIANT_ALTERNATIVE_T_MACRO
# define variant_alternative_T(K,T) typename nonstd::variant_alternative<K,T >::type
#endif
namespace nonstd {
using std::variant;
using std::monostate;
using std::bad_variant_access;
using std::variant_size;
using std::variant_size_v;
using std::variant_alternative;
using std::variant_alternative_t;
using std::hash;
using std::visit;
using std::holds_alternative;
using std::get;
using std::get_if;
using std::operator==;
using std::operator!=;
using std::operator<;
using std::operator<=;
using std::operator>;
using std::operator>=;
using std::swap;
constexpr auto variant_npos = std::variant_npos;
}
#else // variant_USES_STD_VARIANT
#include <cstddef>
#include <limits>
#include <new>
#include <utility>
#if variant_CONFIG_NO_EXCEPTIONS
# include <cassert>
#else
# include <stdexcept>
#endif
// variant-lite type and visitor argument count configuration (script/generate_header.py):
#define variant_CONFIG_MAX_TYPE_COUNT 16
#define variant_CONFIG_MAX_VISITOR_ARG_COUNT 5
// variant-lite alignment configuration:
#ifndef variant_CONFIG_MAX_ALIGN_HACK
# define variant_CONFIG_MAX_ALIGN_HACK 0
#endif
#ifndef variant_CONFIG_ALIGN_AS
// no default, used in #if defined()
#endif
#ifndef variant_CONFIG_ALIGN_AS_FALLBACK
# define variant_CONFIG_ALIGN_AS_FALLBACK double
#endif
// half-open range [lo..hi):
#define variant_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) )
// Compiler versions:
//
// MSVC++ 6.0 _MSC_VER == 1200 variant_COMPILER_MSVC_VERSION == 60 (Visual Studio 6.0)
// MSVC++ 7.0 _MSC_VER == 1300 variant_COMPILER_MSVC_VERSION == 70 (Visual Studio .NET 2002)
// MSVC++ 7.1 _MSC_VER == 1310 variant_COMPILER_MSVC_VERSION == 71 (Visual Studio .NET 2003)
// MSVC++ 8.0 _MSC_VER == 1400 variant_COMPILER_MSVC_VERSION == 80 (Visual Studio 2005)
// MSVC++ 9.0 _MSC_VER == 1500 variant_COMPILER_MSVC_VERSION == 90 (Visual Studio 2008)
// MSVC++ 10.0 _MSC_VER == 1600 variant_COMPILER_MSVC_VERSION == 100 (Visual Studio 2010)
// MSVC++ 11.0 _MSC_VER == 1700 variant_COMPILER_MSVC_VERSION == 110 (Visual Studio 2012)
// MSVC++ 12.0 _MSC_VER == 1800 variant_COMPILER_MSVC_VERSION == 120 (Visual Studio 2013)
// MSVC++ 14.0 _MSC_VER == 1900 variant_COMPILER_MSVC_VERSION == 140 (Visual Studio 2015)
// MSVC++ 14.1 _MSC_VER >= 1910 variant_COMPILER_MSVC_VERSION == 141 (Visual Studio 2017)
// MSVC++ 14.2 _MSC_VER >= 1920 variant_COMPILER_MSVC_VERSION == 142 (Visual Studio 2019)
#if defined(_MSC_VER ) && !defined(__clang__)
# define variant_COMPILER_MSVC_VER (_MSC_VER )
# define variant_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) )
#else
# define variant_COMPILER_MSVC_VER 0
# define variant_COMPILER_MSVC_VERSION 0
#endif
#define variant_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) )
#if defined(__clang__)
# define variant_COMPILER_CLANG_VERSION variant_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
#else
# define variant_COMPILER_CLANG_VERSION 0
#endif
#if defined(__GNUC__) && !defined(__clang__)
# define variant_COMPILER_GNUC_VERSION variant_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
#else
# define variant_COMPILER_GNUC_VERSION 0
#endif
#if variant_BETWEEN( variant_COMPILER_MSVC_VER, 1300, 1900 )
# pragma warning( push )
# pragma warning( disable: 4345 ) // initialization behavior changed
#endif
// Presence of language and library features:
#define variant_HAVE( feature ) ( variant_HAVE_##feature )
#ifdef _HAS_CPP0X
# define variant_HAS_CPP0X _HAS_CPP0X
#else
# define variant_HAS_CPP0X 0
#endif
// Unless defined otherwise below, consider VC14 as C++11 for variant-lite:
#if variant_COMPILER_MSVC_VER >= 1900
# undef variant_CPP11_OR_GREATER
# define variant_CPP11_OR_GREATER 1
#endif
#define variant_CPP11_90 (variant_CPP11_OR_GREATER_ || variant_COMPILER_MSVC_VER >= 1500)
#define variant_CPP11_100 (variant_CPP11_OR_GREATER_ || variant_COMPILER_MSVC_VER >= 1600)
#define variant_CPP11_110 (variant_CPP11_OR_GREATER_ || variant_COMPILER_MSVC_VER >= 1700)
#define variant_CPP11_120 (variant_CPP11_OR_GREATER_ || variant_COMPILER_MSVC_VER >= 1800)
#define variant_CPP11_140 (variant_CPP11_OR_GREATER_ || variant_COMPILER_MSVC_VER >= 1900)
#define variant_CPP11_141 (variant_CPP11_OR_GREATER_ || variant_COMPILER_MSVC_VER >= 1910)
#define variant_CPP14_000 (variant_CPP14_OR_GREATER)
#define variant_CPP17_000 (variant_CPP17_OR_GREATER)
// Presence of C++11 language features:
#define variant_HAVE_CONSTEXPR_11 variant_CPP11_140
#define variant_HAVE_INITIALIZER_LIST variant_CPP11_120
#define variant_HAVE_NOEXCEPT variant_CPP11_140
#define variant_HAVE_NULLPTR variant_CPP11_100
#define variant_HAVE_OVERRIDE variant_CPP11_140
// Presence of C++14 language features:
#define variant_HAVE_CONSTEXPR_14 variant_CPP14_000
// Presence of C++17 language features:
// no flag
// Presence of C++ library features:
#define variant_HAVE_CONDITIONAL variant_CPP11_120
#define variant_HAVE_REMOVE_CV variant_CPP11_120
#define variant_HAVE_STD_ADD_POINTER variant_CPP11_100
#define variant_HAVE_TYPE_TRAITS variant_CPP11_90
#define variant_HAVE_ENABLE_IF variant_CPP11_100
#define variant_HAVE_IS_SAME variant_CPP11_100
#define variant_HAVE_TR1_TYPE_TRAITS (!! variant_COMPILER_GNUC_VERSION )
#define variant_HAVE_TR1_ADD_POINTER (!! variant_COMPILER_GNUC_VERSION || variant_CPP11_90)
// C++ feature usage:
#if variant_HAVE_CONSTEXPR_11
# define variant_constexpr constexpr
#else
# define variant_constexpr /*constexpr*/
#endif
#if variant_HAVE_CONSTEXPR_14
# define variant_constexpr14 constexpr
#else
# define variant_constexpr14 /*constexpr*/
#endif
#if variant_HAVE_NOEXCEPT
# define variant_noexcept noexcept
#else
# define variant_noexcept /*noexcept*/
#endif
#if variant_HAVE_NULLPTR
# define variant_nullptr nullptr
#else
# define variant_nullptr NULL
#endif
#if variant_HAVE_OVERRIDE
# define variant_override override
#else
# define variant_override /*override*/
#endif
// additional includes:
#if variant_CPP11_OR_GREATER
# include <functional> // std::hash
#endif
#if variant_HAVE_INITIALIZER_LIST
# include <initializer_list>
#endif
#if variant_HAVE_TYPE_TRAITS
# include <type_traits>
#elif variant_HAVE_TR1_TYPE_TRAITS
# include <tr1/type_traits>
#endif
//
// variant:
//
namespace nonstd { namespace variants {
// C++11 emulation:
namespace std11 {
#if variant_HAVE_STD_ADD_POINTER
using std::add_pointer;
#elif variant_HAVE_TR1_ADD_POINTER
using std::tr1::add_pointer;
#else
template< class T > struct remove_reference { typedef T type; };
template< class T > struct remove_reference<T&> { typedef T type; };
template< class T > struct add_pointer
{
typedef typename remove_reference<T>::type * type;
};
#endif // variant_HAVE_STD_ADD_POINTER
#if variant_HAVE_REMOVE_CV
using std::remove_cv;
#else
template< class T > struct remove_const { typedef T type; };
template< class T > struct remove_const<const T> { typedef T type; };
template< class T > struct remove_volatile { typedef T type; };
template< class T > struct remove_volatile<volatile T> { typedef T type; };
template< class T >
struct remove_cv
{
typedef typename remove_volatile<typename remove_const<T>::type>::type type;
};
#endif // variant_HAVE_REMOVE_CV
#if variant_HAVE_CONDITIONAL
using std::conditional;
#else
template< bool Cond, class Then, class Else >
struct conditional;
template< class Then, class Else >
struct conditional< true , Then, Else > { typedef Then type; };
template< class Then, class Else >
struct conditional< false, Then, Else > { typedef Else type; };
#endif // variant_HAVE_CONDITIONAL
#if variant_HAVE_ENABLE_IF
using std::enable_if;
#else
template< bool B, class T = void >
struct enable_if { };
template< class T >
struct enable_if< true, T > { typedef T type; };
#endif // variant_HAVE_ENABLE_IF
#if variant_HAVE_IS_SAME
using std::is_same;
#else
template< class T, class U >
struct is_same {
enum V { value = 0 } ;
};
template< class T >
struct is_same< T, T > {
enum V { value = 1 } ;
};
#endif // variant_HAVE_IS_SAME
} // namespace std11
// Method enabling
#if variant_CPP11_OR_GREATER
#define variant_REQUIRES_T(...) \
, typename std::enable_if< (__VA_ARGS__), int >::type = 0
#define variant_REQUIRES_R(R, ...) \
typename std::enable_if< (__VA_ARGS__), R>::type
#define variant_REQUIRES_A(...) \
, typename std::enable_if< (__VA_ARGS__), void*>::type = nullptr
#endif // variant_CPP11_OR_GREATER
#define variant_REQUIRES_0(...) \
template< bool B = (__VA_ARGS__), typename std11::enable_if<B, int>::type = 0 >
#define variant_REQUIRES_B(...) \
, bool B = (__VA_ARGS__), typename std11::enable_if<B, int>::type = 0
/// type traits C++17:
namespace std17 {
#if variant_CPP17_OR_GREATER
using std::is_swappable;
using std::is_nothrow_swappable;
#elif variant_CPP11_OR_GREATER
namespace detail {
using std::swap;
struct is_swappable
{
template< typename T, typename = decltype( swap( std::declval<T&>(), std::declval<T&>() ) ) >
static std::true_type test( int );
template< typename >
static std::false_type test(...);
};
struct is_nothrow_swappable
{
// wrap noexcept(epr) in separate function as work-around for VC140 (VS2015):
template< typename T >
static constexpr bool test()
{
return noexcept( swap( std::declval<T&>(), std::declval<T&>() ) );
}
template< typename T >
static auto test( int ) -> std::integral_constant<bool, test<T>()>{}
template< typename >
static std::false_type test(...);
};
} // namespace detail
// is [nothow] swappable:
template< typename T >
struct is_swappable : decltype( detail::is_swappable::test<T>(0) ){};
template< typename T >
struct is_nothrow_swappable : decltype( detail::is_nothrow_swappable::test<T>(0) ){};
#endif // variant_CPP17_OR_GREATER
} // namespace std17
// detail:
namespace detail {
// typelist:
#define variant_TL1( T1 ) detail::typelist< T1, detail::nulltype >
#define variant_TL2( T1, T2) detail::typelist< T1, variant_TL1( T2) >
#define variant_TL3( T1, T2, T3) detail::typelist< T1, variant_TL2( T2, T3) >
#define variant_TL4( T1, T2, T3, T4) detail::typelist< T1, variant_TL3( T2, T3, T4) >
#define variant_TL5( T1, T2, T3, T4, T5) detail::typelist< T1, variant_TL4( T2, T3, T4, T5) >
#define variant_TL6( T1, T2, T3, T4, T5, T6) detail::typelist< T1, variant_TL5( T2, T3, T4, T5, T6) >
#define variant_TL7( T1, T2, T3, T4, T5, T6, T7) detail::typelist< T1, variant_TL6( T2, T3, T4, T5, T6, T7) >
#define variant_TL8( T1, T2, T3, T4, T5, T6, T7, T8) detail::typelist< T1, variant_TL7( T2, T3, T4, T5, T6, T7, T8) >
#define variant_TL9( T1, T2, T3, T4, T5, T6, T7, T8, T9) detail::typelist< T1, variant_TL8( T2, T3, T4, T5, T6, T7, T8, T9) >
#define variant_TL10( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) detail::typelist< T1, variant_TL9( T2, T3, T4, T5, T6, T7, T8, T9, T10) >
#define variant_TL11( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) detail::typelist< T1, variant_TL10( T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) >
#define variant_TL12( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) detail::typelist< T1, variant_TL11( T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) >
#define variant_TL13( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) detail::typelist< T1, variant_TL12( T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) >
#define variant_TL14( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) detail::typelist< T1, variant_TL13( T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) >
#define variant_TL15( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) detail::typelist< T1, variant_TL14( T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) >
#define variant_TL16( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) detail::typelist< T1, variant_TL15( T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) >
// variant parameter unused type tags:
template< class T >
struct TX : T
{
inline TX<T> operator+ ( ) const { return TX<T>(); }
inline TX<T> operator- ( ) const { return TX<T>(); }
inline TX<T> operator! ( ) const { return TX<T>(); }
inline TX<T> operator~ ( ) const { return TX<T>(); }
inline TX<T>*operator& ( ) const { return variant_nullptr; }
template< class U > inline TX<T> operator* ( U const & ) const { return TX<T>(); }
template< class U > inline TX<T> operator/ ( U const & ) const { return TX<T>(); }
template< class U > inline TX<T> operator% ( U const & ) const { return TX<T>(); }
template< class U > inline TX<T> operator+ ( U const & ) const { return TX<T>(); }
template< class U > inline TX<T> operator- ( U const & ) const { return TX<T>(); }
template< class U > inline TX<T> operator<<( U const & ) const { return TX<T>(); }
template< class U > inline TX<T> operator>>( U const & ) const { return TX<T>(); }
inline bool operator==( T const & ) const { return false; }
inline bool operator< ( T const & ) const { return false; }
template< class U > inline TX<T> operator& ( U const & ) const { return TX<T>(); }
template< class U > inline TX<T> operator| ( U const & ) const { return TX<T>(); }
template< class U > inline TX<T> operator^ ( U const & ) const { return TX<T>(); }
template< class U > inline TX<T> operator&&( U const & ) const { return TX<T>(); }
template< class U > inline TX<T> operator||( U const & ) const { return TX<T>(); }
};
struct S0{}; typedef TX<S0> T0;
struct S1{}; typedef TX<S1> T1;
struct S2{}; typedef TX<S2> T2;
struct S3{}; typedef TX<S3> T3;
struct S4{}; typedef TX<S4> T4;
struct S5{}; typedef TX<S5> T5;
struct S6{}; typedef TX<S6> T6;
struct S7{}; typedef TX<S7> T7;
struct S8{}; typedef TX<S8> T8;
struct S9{}; typedef TX<S9> T9;
struct S10{}; typedef TX<S10> T10;
struct S11{}; typedef TX<S11> T11;
struct S12{}; typedef TX<S12> T12;
struct S13{}; typedef TX<S13> T13;
struct S14{}; typedef TX<S14> T14;
struct S15{}; typedef TX<S15> T15;
struct nulltype{};
template< class Head, class Tail >
struct typelist
{
typedef Head head;
typedef Tail tail;
};
// typelist max element size:
template< class List >
struct typelist_max;
template<>
struct typelist_max< nulltype >
{
enum V { value = 0 } ;
typedef void type;
};
template< class Head, class Tail >
struct typelist_max< typelist<Head, Tail> >
{
private:
enum TV { tail_value = size_t( typelist_max<Tail>::value ) };
typedef typename typelist_max<Tail>::type tail_type;
public:
enum V { value = (sizeof( Head ) > tail_value) ? sizeof( Head ) : std::size_t( tail_value ) } ;
typedef typename std11::conditional< (sizeof( Head ) > tail_value), Head, tail_type>::type type;
};
#if variant_CPP11_OR_GREATER
// typelist max alignof element type:
template< class List >
struct typelist_max_alignof;
template<>
struct typelist_max_alignof< nulltype >
{
enum V { value = 0 } ;
};
template< class Head, class Tail >
struct typelist_max_alignof< typelist<Head, Tail> >
{
private:
enum TV { tail_value = size_t( typelist_max_alignof<Tail>::value ) };
public:
enum V { value = (alignof( Head ) > tail_value) ? alignof( Head ) : std::size_t( tail_value ) };
};
#endif
// typelist size (length):
template< class List >
struct typelist_size
{
enum V { value = 1 };
};
template<> struct typelist_size< T0 > { enum V { value = 0 }; };
template<> struct typelist_size< T1 > { enum V { value = 0 }; };
template<> struct typelist_size< T2 > { enum V { value = 0 }; };
template<> struct typelist_size< T3 > { enum V { value = 0 }; };
template<> struct typelist_size< T4 > { enum V { value = 0 }; };
template<> struct typelist_size< T5 > { enum V { value = 0 }; };
template<> struct typelist_size< T6 > { enum V { value = 0 }; };
template<> struct typelist_size< T7 > { enum V { value = 0 }; };
template<> struct typelist_size< T8 > { enum V { value = 0 }; };
template<> struct typelist_size< T9 > { enum V { value = 0 }; };
template<> struct typelist_size< T10 > { enum V { value = 0 }; };
template<> struct typelist_size< T11 > { enum V { value = 0 }; };
template<> struct typelist_size< T12 > { enum V { value = 0 }; };
template<> struct typelist_size< T13 > { enum V { value = 0 }; };
template<> struct typelist_size< T14 > { enum V { value = 0 }; };
template<> struct typelist_size< T15 > { enum V { value = 0 }; };
template<> struct typelist_size< nulltype > { enum V { value = 0 } ; };
template< class Head, class Tail >
struct typelist_size< typelist<Head, Tail> >
{
enum V { value = size_t(typelist_size<Head>::value) + size_t(typelist_size<Tail>::value) };
};
// typelist index of type:
template< class List, class T >
struct typelist_index_of;
template< class T >
struct typelist_index_of< nulltype, T >
{
enum V { value = -1 };
};
template< class Tail, class T >
struct typelist_index_of< typelist<T, Tail>, T >
{
enum V { value = 0 };
};
template< class Head, class Tail, class T >
struct typelist_index_of< typelist<Head, Tail>, T >
{
private:
enum TV { nextVal = typelist_index_of<Tail, T>::value };
public:
enum V { value = nextVal == -1 ? -1 : 1 + nextVal } ;
};
// typelist type at index:
template< class List, std::size_t i>
struct typelist_type_at;
template< class Head, class Tail >
struct typelist_type_at< typelist<Head, Tail>, 0 >
{
typedef Head type;
};
template< class Head, class Tail, std::size_t i >
struct typelist_type_at< typelist<Head, Tail>, i >
{
typedef typename typelist_type_at<Tail, i - 1>::type type;
};
// typelist type is unique:
template< class List, std::size_t CmpIndex, std::size_t LastChecked = typelist_size<List>::value >
struct typelist_type_is_unique
{
private:
typedef typename typelist_type_at<List, CmpIndex>::type cmp_type;
typedef typename typelist_type_at<List, LastChecked - 1>::type cur_type;
public:
enum V { value = ((CmpIndex == (LastChecked - 1)) | !std11::is_same<cmp_type, cur_type>::value)
&& typelist_type_is_unique<List, CmpIndex, LastChecked - 1>::value } ;
};
template< class List, std::size_t CmpIndex >
struct typelist_type_is_unique< List, CmpIndex, 0 >
{
enum V { value = 1 } ;
};
template< class List, class T >
struct typelist_contains_unique_type : typelist_type_is_unique< List, typelist_index_of< List, T >::value >
{
};
#if variant_CONFIG_MAX_ALIGN_HACK
// Max align, use most restricted type for alignment:
#define variant_UNIQUE( name ) variant_UNIQUE2( name, __LINE__ )
#define variant_UNIQUE2( name, line ) variant_UNIQUE3( name, line )
#define variant_UNIQUE3( name, line ) name ## line
#define variant_ALIGN_TYPE( type ) \
type variant_UNIQUE( _t ); struct_t< type > variant_UNIQUE( _st )
template< class T >
struct struct_t { T _; };
union max_align_t
{
variant_ALIGN_TYPE( char );
variant_ALIGN_TYPE( short int );
variant_ALIGN_TYPE( int );
variant_ALIGN_TYPE( long int );
variant_ALIGN_TYPE( float );
variant_ALIGN_TYPE( double );
variant_ALIGN_TYPE( long double );
variant_ALIGN_TYPE( char * );
variant_ALIGN_TYPE( short int * );
variant_ALIGN_TYPE( int * );
variant_ALIGN_TYPE( long int * );
variant_ALIGN_TYPE( float * );
variant_ALIGN_TYPE( double * );
variant_ALIGN_TYPE( long double * );
variant_ALIGN_TYPE( void * );
#ifdef HAVE_LONG_LONG
variant_ALIGN_TYPE( long long );
#endif
struct Unknown;
Unknown ( * variant_UNIQUE(_) )( Unknown );
Unknown * Unknown::* variant_UNIQUE(_);
Unknown ( Unknown::* variant_UNIQUE(_) )( Unknown );
struct_t< Unknown ( * )( Unknown) > variant_UNIQUE(_);
struct_t< Unknown * Unknown::* > variant_UNIQUE(_);
struct_t< Unknown ( Unknown::* )(Unknown) > variant_UNIQUE(_);
};
#undef variant_UNIQUE
#undef variant_UNIQUE2
#undef variant_UNIQUE3
#undef variant_ALIGN_TYPE
#elif defined( variant_CONFIG_ALIGN_AS ) // variant_CONFIG_MAX_ALIGN_HACK
// Use user-specified type for alignment:
#define variant_ALIGN_AS( unused ) \
variant_CONFIG_ALIGN_AS
#else // variant_CONFIG_MAX_ALIGN_HACK
// Determine POD type to use for alignment:
#define variant_ALIGN_AS( to_align ) \
typename detail::type_of_size< detail::alignment_types, detail::alignment_of< to_align >::value >::type
template< typename T >
struct alignment_of;
template< typename T >
struct alignment_of_hack
{
char c;
T t;
alignment_of_hack();
};
template< size_t A, size_t S >
struct alignment_logic
{
enum V { value = A < S ? A : S };
};
template< typename T >
struct alignment_of
{
enum V { value = alignment_logic<
sizeof( alignment_of_hack<T> ) - sizeof(T), sizeof(T) >::value };
};
template< typename List, size_t N >
struct type_of_size
{
typedef typename std11::conditional<
N == sizeof( typename List::head ),
typename List::head,
typename type_of_size<typename List::tail, N >::type >::type type;
};
template< size_t N >
struct type_of_size< nulltype, N >
{
typedef variant_CONFIG_ALIGN_AS_FALLBACK type;
};
template< typename T>
struct struct_t { T _; };
#define variant_ALIGN_TYPE( type ) \
typelist< type , typelist< struct_t< type >
struct Unknown;
typedef
variant_ALIGN_TYPE( char ),
variant_ALIGN_TYPE( short ),
variant_ALIGN_TYPE( int ),
variant_ALIGN_TYPE( long ),
variant_ALIGN_TYPE( float ),
variant_ALIGN_TYPE( double ),
variant_ALIGN_TYPE( long double ),
variant_ALIGN_TYPE( char *),
variant_ALIGN_TYPE( short * ),
variant_ALIGN_TYPE( int * ),
variant_ALIGN_TYPE( long * ),
variant_ALIGN_TYPE( float * ),
variant_ALIGN_TYPE( double * ),
variant_ALIGN_TYPE( long double * ),
variant_ALIGN_TYPE( Unknown ( * )( Unknown ) ),
variant_ALIGN_TYPE( Unknown * Unknown::* ),
variant_ALIGN_TYPE( Unknown ( Unknown::* )( Unknown ) ),
nulltype
> > > > > > > > > > > > > >
> > > > > > > > > > > > > >
> > > > > >
alignment_types;
#undef variant_ALIGN_TYPE
#endif // variant_CONFIG_MAX_ALIGN_HACK
#if variant_CPP11_OR_GREATER
template< typename T>
inline std::size_t hash( T const & v )
{
return std::hash<T>()( v );
}
inline std::size_t hash( T0 const & ) { return 0; }
inline std::size_t hash( T1 const & ) { return 0; }
inline std::size_t hash( T2 const & ) { return 0; }