Consider following code
template<typename T>
T modify(const T& item, std::function<T(const T&)> fn)
{
return fn(item);
}
When trying to use it as modify(5, [](const int& i){return 10*i;});
it fails to compile with
could not deduce template argument for
'std::function<T(const T &)>
fromlambda
I know that compiler can not deduce T
from lambda, because lambda is not std::function
, but isn't T
already deduced from 5
?
I can get over it using
template<typename T, typename F>
T modify(const T& item, const F& functor)
{
return functor(item);
}
for which previous example compiles, but it is in my opinion less intuitive. Is there a way to let the function argument to remain std::function
and have it's template argument deduced automatically from item
?