Why overload resolution for calling max(x, y) in the expression return max(max(x, y), z); below leads to a function call without a char const* max(char const*, char const*) pattern char const* max(char const*, char const*) ?
When calling this function:
template <typename T> T const& max (T const& x, T const& y, T const& z) { return max (max(x, y), z); }
T is output as const char* . Therefore, this signature is created:
const char* const& max ( const char* const& x, const char* const& y, const char* const& z )
The function internally calls the binary version of max() with arguments of type const char* . Both the pattern and the abnormal overload are viable for an argument of type const char* .
However, when two functions are viable for call resolution, and one of them is not a template, a version other than the template is considered the most suitable .
In paragraph 13.3.3 / 1 of the C ++ 11 standard:
Given these definitions, ** a viable function F1 is defined as a better function than another viable function F2 if ** for all arguments i, ICSi (F1) is no worse than the conversion scheme than ICSi (F2), and then
- for some argument j, ICSj (F1) is a better conversion sequence than ICSj (F2), or, if not this,
- the context is initialization by user conversion (see 8.5, 13.3.1.5 and 13.3.1.6) and the standard conversion sequence from the return type F1 to the destination type (i.e., the initialization type of the object) is a better conversion sequence than the standard conversion sequence from return type F2 to destination type. [...] or, if not this,
- F1 is a function without a template, and F2 is a specialized function of a template , or, if not this,
- F1 and F2 are specialized function templates, and the function template for F1 is more specialized than the template for F2 in accordance with the partial ordering rules described in 14.5.6.2.
This explains why non-template overloading is selected.