Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the overload resolution below calls the non-template function? [duplicate]

Why the overload resolution for the call max(x, y) in the expression return max(max(x, y), z); below results in a call to the non-template function char const* max(char const*, char const*) ?

As far as I can understand, the function max<const char*>(x, y) is a better fit than the former, as x is a const char* const& and y is a const char* const& !

#include <iostream>

template <typename T>
T const& max (T const& x, T const& y)
{
    return x < y ? y : x;
}

char const* max (char const* x, char const* y)
{
    return std::strcmp(x, y) < 0 ? y : x;
}

template <typename T>
T const& max (T const& x, T const& y, T const& z)
{
    return max (max(x, y), z);
}

int main ()
{
    const char* sx = "String_x";
    const char* sy = "String_y";
    const char* sz = "String_z";
    max(sx, sy, sz);
}
like image 205
Belloc Avatar asked Mar 23 '13 12:03

Belloc


People also ask

What is the relationship between function templates and overloading?

overloading is used when we have various functions , doing SIMILAR operations . template is used when we have various functions , doing IDENTICAL operations .

Is overloading is possible for template functions?

You may overload a function template either by a non-template function or by another function template. The function call f(1, 2) could match the argument types of both the template function and the non-template function.

What is difference between overloaded function and function template?

Function overloading is used when multiple functions do similar operations; templates are used when multiple functions do identical operations. Templates provide an advantage when you want to perform the same action on types that can be different.

What do you mean by overloading of template function?

Overloading function templates in C++ A template is a tool that reduces the efforts in writing the same code as templates can be used at those places. A template function can be overloaded either by a non-template function or using an ordinary function template.


1 Answers

Why the overload resolution for the call max(x, y) in the expression return max(max(x, y), z); below results in a call to the non-template function char const* max(char const*, char const*)?

When invoking 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 deduced to be const char*. Therefore, this signature is instantiated:

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 template and the non-template overload are viable for an argument of type const char*.

However, when two functions are viable for resolving the call and one of them is not a template, the non-template version is considered a best fit.

Per Paragraph 13.3.3/1 of the C++11 Standard:

Given these definitions,** a viable function F1 is defined to be a better function than another viable function F2 if** for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then

— for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2), or, if not that,

— the context is an initialization by user-defined conversion (see 8.5, 13.3.1.5, and 13.3.1.6) and the standard conversion sequence from the return type of F1 to the destination type (i.e., the type of the entity being initialized) is a better conversion sequence than the standard conversion sequence from the return type of F2 to the destination type. [ ... ] or, if not that,

F1 is a non-template function and F2 is a function template specialization, or, if not that,

— F1 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in 14.5.6.2.

This explains why the non-template overload is picked.

like image 85
Andy Prowl Avatar answered Oct 10 '22 02:10

Andy Prowl