I had created a Template Specialized function and a non template function with the same arguments. Since effectively both are same functions, I was not sure how C++ compiler would run it as now it has two same functions, one is a template specialized the other is a non template one. I was expecting this would result in a compiler error as compiler would find two functions with the same parameters and return type (In this case void foo(string) ). But looks like the non template version is the one that executes when this is called. So is there a precedence when this is done?
Kindly let me know if I have misunderstood.
Code: This prints 'String Non-Template'
#include <iostream>
#include <string>
using namespace std;
template<typename T>
void foo(T input)
{
cout <<"Generic Template"<<endl;
}
template<>
void foo<string>(string input)
{
cout <<"String Template"<<endl;
}
void foo(string input)
{
cout <<"String Non-Template"<<endl;
}
int main() {
string input = "abc";
foo(input);
return 0;
}