Nontemplate and Template Overloads

For our next example, we’ll define an ordinary nontemplate version of debug_rep to print strings 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.


Image Note

When a nontemplate function provides an equally good match for a call as a function template, the nontemplate version is preferred.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.88.54