【发布时间】:2020-01-12 03:45:51
【问题描述】:
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
template <class T>
T max(T const& t1, T const& t2) {
return t1 < t2 ? t2 : t1;
}
int main() {
cout << max(1, 2) << endl;
cout << max<double>(1.2, 2) << endl;
string s1 = "hello";
string s2 = "world";
--> cout << max(s1, s1) << endl;
}
在带箭头的那一行,它抱怨: "函数模板 "max" 的多个实例与参数列表匹配:-- 函数模板 "const _Tp &std::__1::max(const _Tp &__a, const _Tp &__b)"-- 函数模板 "T max(const T &t1, const T &t2)" -- 参数类型为:(std::__1::string, std::__1::string)"
我很困惑,因为它们都是字符串,不确定模板还能匹配什么。
【问题讨论】:
-
与 std::max 冲突?如果更改名称会怎样?