博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
remove_reference(引用移除)
阅读量:4110 次
发布时间:2019-05-25

本文共 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] 等同于修改了b

template 
class 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/

你可能感兴趣的文章
C#入门
查看>>
查找最大值最小值
查看>>
杨辉三角
查看>>
C#中ColorDialog需点两次确定才会退出的问题
查看>>
16、Memento 备忘录模式
查看>>
Java基础篇(一)
查看>>
数据库
查看>>
nginx反代 499 502 bad gateway 和timeout
查看>>
linux虚拟机安装tar.gz版jdk步骤详解
查看>>
python猜拳游戏
查看>>
python实现100以内自然数之和,偶数之和
查看>>
python数字逆序输出及多个print输出在同一行
查看>>
ESP8266 WIFI数传 Pixhaw折腾笔记
查看>>
苏宁产品经理面经
查看>>
百度产品经理群面
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
element ui 弹窗在IE11中关闭时闪现问题修复
查看>>
vue 遍历对象并动态绑定在下拉列表中
查看>>
Vue动态生成el-checkbox点击无法选中的解决方法
查看>>
python __future__
查看>>