//从左值引用函数参数推断类型
//只能传左值, 若实参为const, 则推断为const 左值引用
template
void f1(T&);
int i = 0;
const int ci = i;
f1(i); //T is int
f1(ci); //T is const int
f1(5); //error
//若函数参数类型为const T&, 则可以传任何类型的实参
//均推断为其基本类型
template
void f2(const T&);
f2(i); //int
f2(ci); //int
f2(5); //int
//从右值引用函数参数推断类型
template
void f3(T&&);
//可以传任何值, 但是:
//传入右值则推断为基本类型
f3(5); //int
//传入左值则转为左值引用
f3(i) //int&
//传入const 的左值转为const 的左值引用
f3(ci) //cnost int&
模版实参的推断和引用
最新推荐文章于 2024-04-04 13:35:56 发布