如果该类包含基类的成员,则编译器可以利用空的基优化吗? [英] Can the compiler exploit empty base optimisation if the class contains a member of the base class?

查看:74
本文介绍了如果该类包含基类的成员,则编译器可以利用空的基优化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑

struct base {};
struct child : base {};

众所周知,通过应用空基数优化sizeof(child)可以为1.

It's well-known that sizeof(child) can be 1 by application of the empty base optimisation.

现在,请考虑

struct base {};
struct child : base {base b;};

编译器是否可以立即应用空基优化,或者sizeof(child)必须至少为2?

Can the compiler apply the empty base optimisation now, or must sizeof(child) be at least 2?

参考: http://en.cppreference.com/w/cpp/language/ebo

推荐答案

否,不能.来自同一参考:

No, it cannot. From the same reference:

如果空基类之一是空基优化,则被禁止 也是第一个非静态数据的类型或类型的基础 成员

Empty base optimization is prohibited if one of the empty base classes is also the type or the base of the type of the first non-static data member

因此sizeof(child) >= 2.

这篇关于如果该类包含基类的成员,则编译器可以利用空的基优化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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