注册 登录
编程论坛 C++教室

模板形参的实参的首先,

未未来 发布于 2013-10-15 17:29, 437 次点击
程序代码:

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)
1 回复
#2
华子hear2013-10-21 22:31
出现引用了,就是传址,传值当然报错了。常引用是为了保护数据,应该和你想问的问题无关。
           仅供参考
1