-
Notifications
You must be signed in to change notification settings - Fork 0
/
bazaar_traits.hpp
1745 lines (1421 loc) · 70.2 KB
/
bazaar_traits.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 (c) 2022 Papa Libasse Sow.
// https://github.com/Nandite/bazaar_traits
// Distributed under the MIT Software License (X11 license).
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of
// the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef BAZAAR_TRAITS_HPP
#define BAZAAR_TRAITS_HPP
#include "logical_operators.hpp"
#include "type_list.hpp"
#include "macros.hpp"
// Comment this line to use handwritten implementation of some traits
// instead of relying on the compiler to provides them trough keywords.
#define USE_COMPILER_SUPPORT_WHEN_POSSIBLE
namespace bazaar::traits {
//-------------------------------------------------------------------------------------------
// Array properties and transformations
//-------------------------------------------------------------------------------------------
// Rank
namespace impl
{
template<typename Tp> struct rank_impl : integral_constant<std::size_t ,0>{};
template<typename Tp> struct rank_impl<Tp[]> : integral_constant< std::size_t,
1 + rank_impl<Tp>::value>{};
template<typename Tp, std::size_t N> struct rank_impl<Tp[N]> : integral_constant<std::size_t,
1 + rank_impl<Tp>::value>{};
}
template<typename Tp> struct rank : public impl::rank_impl<Tp>{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto rank_v{rank<Tp>::value};
// Extent
namespace impl {
template<typename Tp, std::size_t N = 0>
struct extent_impl : public integral_constant<std::size_t, 0>{};
template<typename Tp>
struct extent_impl<Tp[], 0> : public integral_constant<std::size_t, 0>{};
template<typename Tp, std::size_t Dim>
struct extent_impl<Tp[], Dim> : public extent_impl<Tp, Dim-1>{};
template<typename Tp, std::size_t Dim>
struct extent_impl<Tp[Dim], 0> : public integral_constant<std::size_t, Dim>{};
template<typename Tp, std::size_t Dim, std::size_t N>
struct extent_impl<Tp[Dim], N> : public extent_impl<Tp, N - 1>{};
}
template<typename Tp, std::size_t N = 0>
struct extent : public impl::extent_impl<Tp, N>{};
template<typename Tp, std::size_t N = 0>
[[maybe_unused]] inline constexpr auto extent_v{extent<Tp, N>::value};
// Remove extent
template<typename Tp> struct remove_extent : public identity<Tp>{};
template<typename Tp> struct remove_extent<Tp[]> : public identity<Tp>{};
template<typename Tp, std::size_t N>
struct remove_extent<Tp[N]> : public identity<Tp>{};
template<typename Tp>
using remove_extent_t [[maybe_unused]] = typename remove_extent<Tp>::type;
// Remove all extents
template<typename Tp> struct remove_all_extents : public identity<Tp>{};
template<typename Tp>
struct remove_all_extents<Tp[]> : public identity<typename remove_all_extents<Tp>::type>{};
template<typename Tp, std::size_t N>
struct remove_all_extents<Tp[N]> : public identity<typename remove_all_extents<Tp>::type> {};
template<typename Tp>
using remove_all_extents_t [[maybe_unused]] = typename remove_all_extents<Tp>::type;
namespace impl {
template<typename Tp> struct is_bounded_array_impl : public false_type {};
template<typename Tp, std::size_t N> struct is_bounded_array_impl<Tp[N]> : public true_type {};
template<typename Tp> struct is_unbounded_array_impl : public false_type {};
template<typename Tp> struct is_unbounded_array_impl<Tp[]> : public true_type {};
}
template<typename Tp>
struct is_bounded_array : public impl::is_bounded_array_impl<Tp>{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_bounded_array_v{is_bounded_array<Tp>::value};
template<typename Tp>
struct is_unbounded_array : public impl::is_unbounded_array_impl<Tp>{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_unbounded_array_v{is_unbounded_array<Tp>::value};
//-------------------------------------------------------------------------------------------
// Reference transformations
//-------------------------------------------------------------------------------------------
// Remove reference
template<typename Tp> struct remove_reference : public identity<Tp>{};
template<typename Tp> struct remove_reference<Tp&> : public identity<Tp>{};
template<typename Tp> struct remove_reference<Tp&&> : public identity<Tp>{};
template<typename Tp>
using remove_reference_t [[maybe_unused]] = typename remove_reference<Tp>::type;
namespace impl
{
// Is referenceable
struct is_non_referenceable_type;
template<typename Tp> Tp& test_is_referenceable(int);
template<typename> is_non_referenceable_type test_is_referenceable(...);
template<typename Tp>
struct is_referenceable
: public is_not_same<is_non_referenceable_type,
decltype(test_is_referenceable<Tp>(0))> {
};
}
// Add lvalue reference
namespace impl {
template<typename Tp, bool = impl::is_referenceable<Tp>::value>
struct add_lvalue_reference_impl : public identity<Tp>{};
template<typename Tp> struct add_lvalue_reference_impl<Tp, true> : public identity<Tp&>{};
}
template<typename Tp>
struct add_lvalue_reference : public identity<typename impl::add_lvalue_reference_impl<Tp>::type> {};
template<typename Tp>
using add_lvalue_reference_t [[maybe_unused]] = typename add_lvalue_reference<Tp>::type;
// Add rvalue reference
namespace impl {
template<typename Tp, bool = impl::is_referenceable<Tp>::value>
struct add_rvalue_reference_impl : public identity<Tp>{};
template<typename Tp> struct add_rvalue_reference_impl<Tp, true> : public identity<Tp&&>{};
}
template<typename Tp>
struct add_rvalue_reference : public identity<typename impl::add_rvalue_reference_impl<Tp>::type> {};
template<typename Tp>
using add_rvalue_reference_t [[maybe_unused]] = typename add_rvalue_reference<Tp>::type;
//-------------------------------------------------------------------------------------------
// Const-volatile properties and transformations
//-------------------------------------------------------------------------------------------
// Is const
template<typename Tp> struct is_const : public false_type {};
template<typename Tp> struct is_const<Tp const> : public true_type {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_const_v {is_const<Tp>::value};
// Is volatile
template<typename Tp> struct is_volatile : public false_type {};
template<typename Tp> struct is_volatile<Tp volatile> : public true_type {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_volatile_v{is_volatile<Tp>::value};
// Remove const
template <typename Tp> struct remove_const : public identity<Tp>{};
template <typename Up> struct remove_const<const Up> : public identity<Up>{};
template<typename Tp> using remove_const_t = typename remove_const<Tp>::type ;
// Remove volatile
template <typename Tp> struct remove_volatile : public identity<Tp>{};
template <typename Up> struct remove_volatile<volatile Up> : public identity<Up>{};
template<typename Tp> using remove_volatile_t = typename remove_volatile<Tp>::type ;
// Remove cv
template<typename Tp>
struct remove_cv : public identity<remove_const_t<remove_volatile_t<Tp>>>{};
template<typename Tp> using remove_cv_t = typename remove_cv<Tp>::type;
// Add const
template<typename Tp>
struct add_const : public identity<const Tp>{};
template<typename Tp>
using add_const_t = typename add_const<Tp>::type;
// Add volatile
template<typename Tp>
struct add_volatile : public identity<volatile Tp>{};
template<typename Tp>
using add_volatile_t = typename add_volatile<Tp>::type;
// Add cv
template<typename Tp>
struct add_cv : public identity<add_const_t<add_volatile_t<Tp>>> {};
template<typename Tp>
using add_cv_t [[maybe_unused]] = typename add_cv<Tp>::type;
// Apply cv
namespace impl
{
template<typename In, typename Out, bool = is_const_v<remove_reference_t<In>>,
bool = is_volatile_v<remove_reference_t<In>>>
struct apply_cv_impl : public identity<Out> {};
template<typename In, typename Out>
struct apply_cv_impl<In, Out, false, false> : public identity<Out>{};
template<typename In, typename Out>
struct apply_cv_impl<In, Out, true, false> : public identity<const Out>{};
template<typename In, typename Out>
struct apply_cv_impl<In, Out, false, true> : public identity<volatile Out>{};
template<typename In, typename Out>
struct apply_cv_impl<In, Out, true, true> : public identity<const volatile Out>{};
template<typename In, typename Out>
struct apply_cv_impl<In&, Out, false, false> : public identity<Out&>{};
template<typename In, typename Out>
struct apply_cv_impl<In&, Out, true, false> : public identity<const Out&>{};
template<typename In, typename Out>
struct apply_cv_impl<In&, Out, false, true> : public identity<volatile Out&>{};
template<typename In, typename Out>
struct apply_cv_impl<In&, Out, true, true> : public identity<const volatile Out&>{};
template<typename In, typename Out>
struct apply_cv : public impl::apply_cv_impl<In, Out> {};
}
//-------------------------------------------------------------------------------------------
// Primary classification traits
//-------------------------------------------------------------------------------------------
// Is void
template<typename Tp>
struct is_void : public impl::is_one_of<Tp,
void,
void const,
void volatile,
void const volatile>{};
// Alternate design
// template<typename Tp>
// struct [[maybe_unused]] is_void {
// static constexpr bool value = is_same_v<std::remove_cv_t<Tp>, void>
// || is_same_v<std::remove_cv_t<Tp>, void const>
// || is_same_v<std::remove_cv_t<Tp>, void volatile> ||
// is_same_v<std::remove_cv_t<Tp>, void const volatile>;
// };
// Alternate design
// template<typename Tp>
// struct [[maybe_unused]] is_void : public is_same<remove_cv_t<Tp>, void>::type {};
// Alternate design
// template<typename Tp> struct [[maybe_unused]] is_void : public false_type {};
// template<> struct [[maybe_unused]] is_void<void> : public true_type {};
// template<> struct [[maybe_unused]] is_void<void const> : public true_type {};
// template<> struct [[maybe_unused]] is_void<void volatile> : public true_type {};
// template<> struct [[maybe_unused]] is_void<void const volatile> : public true_type {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_void_v{is_void<Tp>::value};
// Is nullptr
namespace impl {
template<typename Tp>
struct is_null_pointer_impl {static constexpr bool value{is_same_v<remove_cv_t<Tp>, nullptr_t>};};
// Alternate design
// template<typename Tp>
// struct is_null_pointer_impl : public is_same<remove_cv_t<Tp>, nullptr_t>::type{};
}
template<typename Tp>
struct is_null_pointer : public impl::is_null_pointer_impl<Tp>{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_null_pointer_v{is_null_pointer<Tp>::value};
// Is integral and floating
namespace impl
{
template<typename Tp> struct is_integral_impl : public false_type {};
template<> struct is_integral_impl<bool> : public true_type {};
template<> struct is_integral_impl<char> : public true_type {};
template<> struct is_integral_impl<signed char> : public true_type {};
template<> struct is_integral_impl<unsigned char> : public true_type {};
template<> struct is_integral_impl<wchar_t> : public true_type {};
template<> struct is_integral_impl<char16_t> : public true_type {};
template<> struct is_integral_impl<char32_t> : public true_type {};
template<> struct is_integral_impl<short> : public true_type {};
template<> struct is_integral_impl<unsigned short> : public true_type {};
template<> struct is_integral_impl<int> : public true_type {};
template<> struct is_integral_impl<unsigned int> : public true_type {};
template<> struct is_integral_impl<long> : public true_type {};
template<> struct is_integral_impl<unsigned long> : public true_type {};
template<> struct is_integral_impl<long long> : public true_type {};
template<> struct is_integral_impl<unsigned long long> : public true_type {};
template<typename Tp> struct is_floating_point_impl : public false_type {};
template<> struct is_floating_point_impl<float> : public true_type {};
template<> struct is_floating_point_impl<double> : public true_type {};
template<> struct is_floating_point_impl<long double> : public true_type {};
template<typename Tp> struct is_signed_integer_impl : public false_type{};
template<> struct is_signed_integer_impl<signed char> : public true_type{};
template<> struct is_signed_integer_impl<signed short> : public true_type{};
template<> struct is_signed_integer_impl<signed int> : public true_type{};
template<> struct is_signed_integer_impl<signed long> : public true_type{};
template<> struct is_signed_integer_impl<signed long long> : public true_type{};
template<typename Tp> struct is_unsigned_integer_impl : public false_type{};
template<> struct is_unsigned_integer_impl<unsigned char> : public true_type{};
template<> struct is_unsigned_integer_impl<unsigned short> : public true_type{};
template<> struct is_unsigned_integer_impl<unsigned int> : public true_type{};
template<> struct is_unsigned_integer_impl<unsigned long> : public true_type{};
template<> struct is_unsigned_integer_impl<unsigned long long> : public true_type{};
}
template<typename Tp>
struct is_integral : public impl::is_integral_impl<remove_cv_t<Tp>> {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_integral_v{is_integral<Tp>::value};
template<typename Tp>
struct is_floating_point : public impl::is_floating_point_impl<remove_cv_t<Tp>> {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_floating_v{is_floating_point<Tp>::value};
template<typename Tp>
struct is_signed_integer : public conjunction<is_integral<remove_cv_t<Tp>>,
impl::is_signed_integer_impl<remove_cv_t<Tp>>>{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_signed_integer_v{is_signed_integer<Tp>::value};
template<typename Tp>
struct is_unsigned_integer : public conjunction<is_integral<remove_cv_t<Tp>>,
impl::is_unsigned_integer_impl<remove_cv_t<Tp>>>{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_unsigned_integer_v{is_unsigned_integer<Tp>::value};
// Is arithmetic
template<typename Tp>
struct is_arithmetic : public disjunction<is_integral<Tp>,
is_floating_point<Tp>>{};
// Alternate design
// template<typename Tp>
// struct is_arithmetic : public impl::is_true_one_of<typename is_integral<Tp>::type,
// typename is_floating_point<Tp>::type>{};
// Alternate design
// template<typename Tp>
// struct is_arithmetic : public bool_constant<is_integral_v<Tp> || is_floating_v<Tp>>{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_arithmetic_v {is_arithmetic<Tp>::value};
// Is array
template<typename Tp> struct is_array : public false_type {};
template<typename Tp> struct is_array<Tp[]> : public true_type {};
template<typename Tp, std::size_t N> struct is_array<Tp[N]> : public true_type {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_array_v{is_array<Tp>::value};
// Is pointer
namespace impl {
template<typename Tp> struct is_pointer_impl : public false_type {};
template<typename Tp> struct is_pointer_impl<Tp*> : public true_type {};
}
template<typename Tp>
struct is_pointer : public impl::is_pointer_impl<remove_cv_t<Tp>> {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_pointer_v{is_pointer<Tp>::value};
// Is lvalue reference
template<typename Tp> struct is_lvalue_reference : public false_type {};
template<typename Tp> struct is_lvalue_reference<Tp&> : public true_type {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_lvalue_reference_v{is_lvalue_reference<Tp>::value};
// Is rvalue reference
template<typename Tp> struct is_rvalue_reference : public false_type {};
template<typename Tp> struct is_rvalue_reference<Tp&&> : public true_type {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_rvalue_reference_v{is_rvalue_reference<Tp>::value};
// Is reference
template<typename Tp>
struct is_reference : public disjunction<is_lvalue_reference<Tp>, is_rvalue_reference<Tp>> {};
// Alternate design
// template<typename Tp> struct is_reference : public false_type {};
// template<typename Tp> struct is_reference<Tp&> : public true_type {};
// template<typename Tp> struct is_reference<Tp&&> : public true_type {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_reference_v{is_reference<Tp>::value};
// Is function
template<typename Tp>
struct is_function : public negation<
disjunction<is_const<const Tp>, is_reference<Tp>>>{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_function_v{is_function<Tp>::value};
// Is member pointer
namespace impl
{
template<typename Tp>
struct is_member_pointer_impl : public false_type {};
template<typename Tp, typename Up>
struct is_member_pointer_impl<Tp Up::*> : public true_type {};
}
template<typename Tp>
struct is_member_pointer : public impl::is_member_pointer_impl<Tp>{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_member_pointer_v{is_member_pointer<Tp>::value};
// Is member object pointer
namespace impl{
template<typename Tp>
struct is_member_object_pointer_impl : public false_type {};
template<typename Tp, typename Up>
struct is_member_object_pointer_impl<Tp Up::*> :
public negation<is_function<Tp>>{};
}
template<typename Tp>
struct is_member_object_pointer : public
impl::is_member_object_pointer_impl<remove_cv_t<Tp>>
{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_member_object_pointer_v{is_member_object_pointer<Tp>::value};
// Is member function pointer
namespace impl {
template<typename Tp>
struct is_member_function_pointer_impl : public false_type {};
template<typename Tp, typename Up>
struct is_member_function_pointer_impl<Tp Up::*> :
public is_function<Tp>{};
}
template<typename Tp>
struct is_member_function_pointer : public
impl::is_member_function_pointer_impl<remove_cv_t<Tp>>
{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_member_function_pointer_v{is_member_function_pointer<Tp>::value};
// Is member union
#if (__has_feature(is_union) || defined(IS_COMPILER_GCC))
template <typename Tp> struct is_union : public bool_constant<__is_union(Tp)> {};
#else
template <typename Tp> struct is_union_impl : public false_type {};
template <typename Tp> struct is_union : public is_union_impl<remove_cv_t<Tp>> {};
#endif
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_union_v{is_union<Tp>::value};
// Is class
#if (__has_feature(is_class) || defined(IS_COMPILER_GCC)) \
&& defined(USE_COMPILER_SUPPORT_WHEN_POSSIBLE)
template<typename Tp> struct is_class : public bool_constant<__is_class(Tp)>{};
#else
namespace impl
{
struct is_not_class_type {};
template<typename Tp, typename = void_t<int Tp::*>> true_type is_class_test(int);
template<typename> is_not_class_type is_class_test(...);
}
template<typename Tp> struct is_class : public conjunction<negation<is_union<Tp>>,
is_not_same<decltype(impl::is_class_test<Tp>(0)), impl::is_not_class_type>>
{};
#endif
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_class_v{is_class<Tp>::value};
// Is enum
#if (__has_feature(is_enum) || defined(IS_COMPILER_GCC)) \
&& defined(USE_COMPILER_SUPPORT_WHEN_POSSIBLE)
template<typename Tp> struct is_enum : public bool_constant<__is_enum(Tp)>{};
#else
template<typename Tp> struct is_enum : public negation<disjunction<
is_void<Tp>,
is_arithmetic<Tp>,
is_array<Tp>,
is_pointer<Tp>,
is_reference<Tp>,
is_member_pointer<Tp>,
is_union<Tp>,
is_class<Tp>,
is_function<Tp>>>{};
#endif
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_enum_v{is_enum<Tp>::value};
namespace impl
{
template<typename Tp>
using is_complete_helper = decltype(sizeof(std::declval<Tp>()));
template <typename Tp, typename = void_t<is_complete_helper<Tp>>>
[[maybe_unused]] static true_type test_is_complete(Tp*);
template <typename, typename = void> static false_type test_is_complete(...);
template<typename Tp>
struct is_complete_impl : public decltype(test_is_complete<Tp>(nullptr)) {};
}
template<typename Tp>
struct is_complete : public disjunction<
is_function<remove_reference_t<Tp>>,
impl::is_complete_impl<Tp>> {};
template<typename Tp>
struct is_complete<Tp&> : public is_complete<remove_reference_t<Tp>> {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_complete_v{is_complete<Tp>::value};
namespace impl
{
template<typename Tp>
struct is_complete_or_unbounded : public disjunction<is_complete<Tp>, is_unbounded_array<Tp>> {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_complete_or_unbounded_v{is_complete_or_unbounded<Tp>::value};
}
//-------------------------------------------------------------------------------------------
// Secondary classification traits
//-------------------------------------------------------------------------------------------
// Is fundamental
template<typename Tp>
struct is_fundamental : public disjunction<
is_arithmetic<Tp>,
is_void<Tp>,
is_null_pointer<Tp>>{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_fundamental_v{is_fundamental<Tp>::value};
// Is scalar
template<typename Tp>
struct is_scalar : public disjunction<
is_arithmetic<Tp>,
is_pointer<Tp>,
is_member_pointer<Tp>,
is_enum<Tp>,
is_null_pointer<Tp>
// Case when Tp is an objective-C++ block is not handled
> {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_scalar_v{is_scalar<Tp>::value};
// Is object
template<typename Tp>
struct is_object : public conjunction<
negation<is_reference<Tp>>,
negation<is_function<Tp>>,
negation<is_void<Tp>>>{};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_object_v{is_object<Tp>::value};
//Is compound
template<typename Tp>
struct is_compound : public negation<is_fundamental<Tp>> {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_compound_v{is_compound<Tp>::value};
//-------------------------------------------------------------------------------------------
// Pointer transformations
//-------------------------------------------------------------------------------------------
// Remove pointer
namespace impl
{
template<typename Tp> struct remove_pointer_helper : public identity<Tp>{};
template<typename Tp> struct remove_pointer_helper<Tp*> : public identity<Tp>{};
template<typename Tp, bool = is_pointer_v<Tp>> struct remove_pointer_impl : public identity<Tp> {};
template<typename Tp>
struct remove_pointer_impl<Tp, true> : public identity<typename remove_pointer_helper<Tp>::type> {};
}
// Alternate design
// template<typename Tp> struct remove_pointer : identity<Tp> {};
// template<typename Tp> struct remove_pointer<Tp*> : identity<Tp> {};
// template<typename Tp> struct remove_pointer<Tp* const> : identity<Tp> {};
// template<typename Tp> struct remove_pointer<Tp * volatile> : identity<Tp> {};
// template<typename Tp> struct remove_pointer<Tp * const volatile> : identity<Tp> {};
template<typename Tp>
struct remove_pointer : public impl::remove_pointer_impl<remove_cv_t<Tp>> {};
template<typename Tp>
using remove_pointer_t [[maybe_unused]] = typename remove_pointer<Tp>::type;
// Add pointer
namespace impl
{
template<typename Tp, bool = is_referenceable<Tp>::value || is_void_v<Tp>>
struct add_pointer_impl : public identity<remove_reference_t<Tp>*>{};
template<typename Tp>
struct add_pointer_impl<Tp, false> : public identity<Tp>{};
}
template<typename Tp>
struct add_pointer : public impl::add_pointer_impl<Tp>{};
template<typename Tp>
using add_pointer_t [[maybe_unused]] = typename add_pointer<Tp>::type;
//-------------------------------------------------------------------------------------------
// Integral properties
//-------------------------------------------------------------------------------------------
// Is signed
namespace impl
{
template<typename Tp, bool = is_integral_v<Tp>>
struct is_signed_impl : public bool_condition<Tp(-1) < Tp(0)>::type {};
template<typename Tp> // floating point is signed by default
struct is_signed_impl<Tp, false> : public true_type {};
}
template<typename Tp, bool = is_arithmetic_v<Tp>>
struct is_signed : public impl::is_signed_impl<Tp>{};
template<typename Tp>
struct is_signed<Tp, false> : public false_type {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_signed_v{is_signed<Tp>::value};
// Is unsigned
namespace impl
{
template<typename Tp, bool = is_integral_v<Tp>>
struct is_unsigned_impl : public bool_condition<Tp(0) < Tp(-1)>::type {};
template<typename Tp> // floating point is signed by default
struct is_unsigned_impl<Tp, false> : public false_type {};
}
template<typename Tp, bool = is_arithmetic_v<Tp>>
struct is_unsigned : public impl::is_unsigned_impl<Tp>{};
template<typename Tp>
struct is_unsigned<Tp, false> : public false_type {};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_unsigned_v{is_unsigned<Tp>::value};
// Make signed
namespace impl
{
template<typename Tp, bool = is_integral_v<Tp> || is_enum_v<Tp>>
struct make_signed_impl {};
template<typename Tp>
struct make_signed_impl<Tp, true> {
using type = typename make_signed_impl<typename
impl::find_first_upper_bound_element_by_size<signed_types_list,
sizeof(Tp)>::type>::type;
};
template<> struct make_signed_impl <bool, true>;
template<> struct make_signed_impl <signed char, true> : public identity<signed char>{};
template<> struct make_signed_impl <unsigned char, true> : public identity<signed char>{};
template<> struct make_signed_impl <signed short, true> : public identity<signed short>{};
template<> struct make_signed_impl <unsigned short, true> : public identity<signed short>{};
template<> struct make_signed_impl <signed int, true> : public identity<signed int>{};
template<> struct make_signed_impl <unsigned int, true> : public identity<signed int>{};
template<> struct make_signed_impl <signed long, true> : public identity<signed long>{};
template<> struct make_signed_impl <unsigned long, true> : public identity<signed long>{};
template<> struct make_signed_impl <signed long long, true> : public identity<signed long long>{};
template<> struct make_signed_impl <unsigned long long, true> : public identity<signed long long>{};
template<>
struct make_signed_impl<wchar_t, false> {
using type = typename make_signed_impl<typename
impl::find_first_upper_bound_element_by_size<signed_types_list,
sizeof(wchar_t)>::type>::type;
};
#if __cplusplus > 201703L
template<>
struct make_signed_impl<char8_t, false> {
using type = typename make_signed_impl<typename
impl::find_first_upper_bound_element_by_size<signed_types_list,
sizeof(char8_t)>::type>::type;
};
#endif
template<>
struct make_signed_impl<char16_t, false> {
using type = typename make_signed_impl<typename
impl::find_first_upper_bound_element_by_size<signed_types_list,
sizeof(char16_t)>::type>::type;
};
template<>
struct make_signed_impl<char32_t, false> {
using type = typename make_signed_impl<typename
impl::find_first_upper_bound_element_by_size<signed_types_list,
sizeof(char32_t)>::type>::type;
};
}
template<typename Tp>
struct make_signed : public impl::apply_cv<Tp,
typename impl::make_signed_impl<remove_cv_t<Tp>>::type
> {};
template<typename Tp>
using make_signed_t [[maybe_unused]] = typename make_signed<Tp>::type;
// Make unsigned
namespace impl
{
template<typename Tp, bool = is_integral_v<Tp> || is_enum_v<Tp>>
struct make_unsigned_impl {};
template<typename Tp>
struct make_unsigned_impl<Tp, true> {
using type = typename make_unsigned_impl<
typename impl::find_first_upper_bound_element_by_size<unsigned_types_list,
sizeof(Tp)>::type>::type;
};
template<> struct make_unsigned_impl <bool, true> {};
template<> struct make_unsigned_impl <signed char, true> : public identity<unsigned char>{};
template<> struct make_unsigned_impl <unsigned char, true> : public identity<unsigned char>{};
template<> struct make_unsigned_impl <signed short, true> : public identity<unsigned short>{};
template<> struct make_unsigned_impl <unsigned short, true> : public identity<unsigned short>{};
template<> struct make_unsigned_impl <signed int, true> : public identity<unsigned int>{};
template<> struct make_unsigned_impl <unsigned int, true> : public identity<unsigned int>{};
template<> struct make_unsigned_impl <signed long, true> : public identity<unsigned long>{};
template<> struct make_unsigned_impl <unsigned long, true> : public identity<unsigned long>{};
template<> struct make_unsigned_impl <signed long long, true> : public identity<unsigned long long>{};
template<> struct make_unsigned_impl <unsigned long long, true> : public identity<unsigned long long>{};
template<>
struct make_unsigned_impl<wchar_t, false> {
using type = typename make_unsigned_impl<typename
impl::find_first_upper_bound_element_by_size<unsigned_types_list ,
sizeof(wchar_t)>::type>::type;
};
#if __cplusplus > 201703L
template<>
struct make_unsigned_impl<char8_t, false> {
using type = typename make_unsigned_impl<typename
impl::find_first_upper_bound_element_by_size<unsigned_types_list ,
sizeof(char8_t)>::type>::type;
};
#endif
template<>
struct make_unsigned_impl<char16_t, false> {
using type = typename make_unsigned_impl<typename
impl::find_first_upper_bound_element_by_size<unsigned_types_list ,
sizeof(char16_t)>::type>::type;
};
template<>
struct make_unsigned_impl<char32_t, false> {
using type = typename make_unsigned_impl<typename
impl::find_first_upper_bound_element_by_size<unsigned_types_list ,
sizeof(char32_t)>::type>::type;
};
}
template<typename Tp>
struct make_unsigned : impl::apply_cv<Tp,
typename impl::make_unsigned_impl<remove_cv_t<Tp>>::type> {};
template<typename Tp>
using make_unsigned_t [[maybe_unused]] = typename make_unsigned<Tp>::type;
//-------------------------------------------------------------------------------------------
// Member introspection
//-------------------------------------------------------------------------------------------
// Is constructible
namespace impl
{
template<typename Tp, typename ... Args>
struct is_constructible_impl: public bool_constant<__is_constructible(Tp, Args...)> {};
}
template<typename Tp, typename ... Args>
struct is_constructible : public impl::is_constructible_impl<Tp, Args...> {
static_assert(impl::is_complete_or_unbounded_v<Tp>,
"Template argument must be a complete type or an unbounded array");
};
template<typename Tp, typename ... Args>
[[maybe_unused]] inline constexpr auto is_constructible_v {is_constructible<Tp, Args...>::value};
// Is default constructible
template<typename Tp>
struct is_default_constructible : public is_constructible<Tp>{
static_assert(impl::is_complete_or_unbounded_v<Tp>,
"Template argument must be a complete type or an unbounded array");
};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_default_constructible_v{is_default_constructible<Tp>::value};
// Is copy constructible
namespace impl
{
template<typename Tp, bool = is_referenceable<Tp>::value>
struct is_copy_constructible_impl : public is_constructible<Tp,
add_lvalue_reference_t<add_const_t<Tp>>> {};
template<typename Tp>
struct is_copy_constructible_impl<Tp, false> : public false_type {};
}
template<typename Tp>
struct is_copy_constructible : public impl::is_copy_constructible_impl<Tp>{
static_assert(impl::is_complete_or_unbounded_v<Tp>,
"Template argument must be a complete type or an unbounded array");
};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_copy_constructible_v{is_copy_constructible<Tp>::value};
// Is move constructible
template<typename Tp>
struct is_move_constructible : public is_constructible<Tp,
add_rvalue_reference_t<Tp>>{
static_assert(impl::is_complete_or_unbounded_v<Tp>,
"Template argument must be a complete type or an unbounded array");
};
template<typename Tp>
[[maybe_unused]] inline constexpr auto is_move_constructible_v{is_move_constructible<Tp>::value};
// Is assignable
namespace impl {
template<typename Tp, typename Up>
using is_assignable_helper_t = decltype(std::declval<Tp>() = std::declval<Up>());
// Alternate design
// template<typename Tp, typename Up, typename = void_t<is_assignable_helper_t<Tp, Up>>>
// true_type is_assignable_test(int);
//
// template<typename, typename>
// false_type is_assignable_test(...);
//
// template<typename Tp, typename Up, bool = is_void_v<Tp> || is_void_v<Up>>
// struct is_assignable_impl : public decltype((is_assignable_test<Tp, Up>(0))){};
//
// template<typename Tp, typename Up>
// struct is_assignable_impl<Tp, Up, true> : public false_type {};
template<typename, typename, typename = void>
struct is_assignable_impl : public false_type {};
template<typename Tp, typename Up>
struct is_assignable_impl<Tp, Up, void_t<is_assignable_helper_t<Tp, Up>>> : public is_same<
is_assignable_helper_t<Tp, Up>, Tp> {};
// Alternate design
// template<typename Tp, typename Up>
// struct is_assignable_impl
// {
// private:
// template<typename = decltype((std::declval<Tp>() = std::declval<Up>()))>
// static true_type is_assignable_test(int);
// static false_type is_assignable_test(...);
// public:
// using type = decltype((is_assignable_test(0)));
// };
}
template<typename Tp, typename Up>
struct is_assignable : public impl::is_assignable_impl<Tp, Up> {};
template<typename Tp, typename Up>
[[maybe_unused]] static constexpr bool is_assignable_v{is_assignable<Tp, Up>::value};
// Is copy assignable
template<typename Tp>
struct is_copy_assignable : public is_assignable<add_lvalue_reference_t<Tp>,
add_const_t<add_lvalue_reference_t<Tp>>>::type {
static_assert(impl::is_complete_or_unbounded_v<Tp>,
"Template argument must be a complete type or an unbounded array");
};
template<typename Tp>
[[maybe_unused]] static constexpr auto is_copy_assignable_v{is_copy_assignable<Tp>::value};
// Is move assignable
template<typename Tp>
struct is_move_assignable : public is_assignable<add_lvalue_reference_t<Tp>,
add_rvalue_reference_t<Tp>>::type{
static_assert(impl::is_complete_or_unbounded_v<Tp>,
"Template argument must be a complete type or an unbounded array");
};
template<typename Tp>
[[maybe_unused]] static constexpr auto is_move_assignable_v{is_move_assignable<Tp>::value};
// Is swappable with
namespace impl
{
template<typename Tp, typename Up>
using is_swappable_with_helper_t = decltype(std::swap(std::declval<Tp>(), std::declval<Up>()));
template<typename , typename, typename = void>
struct is_swappable_with_impl : public false_type {};
template<typename Tp, typename Up>
struct is_swappable_with_impl<Tp, Up, void_t<is_swappable_with_helper_t<Tp, Up>,
is_swappable_with_helper_t<Up,Tp>>> : public true_type {};
}
template<typename Tp, typename Up>
struct is_swappable_with : public conjunction<
impl::is_swappable_with_impl<Tp, Up>
> {
static_assert(impl::is_complete_or_unbounded_v<Tp>,
"Template argument must be a complete type or an unbounded array");
static_assert(impl::is_complete_or_unbounded_v<Up>,
"Template argument must be a complete type or an unbounded array");
};
template<typename Tp, typename Up>
[[maybe_unused]] static constexpr auto is_swappable_with_v{is_swappable_with<Tp,Up>::value};
// Is swappable
template<typename Tp>
struct is_swappable : public conjunction<
impl::is_referenceable<Tp>,
negation<is_void<Tp>>,
is_swappable_with<add_lvalue_reference_t<Tp>,
add_lvalue_reference_t<Tp>>>{
static_assert(impl::is_complete_or_unbounded_v<Tp>,
"Template argument must be a complete type or an unbounded array");
};
// Alternate design
// template<typename Tp>
// struct is_swappable : public conditional_t<
// impl::is_referenceable<Tp>::value && negation_v<is_void<Tp>> ,
// typename is_swappable_with<add_lvalue_reference_t<Tp>,
// add_lvalue_reference_t<Tp>>::type,
// false_type