Skip to content

Commit

Permalink
Add predicate-to-unary binder
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-golubev committed Feb 24, 2023
1 parent 70c5c13 commit f0dcd0b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions algorithm/type_algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ template<template<typename U> typename UnaryPred, typename T, typename... Ts> st
static constexpr bool value = !any_of<UnaryPred, T, Ts...>::value;
};

/*! \brief Converts \a Pred predicate into a unary predicate by binding first
\c{N - 1} types of \a Us to \a Ts.
*/
template<template<typename... Us> typename Pred, typename... Ts> struct bind_to_unary {
template<typename T> using type = Pred<Ts..., T>;
};
} // namespace clsc
15 changes: 15 additions & 0 deletions tests/type_algorithm_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,18 @@ TEST(type_algorithm_tests, none_of) {
static_assert(!clsc::none_of<std::is_function, int, decltype(foofunc)>::value);
static_assert(!clsc::none_of<std::is_fundamental, decltype(nullptr)>::value);
}

class Base {};
class Derived : Base {};
template<typename T, typename U, typename X> struct three_way_base_of {
static constexpr bool value = std::is_base_of_v<T, U> && std::is_base_of_v<U, X>;
};

TEST(type_algorithm_tests, bind_to_unary) {
static_assert(clsc::bind_to_unary<std::is_same, float>::type<float>::value);
static_assert(!clsc::bind_to_unary<std::is_convertible, float>::type<float*>::value);
class DerivedDerived : Derived {};
static_assert(
clsc::bind_to_unary<three_way_base_of, Base, Derived>::type<DerivedDerived>::value);
static_assert(clsc::any_of<clsc::bind_to_unary<std::is_same, int>::type, int, double>::value);
}

0 comments on commit f0dcd0b

Please sign in to comment.