谭浩强《C++程序设计》关于函数模板的那一节,手痒想试试。就敲了如下代码:
#include <iostream>
using namespace std;
template <typename T>
T max(T t1, T t2){
if (t1 < t2) t1 = t2;
return t1;
}
int main(){
int a1 = 1, a2 = 2;
a1 = max(a1, a2);
cout<<a1<<endl;
return 0;
}
vs2010一直红线提示error:more than one instance of overloaded function *** matches the argument lists
提示表示是因为重载函数max的原因,但我这里只有一个max函数,猜测可能是 iostream中包含了预设的同名函数
go to definition 一下,果然在include\xutility中有同名函数。
但并未直接在iostream中找到此函数。估计是拐了好几个弯被包含进去的。
从这里得到一个经验:千万要注意命名规范。