Imagine the implementation of std::all_of
in a future C++ standard as such:
// constraining InputIt doesn't concern this question
template<typename InputIt, typename Pred>
bool all_of(InputIt first, InputIt last, Pred pred)
requires Predicate<Pred, decltype(*first)>()
// requires Predicate<Pred>() // this would accept any, would like this
{
for (; first != last; ++first)
if (!pred(*first))
return false;
return true;
}
Where the concept Predicate
is defined as:
template<typename Pred, typename Param>
concept bool Predicate()
{
return requires(Pred p, Param a) {
{ p(a) } -> bool;
};
}
This obviously works as intended. (Also when the function object has defaulted parameters, but isn't that still technically an UnaryPredicate?)
Anyway, having to specify the type which the predicate accepts can sometimes be a burden.
Is there a way to implement such a concept which would return true
for function objects taking one or more parameters of any type where the type isn't explicitly specified?
I don't think there's a reasonable way to express this. Maybe with the reflection proposal it is possible to check that a function object accepts only arguments that are either
auto
or, equivalently, a plain template parameterT
.Regarding:
Concepts test properties of types, so you actually need to specify the argument type
A
for which a typeP
should satisfyPredicate<P,A>()
. If you could do this without specifyingA
, thenPredicate
could not check the specified constraints betweenP
andA
, which is what concepts are for.