阅读背景:

当参数类型是c ++ 11中的const lvalue reference vs non-const lvalue refernce时,函数模板类型推导如何工作

来源:互联网 
template<typename T>
void fun(T& param) // param is a reference
{
   T abc;
   abc=1;//cannot change abc value because T is deduced as const int
//It gives compilation error because T is deduced as const int       
}

template<typename T>
void fun1(const T& param) // param is a reference
{
   T abc;
   abc=1;//can change abc value because T is deduced as int
//why T is deduced int here
}


int main()
{
int x = 2; // x is an int
const int cx = x; // cx is a const int
const int& rx = x; // rx is a reference to x as a const int
fun(x); // T is int, param's type is int&
fun(cx); // T is const int, param's type is const int&
fun(rx); // T is const int,param's type is const int&


fun1(x); // T is int, param's type is int&
fun1(cx); // T is int, param's type is const int&
fun1(rx); // T is int,param's type is const int&
return 0;
}
template<typename T>
void fun(T& param) // para



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: