For our next example, we’ll define an ordinary nontemplate version of debug_rep
to print string
s inside double quotes:
// print strings inside double quotes
string debug_rep(const string &s)
{
return '"' + s + '"';
}
Now, when we call debug_rep
on a string
,
string s("hi");
cout << debug_rep(s) << endl;
there are two equally good viable functions:
• debug_rep<string>(const string&)
, the first template with T
bound to string
• debug_rep(const string&)
, the ordinary, nontemplate function
In this case, both functions have the same parameter list, so obviously, each function provides an equally good match for this call. However, the nontemplate version is selected. For the same reasons that the most specialized of equally good function templates is preferred, a nontemplate function is preferred over equally good match(es) to a function template.