Multiple Viable Templates
As another example, consider the following call:
const string *sp = &s;cout << debug_rep(sp) << endl;
Here both templates are viable and both provide an exact match:
• debug_rep(const string* &), the instantiation of the first version of the template with T bound to const string*
• debug_rep(const string*), the instantiation of the second version of the template with T bound to const string
In this case, normal function matching can’t distinguish between these two calls. We might expect this call to be ambiguous. However, due to the special rule for overloaded function templates, this call resolves to debug_rep(T*), which is the more specialized template.
The reason for this rule is that without it, there would be no ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access