How to apply a concept to a trailing return type
04:40 10 Nov 2025

Here is some basic example:

template 
constexpr auto foo(T arg, Callable&& make)
    -> decltype(make(std::declval())) {
    return make(arg);
}

Is it possible to apply a constraint to the trailing return type and how?

Now what I'm doing is:

template ()(std::declval()))>
constexpr auto bar(T arg, Callable&& make){
    return make(arg);
}

bar can only be called with callable returning an integral value, which I find a bit convoluted and hard to read (probably a matter of taste though).

This is a full example:

#include 
#include 
#include 

template 
constexpr auto foo(T arg, Callable&& make)
    -> decltype(make(std::declval())) {
    return make(arg);
}

template ()(std::declval()))>
constexpr auto bar(T arg, Callable&& make) {
    return make(arg);
}

double make_double(int i) { return 3.14 * i; }
int make_int(int i) { return i; }

int main() {
    std::cout << foo(1, make_double) << '\n';
    std::cout << foo(1, make_int) << '\n';
    // constraint not fullfilled
    // std::cout << bar(1, make_double) << '\n';
    std::cout << bar(1, make_int) << '\n';
}

LIVE

c++ templates c++20 c++-concepts trailing-return-type