vendredi 26 août 2016

Template type deduction with std::function

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 "no instance of function template "stdfunc_test" matches the argument list" error 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?

Aucun commentaire:

Enregistrer un commentaire