Why does compiler allow this?
#include <iostream>
using namespace std;
template<typename T>
void func(T t)
{
cout<<"The value is :"<<t<<"\n";
}
template<>
void func<int>(int d) //Template Specialization
{
cout<<"Template function\n";
}
void func(int d) //Non-template function with same name & signature
{
cout<<"Non-template function\n";
}
int main()
{
func(4);
func(4.67);
func("TENE");
}
The answer to the question lies in section 13.3.3
So amongst the three choices (non-templated
func, specialization offunc<int>, and the generic templatefunc<typename T>, the non-template version offuncis the best. Since there is a perfect match here, there is no reason for the compiler to even look to the templated versions of the function.