max(x, y)
为什么下面表达式中调用的重载解析return max(max(x, y), z);
会导致调用非模板函数char const* max(char const*, char const*)
?
据我所知,该函数max<const char*>(x, y)
比前者更适合,x
aconst char* const&
和y
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);
}