I have discovered the following behaviour with std::function
and type deduction, which was unexpected for me:
#include <functional>
template <typename T>
void stdfunc_test(std::function<T(T)> func) {};
int test_func(int arg)
{
return arg + 2;
}
int main()
{
stdfunc_test([](int _) {return _ + 2;});
stdfunc_test(test_func);
}
Both lines in main
result in error:
no instance of function template "stdfunc_test" matches the argument list
When attempting to compile in Visual Studio 2015.
Why doesn't the type deduction deduct template type from the function type, and is there a workaround for it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…