c++c++20c++-concepts

order of passing parameters to templates in concepts


using the following code

template <typename T>
concept SomeConcept = requires(T t) {
    { t++ } -> std::convertible_to<T>;
};

The concept SomeConcept ensures that the type T has overloaded the operator++(int) and the result type is convertible to T. And std::convertible_to takes 2 parameters the first is From, and the second is To.

So my question is, which parameter takes the result of the expression and why? and by that answer why is T is passed as the other parameter and not the other way around?

please provide a suitable reference to backup the answer, preferably from the standard draft if possible.

Thanks in advance


Solution

  • From cppreference:

    In a type-constraint, a concept takes one less template argument than its parameter list demands, because the contextually deduced type is implicitly used as the first argument of the concept.

    template<class T, class U>
    concept Derived = std::is_base_of<U, T>::value;
     
    template<Derived<Base> T>
    void f(T); // T is constrained by Derived<T, Base>
    

    So, even though std::convertible_to takes two template parameters, the deduced type will be used as the first, which is the From parameter in your case.