Describing Predicate with C++ Concepts TS

663 views Asked by At

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?

1

There are 1 answers

0
Johel Ernesto Guerero Peña On BEST ANSWER

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 parameter T.

Regarding:

// requires Predicate<Pred>() // this would accept any, would like this

Concepts test properties of types, so you actually need to specify the argument type A for which a type P should satisfy Predicate<P,A>(). If you could do this without specifying A, then Predicate could not check the specified constraints between P and A, which is what concepts are for.