I continue my C++20 concept(ual) jourrney... I would like to simplify the following code by deducing the template parameter T from the predicate argument, so that the client code does not have to precise the type of T if it can be deduced from P1.
I guess it is possible, I just don't know the syntax: I tried various forms of template template + requires clause, but without having a successful compilation.
Any lead?
#include<concepts>
#include<utility>
#include<string>
template<class T, std::predicate<T> P1>
struct Foo
{
P1 _f;
Foo(P1 &&f): _f(std::forward<P1>(f)) {}
};
template<class T, std::predicate<T> P1>
auto make_foo(P1 &&f)
{
return Foo<T, P1>(std::forward<P1>(f));
}
int main()
{
auto fun = [](const std::string &s){return s == "toto";};
// auto my_foo = make_foo(fun); // candidate template ignored: couldn't infer template argument 'T'
auto my_foo = make_foo<std::string>(fun);
return 0;
}