From e3e53bb33b1049e38d64effb5b8822e3a4ba2cbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klaim=20=28Jo=C3=ABl=20Lamotte=29?= <142265+Klaim@users.noreply.github.com> Date: Mon, 8 Apr 2024 00:53:22 +0200 Subject: [PATCH] fixed issues pointed by clang and added related tests --- include/sparrow/mp_utils.hpp | 28 ++++++++++++++-------------- test/test_mpl.cpp | 25 +++++++++++++++++-------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/include/sparrow/mp_utils.hpp b/include/sparrow/mp_utils.hpp index 460f43d2..1deb6229 100644 --- a/include/sparrow/mp_utils.hpp +++ b/include/sparrow/mp_utils.hpp @@ -61,7 +61,7 @@ namespace sparrow::mpl //// Type-predicates ///////////////////////////// /// Matches template types which can be used as type-wrappers for evaluation in type-predicates. - template< template< class > class W, class T > + template< template< class... > class W, class T > concept type_wrapper = std::same_as, typelist> or std::same_as, std::type_identity_t> ; @@ -79,14 +79,14 @@ namespace sparrow::mpl /// `std::type_identity_t`. Basically a value wrapper for representing a type. /// This is useful to detect type predicates which allow being called like normal functions. template< class P, class T > - concept callable_type_predicate = std::semiregular

+ concept callable_type_predicate = std::semiregular> and ( - requires(P predicate) + requires(std::decay_t

predicate) { { predicate(typelist{}) } -> std::convertible_to; } or - requires(P predicate) + requires(std::decay_t

predicate) { { predicate(std::type_identity_t{}) } -> std::convertible_to; } @@ -109,7 +109,7 @@ namespace sparrow::mpl consteval bool evaluate(P predicate) { - if constexpr (requires (P p){ { p(typelist{}) } -> std::same_as; }) + if constexpr (requires (std::decay_t

p){ { p(typelist{}) } -> std::same_as; }) { return predicate(typelist{}); } @@ -131,11 +131,11 @@ namespace sparrow::mpl template struct same_as { - template - consteval - bool operator()(mpl::typelist list) const + template class W, class X> + requires type_wrapper + consteval bool operator()(W) const { - return std::same_as< mpl::typelist, decltype(list) >; + return std::same_as< T, X >; } }; @@ -144,9 +144,9 @@ namespace sparrow::mpl template< template class P > struct ct_type_predicate_to_callable { - template class W, class T> + template class W, class T> requires ct_type_predicate and type_wrapper - consteval bool operator()(W) + consteval bool operator()(W) const { return P::value; } @@ -169,7 +169,7 @@ namespace sparrow::mpl /// @returns 'true' if for at least one type T in the type list L,, `Predicate{}(typelist) == true`. /// `false` otherwise or if the list is empty. template< class Predicate, template class L, class... T> - requires any_typelist> + requires any_typelist> and (callable_type_predicate && ...) consteval bool any_of(L list, Predicate predicate = {}) { @@ -191,7 +191,7 @@ namespace sparrow::mpl /// @returns `true` if for every type T in the type list L, `Predicate{}(typelist) == true` /// or if the list is empty; `false` otherwise. template< class Predicate, template class L, class... T> - requires any_typelist> + requires any_typelist> and (callable_type_predicate && ...) consteval bool all_of(L list, Predicate predicate) { @@ -222,7 +222,7 @@ namespace sparrow::mpl /// @returns The index position in the first type in the provided type list `L` that matches the provided predicate, /// or the size of the list if the matching type was not found. template< class Predicate, template class L, class... T> - requires any_typelist> + requires any_typelist> and (callable_type_predicate && ...) consteval std::size_t find_if(L list, Predicate predicate) { diff --git a/test/test_mpl.cpp b/test/test_mpl.cpp index de470938..c50837eb 100644 --- a/test/test_mpl.cpp +++ b/test/test_mpl.cpp @@ -18,10 +18,16 @@ namespace sparrow { - static_assert(mpl::ct_type_predicate); - static_assert(mpl::callable_type_predicate< mpl::predicate::same_as, float >); + + static constexpr auto some_types = mpl::typelist{}; + static constexpr auto same_as_int = mpl::predicate::same_as{}; + static_assert(same_as_int(mpl::typelist{})); + static_assert( mpl::any_of(some_types, same_as_int) == true); + static_assert(mpl::all_of(some_types, same_as_int) == false); + static_assert(mpl::ct_type_predicate); + static_assert(mpl::callable_type_predicate< mpl::predicate::same_as, float >); using test_list = mpl::typelist< int, char >; struct not_a_list { }; @@ -29,6 +35,15 @@ namespace sparrow static_assert(not mpl::any_typelist); static_assert(mpl::size(test_list{}) == 2); + static_assert(mpl::type_wrapper); + static_assert(mpl::type_wrapper); + + static constexpr mpl::ct_type_predicate_to_callable wtf; + static_assert(wtf(mpl::typelist{})); + + static_assert(mpl::callable_type_predicate< mpl::ct_type_predicate_to_callable, int >); + static constexpr auto is_integral = mpl::as_predicate(); + static_assert(mpl::callable_type_predicate); static_assert(mpl::any_of(test_list{}, mpl::predicate::same_as{})); static_assert(mpl::any_of(test_list{}, mpl::predicate::same_as{})); @@ -45,12 +60,6 @@ namespace sparrow static_assert(mpl::all_of(test_list{})); static_assert(not mpl::all_of(test_list{})); - static constexpr auto some_types = mpl::typelist{}; - static constexpr auto same_as_int = mpl::predicate::same_as{}; - static_assert( mpl::any_of(some_types, same_as_int) == true); - static_assert(mpl::all_of(some_types, same_as_int) == false); - - static_assert(mpl::find_if(test_list{}, mpl::predicate::same_as{}) == 0); static_assert(mpl::find_if(test_list{}, mpl::predicate::same_as{}) == 1); static_assert(mpl::find_if(test_list{}, mpl::predicate::same_as{}) == size(test_list{}));