以下代码在编译时引发编译器错误。
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
// maximum of two C-strings (call-by-value)
inline char const* max (char const* a, char const* b)
{
return strcmp(a,b) < 0 ? b : a;
}
// maximum of three values of any type (call-by-reference)
template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
return max (max(a,b), c);
}
int main ()
{
::max(7, 42, 68);
}在编译时,我得到了错误:
错误:调用重载的'max(const int&,const int&)‘是不明确的 注:候选人如下: 注: const T& max(const T&,const T&)加上T =int 注: const char* max(const char*,const char*)
当我们有匹配调用的模板方法时,max(const char*,const char*)是如何与max(const int&,const int &)几乎匹配的?
发布于 2013-10-08 09:36:19
我敢打赌你的代码中肯定有using namespace std。把它拿开你就没事了。
比较:http://ideone.com/Csq8SV和http://ideone.com/IQAoI6
如果严格需要使用命名空间std,则可以强制根命名空间调用(在main()中这样做):
template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
return ::max(::max(a,b), c);
}https://stackoverflow.com/questions/19216263
复制相似问题