-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstexpr_checks.hpp
666 lines (516 loc) · 22.9 KB
/
constexpr_checks.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
/*!
@file
@copyright Barrett Adair 2015
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 CONSTEXPR_CHECKS_HPP
#define CONSTEXPR_CHECKS_HPP
#include <callable_traits/args.hpp>
#include <callable_traits/is_invokable.hpp>
#include <callable_traits/qualified_parent_class_of.hpp>
#include <type_traits>
#include <utility>
#include <tuple>
#ifndef CONSTEXPR_CHECKS_VARIADIC_LIMIT
#define CONSTEXPR_CHECKS_VARIADIC_LIMIT 10
#endif
namespace constexpr_checks {
namespace detail {
#ifdef __cpp_lib_logical_traits
#define CONSTEXPR_CHECKS_NEGATION(...) \
::std::negation< __VA_ARGS__ >
#define CONSTEXPR_CHECKS_DISJUNCTION(...) \
::std::disjunction< __VA_ARGS__ >
#define CONSTEXPR_CHECKS_CONJUNCTION(...) \
::std::conjunction< __VA_ARGS__ >
#else
// polyfill for C++17 std::negation
template<typename BoolType>
using negation = std::integral_constant<bool, !BoolType::value>;
// polyfill for C++17 std::disjunction (from cppreference.com
// example implementation)
template<typename...>
struct disjunction : std::false_type {};
template<typename T>
struct disjunction<T> : T {};
template<typename T, typename... Ts>
struct disjunction<T, Ts...> : std::conditional<
T::value != false, T, disjunction<Ts...>>::type{};
//polyfill for C++17 std::conjunction (from cppreference.com
// example implementation)
template<typename...>
struct conjunction : std::true_type {};
template<typename T>
struct conjunction<T> : T {};
template<typename T, typename... Ts>
struct conjunction<T, Ts...> : std::conditional<
T::value != false, T, conjunction<Ts...>>::type{};
#define CONSTEXPR_CHECKS_NEGATION(...) \
::constexpr_checks::detail::negation<__VA_ARGS__>
#define CONSTEXPR_CHECKS_DISJUNCTION(...) \
::constexpr_checks::detail::disjunction<__VA_ARGS__>
#define CONSTEXPR_CHECKS_CONJUNCTION(...) \
::constexpr_checks::detail::conjunction<__VA_ARGS__>
#endif //#ifdef __cpp_lib_logical_traits
// a faster version of std::decay_t
template<typename T>
using shallow_decay = typename std::remove_cv<
typename std::remove_reference<T>::type>::type;
template<typename T, typename U = shallow_decay<T>>
using is_constexpr_constructible =
std::integral_constant<bool,
std::is_literal_type<U>::value
&& std::is_default_constructible<U>::value>;
template<typename T>
struct is_integral_constant : std::false_type {};
template<typename T, T Value>
struct is_integral_constant<std::integral_constant<T, Value>>
: std::true_type {
};
template<typename Check, typename Result>
using sfinae_if_not_integral_constant =
typename std::enable_if<
is_integral_constant<Check>::value, Result>::type;
template<typename Check, typename WhenTrue, typename WhenFalse>
using if_integral_constant = typename std::conditional<
is_integral_constant<Check>::value, WhenTrue, WhenFalse>::type;
template<typename T, bool Value>
struct type_value : T {
using type = T;
static constexpr const bool value = Value;
};
template<typename Check, typename Result>
using sfinae_if_integral_constant = typename std::enable_if<!
is_integral_constant<Check>::value, Result>::type;
template<typename T>
struct is_reference_wrapper : std::false_type {};
template<typename T>
struct is_reference_wrapper<std::reference_wrapper<T>>
: std::true_type {
};
template<typename T>
struct can_dereference {
template<typename>
struct check {};
template<typename U>
static std::true_type test(
check<typename std::remove_reference<
decltype(*std::declval<U>())>::type>*);
template<typename>
static std::false_type test(...);
static constexpr const bool value =
decltype(test<T>(nullptr))::value;
};
template<typename T, typename = std::true_type>
struct generalize_t {
using type = T;
};
template<typename T>
struct generalize_t<T, std::integral_constant<bool,
can_dereference<T>::value
&& !is_reference_wrapper<T>::value>>{
using type = decltype(*std::declval<T>());
};
template<typename T>
struct generalize_t<T, is_reference_wrapper<T>> {
using type = decltype(std::declval<T>().get());
};
// When T is a pointer, generalize<T> is the resulting type of
// the pointer dereferenced. When T is an std::
// reference_wrapper, generalize<T> is the underlying reference
// type. Otherwise, generalize<T> is T.
template<typename T>
using generalize = typename generalize_t<T>::type;
// make_invokable is used to abstract away the member pointer
// rules of INVOKE for the parent object
template<typename Base, typename T>
using make_invokable = typename std::conditional<
std::is_base_of<Base, shallow_decay<T>>::value
|| std::is_same<Base, shallow_decay<T>>::value,
T, generalize<T>>::type;
template<typename T>
struct arg_tuple_size {
static constexpr int value = -1;
};
template<typename... T>
struct arg_tuple_size<std::tuple<T...>> {
static constexpr int value = sizeof...(T);
};
template<typename T>
struct arity {
template<typename U>
static callable_traits::args<U> test(int);
template<typename>
static std::integral_constant<int, -1> test(...);
using test_result = decltype(test<T>(0));
static constexpr int value = CONSTEXPR_CHECKS_DISJUNCTION(
type_value<if_integral_constant<
test_result, test_result, std::false_type>,
is_integral_constant<test_result>::value>,
type_value<
std::integral_constant<int, arg_tuple_size<
test_result>::value>, true>
)::type::value;
};
template<typename T, typename U = std::true_type>
struct make_constexpr {
static_assert(sizeof(typename std::decay<T>::type) < 1,
"Cannot perform constexpr checks with this type, "
"because either it is not a literal type, or it "
"is not default constructible.");
};
template<typename T>
struct make_constexpr<T, typename is_constexpr_constructible<
shallow_decay<T>>::type> {
using decayed = shallow_decay<T>;
static constexpr decayed value = decayed{};
using const_cast_type = typename std::conditional<
std::is_rvalue_reference<T>::value,
decayed&&, decayed&>::type;
};
// CONSTEXPR_CHECKS_MAKE_CONSTEXPR(T) expands to a reference of a
// constexpr T object. For this to work, T must be a literal type with
// a constexpr default constructor.
#define CONSTEXPR_CHECKS_MAKE_CONSTEXPR(T) \
static_cast<T>( \
const_cast<typename ::constexpr_checks::detail:: \
make_constexpr<T>::const_cast_type>( \
::constexpr_checks::detail:: \
make_constexpr<T>::value)) \
/**/
template<typename T, typename U = shallow_decay<T>>
using int_if_literal = typename std::enable_if<
is_constexpr_constructible<U>::value, T>::type;
struct constexpr_template_worm;
struct constexpr_template_worm {
using type = constexpr_template_worm;
static const constexpr_template_worm value;
template<typename T, int_if_literal<T> = 0>
inline constexpr operator T& () const {
return CONSTEXPR_CHECKS_MAKE_CONSTEXPR(T&);
}
template<typename T, int_if_literal<T> = 0>
inline constexpr operator T && () const {
return CONSTEXPR_CHECKS_MAKE_CONSTEXPR(T&&);
}
inline constexpr constexpr_template_worm() = default;
inline constexpr constexpr_template_worm(
const constexpr_template_worm&) = default;
inline constexpr constexpr_template_worm(
constexpr_template_worm&&) = default;
template<typename... T>
inline constexpr constexpr_template_worm(T&&...) {}
inline constexpr auto operator=(
constexpr_template_worm) const { return type{}; }
template<typename T>
inline constexpr auto operator=(T&&) const { return type{}; }
inline constexpr auto operator+() const { return type{}; }
inline constexpr auto operator-() const { return type{}; }
inline constexpr auto operator*() const { return type{}; }
inline constexpr auto operator&() const { return type{}; }
inline constexpr auto operator!() const { return type{}; }
inline constexpr auto operator~() const { return type{}; }
inline constexpr auto operator()(...) const {
return type{};
}
};
const constexpr_template_worm
constexpr_template_worm::value = {};
//template_worm is only used in unevaluated contexts
struct template_worm : constexpr_template_worm {
//msvc doesn't like this
static constexpr const auto value =
constexpr_template_worm{};
template<typename T>
operator T& () const;
template<typename T>
operator T && () const;
template_worm() = default;
//MSVC doesn't like this because it can deduce 'void'
template<typename... T>
template_worm(T&&...);
template_worm operator+() const;
template_worm operator-() const;
template_worm operator*() const;
template_worm operator&() const;
template_worm operator!() const;
template_worm operator~() const;
template_worm operator()(...) const;
};
#define CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(...) \
template<typename T> \
constexpr inline constexpr_template_worm \
__VA_ARGS__ (constexpr_template_worm, T&&) { return {}; } \
\
template<typename T> \
constexpr inline constexpr_template_worm \
__VA_ARGS__ (T&&, constexpr_template_worm) { return {}; } \
\
constexpr inline constexpr_template_worm \
__VA_ARGS__ (constexpr_template_worm, \
constexpr_template_worm) { return {}; } \
\
template_worm __VA_ARGS__ (template_worm, template_worm); \
\
template<typename T> \
template_worm __VA_ARGS__ (T&&, template_worm); \
\
template<typename T> \
template_worm __VA_ARGS__ (template_worm, T&&); \
/**/
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator+)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator-)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator/)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator*)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator==)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator!=)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator&&)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator||)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator|)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator&)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator%)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator,)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator<<)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator>>)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator<)
CONSTEXPR_CHECKS_TEMPLATE_WORM_BINARY_OPERATOR(operator>)
template<typename T, typename... Args>
struct invoke_info {
static constexpr bool value =
callable_traits::is_invokable<T, Args...>();
static constexpr int arg_count = sizeof...(Args);
};
template<typename T>
struct invoke_info<T, void> {
static constexpr bool value =
callable_traits::is_invokable<T>();
static constexpr int arg_count = 0;
};
struct sentinel {};
template<typename U, typename = void>
struct min_args {
using result_type = CONSTEXPR_CHECKS_DISJUNCTION(
invoke_info<U, void>,
min_args<U, std::make_index_sequence<1>>
);
static constexpr int arg_count = result_type::arg_count;
};
template<typename U>
struct min_args<U, sentinel> {
static constexpr bool value = true;
static constexpr int arg_count = -1;
};
template<typename U, std::size_t... I>
struct min_args<U, std::index_sequence<I...>> {
using next = typename std::conditional<
sizeof...(I)+1 <= CONSTEXPR_CHECKS_VARIADIC_LIMIT,
std::make_index_sequence<sizeof...(I)+1>,
sentinel
>::type;
using result_type = CONSTEXPR_CHECKS_DISJUNCTION(
invoke_info<U, decltype((I, std::declval<
const template_worm&>()))...>,
min_args<U, next>
);
static constexpr bool value = result_type::value;
static constexpr int arg_count = result_type::arg_count;
};
template<typename T>
struct variadic_min_arity {
static constexpr int value = min_args<T>::arg_count;
};
template<typename T>
using min_arity = std::integral_constant<int,
CONSTEXPR_CHECKS_DISJUNCTION(
type_value<arity<T>, arity<T>::value >= 0>,
type_value<variadic_min_arity<T>, true>
)::type::value
>;
template<typename>
struct test_invoke_constexpr {
// An arbitrary expression as a left-hand argument in a comma
// operator expression passed as a template value argument will
// SFINAE when the expression is not constexpr.
template<typename T, typename... Rgs,
typename U = typename std::remove_reference<T>::type>
inline constexpr auto
operator()(T&& t, Rgs&&...) const
->sfinae_if_not_integral_constant<U,
std::integral_constant<bool, (U::value(
CONSTEXPR_CHECKS_MAKE_CONSTEXPR(Rgs&&)...
), true)>>;
template<typename T, typename... Rgs,
typename U = typename std::remove_reference<T>::type>
inline constexpr auto
operator()(T&&, Rgs&&...) const
->sfinae_if_integral_constant<U,
std::integral_constant<bool,
(CONSTEXPR_CHECKS_MAKE_CONSTEXPR(T&&)(
CONSTEXPR_CHECKS_MAKE_CONSTEXPR(Rgs&&)...
), true)>>;
inline constexpr auto
operator()(...) const -> std::false_type;
};
template<typename Member, typename Class>
struct test_invoke_constexpr<Member Class::*> {
// Here, P is an integral_constant pointer to member fn.
template<typename P, typename U, typename... Rgs,
typename Obj = make_invokable<Class, U&&>>
inline constexpr auto
operator()(P&& p, U&&, Rgs&&... rgs) const
->sfinae_if_not_integral_constant<P,
std::integral_constant<bool,
((CONSTEXPR_CHECKS_MAKE_CONSTEXPR(Obj).*
std::remove_reference<P>::type::value)(
CONSTEXPR_CHECKS_MAKE_CONSTEXPR(Rgs&&)...
), true)>>;
inline constexpr auto
operator()(...) const->std::false_type;
};
template<typename... Ts>
using are_all_constexpr_constructible =
CONSTEXPR_CHECKS_CONJUNCTION(
is_constexpr_constructible<Ts>...);
template<typename T, bool = is_integral_constant<T>::value>
struct unwrap_integral_constant {
using type = T;
};
template<typename T>
struct unwrap_integral_constant<T, true> {
using type = shallow_decay<decltype(T::value)>;
};
template<typename T>
struct unwrap_if_integral_constant {
using no_ref = shallow_decay<T>;
using unwrapped =
typename unwrap_integral_constant<no_ref>::type;
using type = typename std::conditional<
std::is_class<unwrapped>::value, T, unwrapped>::type;
};
template<typename T, typename... Args, typename std::enable_if<
CONSTEXPR_CHECKS_NEGATION(are_all_constexpr_constructible<
T, Args...>)::value, int>::type = 0>
inline constexpr std::false_type
is_constexpr_invokable_impl(T&&, Args&&...) { return{}; }
template<typename T, typename... Args, typename std::enable_if<
are_all_constexpr_constructible<T, Args...>::value,
int>::type = 0>
inline constexpr auto
is_constexpr_invokable_impl(T&& t, Args&&... args) {
using type =
typename unwrap_if_integral_constant<T&&>::type;
using test = test_invoke_constexpr<type>;
using result = decltype(test{}(::std::forward<T>(t),
::std::forward<Args>(args)...));
return std::integral_constant<bool, result::value>{};
}
template<bool, typename T, typename... Args>
struct is_constexpr_invokable_impl_types {
using type = std::false_type;
};
template<typename T, typename... Args>
struct is_constexpr_invokable_impl_types<true, T, Args...> {
using test_type =
typename unwrap_if_integral_constant<T>::type;
using test = test_invoke_constexpr<test_type>;
using result = decltype(test{}(
::std::declval<T>(),
::std::declval<Args>()...));
using type = std::integral_constant<bool, result::value>;
};
template<typename F, typename Seq>
struct is_constexpr_t;
template<typename F, std::size_t... I>
struct is_constexpr_t<F, std::index_sequence<I...>> {
using worm =
::constexpr_checks::detail::constexpr_template_worm;
template<typename T,
typename U = typename std::remove_reference<T>::type>
inline constexpr auto
operator()(T&&) const
->sfinae_if_not_integral_constant<U,
std::integral_constant<bool, (static_cast<void>(U::value(
worm{ I }...
)), true)>>;
template<typename T,
typename U = typename std::remove_reference<T>::type>
inline constexpr auto
operator()(T&&) const
->sfinae_if_integral_constant<U,
std::integral_constant<bool,
(static_cast<void>(CONSTEXPR_CHECKS_MAKE_CONSTEXPR(T&&)(
worm{ I }...
)), true)>>;
inline constexpr auto
operator()(...) const->std::false_type;
};
template<typename Member, typename Class, std::size_t... I>
struct is_constexpr_t<Member Class::*,
std::index_sequence<I...>> {
using worm =
::constexpr_checks::detail::constexpr_template_worm;
using invoke_type =
callable_traits::qualified_parent_class_of<
Member Class::*>;
template<typename T>
inline constexpr auto
operator()(T&& t) {
return decltype(::constexpr_checks::detail::
test_invoke_constexpr<Member Class::*>{}(
::std::forward<T>(t),
::std::declval<invoke_type>(),
worm{ I }...)){};
}
};
template<typename T>
inline constexpr std::false_type
is_constexpr_impl(T&&, std::false_type) { return{}; }
template<typename T>
inline constexpr auto
is_constexpr_impl(T&& t, std::true_type) {
using type =
typename unwrap_if_integral_constant<T&&>::type;
using this_offset = std::integral_constant<int,
std::is_member_pointer<type>::value ? 1 : 0>;
// need to remove the INVOKE-required object
using min_args = std::integral_constant<int,
min_arity<type>::value - this_offset::value>;
using seq = std::make_index_sequence<
min_args::value < 0 ? 0 : min_args::value>;
return decltype(is_constexpr_t<type, seq>{}(
::std::forward<T>(t))){};
}
}
template<typename T, typename... Args>
inline constexpr auto
is_constexpr_invokable(T&& t, Args&&... args) {
return detail::is_constexpr_invokable_impl(
::std::forward<T>(t),
::std::forward<Args>(args)...
);
}
template<typename... Args>
inline constexpr auto
is_constexpr_invokable() {
using are_constexpr_constructible =
detail::are_all_constexpr_constructible<Args...>;
return typename detail::is_constexpr_invokable_impl_types<
are_constexpr_constructible::value, Args...>::type{};
}
template<typename T>
inline constexpr auto
is_constexpr(T&& t) {
return ::constexpr_checks::detail::is_constexpr_impl(
::std::forward<T>(t),
detail::is_constexpr_constructible<T>{}
);
}
template<typename T>
inline constexpr auto
is_constexpr() {
return decltype(::constexpr_checks::
is_constexpr(std::declval<T>())){};
}
}
#endif //#ifndef CONSTEXPR_CHECKS_HPP