I have the following functions:
template <class InType, class OutType>
OutType foo(const InType &a, std::function<OutType (const InType &)> func)
{
return func(a);
}
template <class T>
T bar(T a, T b)
{
return a + b;
}
I can call them like so:
double x = foo<int, double>(1, [] (const int &v) { return float(v) * 1.5f; });
double y = bar<int>(1.0, 2.0);
...and use template argument deduction with bar
double z = bar(1.0, 2.0);
...but if I try to use template argument deduction with foo:
double w = foo(1, [] (const int &v) { return float(v) * 1.5f; });
It fails with this error:
no matching function for call to 'foo'
double w = foo(1, [] (const int &v) { return float(v) * 1.5f; });
^~~
note: candidate template ignored: could not match 'function<type-parameter-0-1 (const type-parameter-0-0 &)>' against '(lambda at ../path/to/file.cpp:x:y)'
OutType foo(const InType &a, std::function<OutType (const InType &)> func)
^
Why is that? From my vantage point it's obvious what the argument types should be deduced as.