为什么在下面的表达式return max(max(x, y), z);
中调用max(x, y)
的重载分辨率会导致调用非模板函数char const* max(char const*, char const*)
?为什么下面的重载分辨率调用非模板函数?
至于我能理解,功能max<const char*>(x, y)
是一个比以前更好的贴合,作为x
是const char* const&
和y
是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);
}
@BoPersson这个例子中的两个功能不具有相同的签名。 – Belloc 2013-03-23 12:20:32