空基类优化 [英] empty base class optimization

查看:81
本文介绍了空基类优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准第1.8节中的两个引号:

Two quotes from the C++ standard, §1.8:


对象是存储区域。

An object is a region of storage.

基类子对象的大小可能为零。

Base class subobjects may have zero size.

我认为存储区域的大小不能为零。这意味着某些基类子对象实际上不是对象。这些语句如何共存?

I don't think a region of storage can be of size zero. That would mean that some base class subobjects aren't actually objects. How do these statements co-exist?

推荐答案

关于区域定义的哲学争论是不必要的。

A philosophical argument over the definition of "region" is unnecessary.

1.8 / 5说,除非是位域,否则派生最多的对象的大小将为非零...基类子对象的大小可能为零。

1.8/5 says, "Unless it is a bit-field, a most derived object shall have a non-zero size ... Base class sub-objects may have zero size".

因此,标准非常明确,哪些对象(以及哪些存储区域)的大小可以为零。如果您不同意英语中区域一词的标准,那么您可能会批评作者的(与编程无关的)文学技能。为此,您可以指责他们的诗歌技巧(14.7.3 / 7),但是很清楚,标准在这里对类类型的对象的大小说了什么。

So the standard is quite clear what objects (and hence what "regions of storage") can have zero size. If you disagree with the standard what "region" means in English that's one thing, you can fault the authors' (non-programming-related) literary skills. For that matter you can fault their poetic skills (14.7.3/7) But it's quite clear what the standard says here about the sizes of objects of class types.

实用的阅读标准的方式是,给一个单词给出两种合理的解释,选择一个与标准同一部分中的另一句话不直接矛盾的解释。不要选择与您更喜欢该词的个人首选使用方式甚至最常用的一种匹配方式。

The pragmatic way to read standards is that given two plausible interpretations of a word, choose the one which doesn't directly contradict another sentence in the same section of the standard. Don't choose the one which matches more closely your personal preferred use of the word, or even the most common use.

这篇关于空基类优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆