C ++ 14中标准布局类的定义 [英] Definition of standard-layout class in C++14

查看:103
本文介绍了C ++ 14中标准布局类的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 14的[class]/7中定义了一个 standard-layout 类,如下所示(强调是我的):

A standard-layout class is defined in [class]/7 in C++14, as follows (emphasis is mine):

标准布局类是这样的类:

  • (7.1)—没有非标准布局类型的非静态数据成员 类(或此类数组)或引用,
  • (7.2)-没有虚拟函数(10.3)和虚拟基类 (10.1),
  • (7.3)—对所有非静态对象具有相同的访问控制(条款11) 数据成员,
  • (7.4)—没有非标准布局的基类,
  • (7.5)—在派生最多的数据中没有非静态数据成员 类,最多一个具有非静态数据成员的基类,或者具有 没有具有非静态数据成员的基类,并且
  • (7.6)—没有与第一个非静态类型相同的基类 数据成员.
  • (7.1) — has no non-static data members of type non-standard-layout class (or array of such types) or reference,
  • (7.2) — has no virtual functions (10.3) and no virtual base classes (10.1),
  • (7.3) — has the same access control (Clause 11) for all non-static data members,
  • (7.4) — has no non-standard-layout base classes,
  • (7.5) — either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and
  • (7.6) — has no base classes of the same type as the first non-static data member.

我关心的是关于项目符号(7.5)的问题:如何在最派生的类中拥有 no 非静态数据成员,同时又拥有一个具有非基本数据的基类,静态数据成员?

My concern is in relation to bullet (7.5): how is it possible to have no non-static data members in the most derived class and at the same time having one base class with non-static data members?

换句话说,不是基类的非静态数据成员,还是派生最多的类的非静态数据成员吗?

In other words, aren't the non-static data members of the base class, also non-static data members of the most-derived class?

推荐答案

是的,这是C ++ 14中的一个缺陷,特别是 direct (非继承)非静态数据成员(如在其他地方可能需要的)来修复它,但选择的修复方法是用以下内容替换您发现有问题的语言:

Yes, this is a defect in C++14, specifically CWG 1813. Although it can be repaired by reading "non-static data members" to refer only to direct (non-inherited) non-static data members (as is probably necessary elsewhere), the chosen fix has been to replace the language you found problematic with the following:

标准布局类是一种类:[...]

A standard-layout class is a class that: [...]

  • 该类及其基类中的所有非静态数据成员和位字段都首先在同一类中声明,[...]

这有点棘手.即使 [class.derived] 具有以下优点,对于将继承的成员视为派生类的成员的想法还是存在一些抵触:

This is a little tricky to get right; there is some resistance to the idea of considering inherited members as members of the derived class, even though [class.derived] has:

[...]除非在派生类中重新声明,否则为基类的成员 也被视为派生类的成员.除构造函数外,基类的成员被称为是派生类继承的. [...]

[...] Unless redeclared in the derived class, members of a base class are also considered to be members of the derived class. Members of a base class other than constructors are said to be inherited by the derived class. [...]

尽管如此,在许多地方,直接和继承的非静态数据成员均应被视为专门调用继承的数据成员,例如在 [class] 中(在解决方案之后)到 CWG 1672 ):

Notwithstanding this, a number of places where both direct and inherited non-static data members are intended to be considered specifically call out inherited data members, for example also in [class] (after the resolution to CWG 1672):

8.6-聚合或联合类型,在其元素或非静态数据成员(包括递归地包括子聚合或所包含的联合的元素或非静态数据成员)中包括上述类型之一,

8.6 - an aggregate or union type that includes one of the aforementioned types among its elements or nonstatic data members (including, recursively, an element or non-static data member of a subaggregate or contained union),

或在 [basic.lval] 中:

7.8-如果X是非联合类类型,则如果X没有(可能是继承的(第10条)),则集合M(X)为空. 非静态数据成员; [...]

7.8 - If X is a non-union class type, the set M(X) is empty if X has no (possibly inherited (Clause 10)) non-static data members; [...]

同时,在许多地方,成员"必须理解为仅指直接成员才有意义;首先,仅查看非静态数据成员(不是成员函数),[expr.rel]/3.2,[expr.const]/5.1,[dcl.constexpr]/4.6,[class.mem]/17, /19,[特殊]/5,[class.ctor]/4.3、/4.4、/4.10、/4.12,[class.dtor]/5.6,[class.base.init]/2、/13.3,[class.复制]/12.2、/12.4、/18.2、/25.2、/25.4、/26.3、/27,[spec.spec]/15.1.1.1、/15.2、/15.3都是可以使用非静态数据成员"的地方或应该以直接"作为前缀.

Meanwhile, in many places "members" has to be read as referring only to direct members to make sense; to begin with and only looking at non-static data members (not member functions), [expr.rel]/3.2, [expr.const]/5.1, [dcl.constexpr]/4.6, [class.mem]/17, /19, [special]/5, [class.ctor]/4.3, /4.4, /4.10, /4.12, [class.dtor]/5.6, [class.base.init]/2, /13.3, [class.copy]/12.2, /12.4, /18.2, /25.2, /25.4, /26.3, /27, [except.spec]/15.1.1.1, /15.2, /15.3 are all places where "non-static data members" could or should have "direct" prefixed.

另一方面,在某些地方(例如,在修改后的[class]或[class.copy]/23.2-23.3、/28中)成员"被隐式地包括继承的成员,因此有点一团糟.

On the other hand, in some places (e.g. in the amended [class], or in [class.copy]/23.2-23.3, /28) "members" is implicitly taken to include inherited members, so it's a bit of a mess.

这篇关于C ++ 14中标准布局类的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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