Could you help me understand why the argument deduction works for the class template and does not work for the function template?
If I understand correctly, the class template defines a function, so when I call it is possible for the compiler to make an implicit cast, but in case of the function template, there is no function definition at the moment, so implicit cast not happening.
But I don't understand why the compiler can't create function definition and then apply implicit cast?
#include <functional>
template<typename ...ARGS>
class Test1
{
public:
void add(const std::function<void(ARGS...)>&) {}
};
class Test2
{
public:
template<typename ...ARGS>
void add(const std::function<void(ARGS...)>&) {}
};
void func(int) {}
int main()
{
Test1<int> test1;
test1.add(func);
Test2 test2;
test2.add<int>(func);
}
The error is:
In function 'int main()':
25:24: error: no matching function for call to 'Test2::add(void (&)(int))'
25:24: note: candidate is:
14:10: note: template void Test2::add(const std::function&)
14:10: note: template argument deduction/substitution failed:
25:24: note: mismatched types 'const std::function' and 'void(int)'