加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么sizeof引用类型给你的sizeof的类型?

发布时间:2020-12-16 06:04:03 所属栏目:百科 来源:网络整理
导读:根据标准,在[expr.sizeof](5.3.3.2)中我们得到: When applied to a reference or a reference type,the result is the size of the referenced type. 这似乎与引用未指定的事实一致[dcl.ref](8.3.2.4): It is unspecified whether or not a reference requ
根据标准,在[expr.sizeof](5.3.3.2)中我们得到:

When applied to a reference or a reference type,the result is the size of the referenced type.

这似乎与引用未指定的事实一致[dcl.ref](8.3.2.4):

It is unspecified whether or not a reference requires storage

但是,在语言中有这种矛盾似乎很奇怪.无论参考是否需要存储,能否确定参考使用的尺寸是否重要?看到这些结果似乎错了:

sizeof(vector<int>) == 24
sizeof(vector<int>*) == 8
sizeof(vector<int>&) == 24
sizeof(reference_wrapper<vector<int>>) == 8

根据定义想要sizeof(T&)== sizeof(T)的原因是什么?

解决方法

这种选择是有些武断的,试图充分证明任何一种选择都会导致圆形的形而上学论证.

引用的意图是对象本身的(别名);根据这个推理,他们都有相同的大小(和地址),这就是语言规定的意义.

抽象是泄漏的 – 有时引用有自己的存储,与对象分离,导致像您指出的异常.但是当我们需要处理一个“引用”作为对象的单独实体时,我们有指针.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读