Do I have to explicitly instantiate a function template's type when it comes to reference type deduction. If that is the case, where is the ambiguity? Let's compare following 2 code snippets:
1st: link for the code
template <typename T>
void foo(T& var, void(*func)(T&)) // T must be instantiated with int and it does .
{
++var;
}
void ret(int & var){}
int main()
{int k =7;
foo(k, &ret);
cout<<k;//prints 8
}
Now let's remove &'s in foo()'s decleration and we have an error.
2nd: link for the code
template <typename T>
void foo(T var, void(*func)(T)) // T must be instantiated with int& but it doesn't.
{
++var;
}
void ret(int & var){}
int main()
{int k =7;
foo(k, &ret); //error: no matching function for call to 'foo(int&, void (*)(int&))'
cout<<k;
}
However, If I call foo by explicitly instantiating with <int&>
"foo<int&>(k,&ret);
", the code gives the same output as the former one. What is the reason of this error? Where is the ambiguity?
Thanks.