本文共 684 字,大约阅读时间需要 2 分钟。
在介绍remove_reference之前,我们先看一段代码
int main(){ int a[] = { 1,2,3}; decltype(*a) b = a[0]; a[0] = 4; cout << b; return 0;}
上面的代码会输出什么?
输出为4,因为decltype(*a)返回*a的类型,我们知道*a的类型实际上是int& ,所以此时修改a[0] 等同于修改了btemplateclass remove_reference{ public: typedef T type;};template class remove_reference { public: typedef T type;};
看看remove_reference 的做了什么
他封装了一个普通的模板类,并且typedef T type,主要看第二个,封装了一个引用类型的T& 我们使用时remove_reference<decltype(*a)>,就会被传到第二个实现中。 remove_reference<int &> ,那么typedef int type,此时type就会变为int,解除引用。int main(){ int a[] = { 1,2,3}; remove_reference::type b = a[0]; a[0] = 4; cout << b; //输出1 return 0;}
转载地址:http://nxlsi.baihongyu.com/