![]() |
#2
华子hear2013-10-21 22:31
|

template<typename T>
int compare(T&v1, T &v2){
if(v1<v2)return -1;
if(v1>v2)return 1;
return 0;
}
1.
const int x=1;
const int y=2;
compare(x,y)// 这样的传递是可行的,
2.
compare(1,2) //为什么这样的传递是不可行的。编译器报错:error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'|
模板必须这样定义:
int compare(const T&v1,const T &v2)