不小心翻阅到的一段关于POD的资料,总结记录在案

什么是POD?这是一个问题.我甚至很难找到2份完全相同的答案.

 

摘自文档ISO/IEC 14882:2003(E) P153:

...A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct,

non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator

and no user-defined destructor. Similarly, a POD-union is an aggregate union that has no non-static data

members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-

defined copy assignment operator and no user-defined destructor. A POD class is a class that is either a

POD-struct or a POD-union.

(我连换行什么的格式都一并拷贝了下来)

 

然而我们的wiki百科在上述这段话的背后又加注了一下面段话,让我更疑惑了.注意我上面贴的那段话也几乎出现在维基中的这段话前面,我说几乎,是个别措辞有些区别并且没有提union,但是意思可以说是不多不少,恰好等价.

http://en.wikipedia.org/wiki/Plain_Old_Data_Structures

(A POD type in C++ is defined as either a scalar type or a POD class. A POD class has no user-defined copy assignment operator, no user-defined destructor, and no non-static data members that are not themselves PODs.)Moreover, a POD class must be an aggregate, meaning it has no user-declared constructors, no private nor protected non-static data, no bases and no virtual functions.

 

这个Moreover(我觉得该Morever的)就彻底让我糊涂了:这是维基志愿编辑者画蛇添足呢,还是真的在ISO文档中确实有这段话,我自己没有找到.因为第一段话我也仅仅是在一个介绍Class的角落中无意找到了,从编撰的格式上来说不像正式定义, 措辞上也仅仅用了"that" 这样的描述性词汇,似乎是在描述其部分特性而不是严格的对其进行定义.

 

不过可以肯定的是,在我们通常用的编译器中,有虚函数的肯定不是POD.这一点让我有些怀疑POD是一个实现定义的标准,标准仅仅给出了其最小要求,然后具体的各个编译器根据其对象模型还各自加了一些要求.

 

我们不能否定可能存在某个编译器,在其虚函数的实现模型中没有在对象中插入数据,也使得其构造过程不需要设置虚表指针之类的( 全局表?). 也不能排除某个编译器可以做出超牛的inline优化, 让构造函数的执行隐藏在了某些C语言标准中的未定义行为中(纯属笔者臆想哈).不然我也实在没法给个理由让自己相信:需要初始化才能用的对象居然还是POD?

 

我见过的最常见的POD要求是(个人总结):

1.ISO标准中的要求.

2.没有虚函数和自定义的任何构造函数. ISO标准只禁止了重载operator "="和析构函数.

3.没有基类.

4.没有非静态的常量和引用类型成员. 这2个玩意儿没有自定义构造函数没法初始化.

5.无虚函数和自定义的任何构造函数的空类是非POD, 继承空类不会影响子类是否是POD的判断. 空类被打成非POD我怀疑仅是为了允许参数传递和返回值优化.

 

C++ 0x标准草案最新POD定义(好像已经投票通过了):

 

http://en.wikipedia.org/wiki/C%2B%2B0x

 

A class/struct is considered a POD if it is trivialstandard-layout, and if all of its non-static members are PODs.

A trivial class or struct is defined as one that:

  1. Has a trivial default constructor. This may use the default constructor syntax (SomeConstructor() = default;).
  2. Has a trivial copy constructor, which may use the default syntax.
  3. Has a trivial copy assignment operator, which may use the default syntax.
  4. Has a trivial destructor, which must not be virtual.

A standard-layout class or struct is defined as one that:

  1. Has only non-static data members that are of standard-layout type
  2. Has the same access control (public, private, protected) for all non-static data members
  3. Has no virtual functions
  4. Has no virtual base classes
  5. Has only base classes that are of standard-layout type
  6. Has no base classes of the same type as the first defined non-static data member
  7. Either has no base classes with non-static data members, or has no non-static data members in the most derived class and at most one base class with non-static data members. In essence, there may be only one class in this class's hierarchy that has non-static data members.

 

中文版:

当class/struct是极简的(trivial)、属于标准布局(standard-layout),以及他的所有非静态(non-static)成员都是POD时,会被视为POD。

一个极简的类或结构符合以下定义:

  1. 极简的默认建构式。这可以使用默认建构式语法,例如SomeConstructor() = default;
  2. 极简的复制建构式,可使用默认语法(default syntax)
  3. 极简的赋值操作符,可使用默认语法(default syntax)
  4. 极简的解构式,不可以是虚拟的(virtual)

一个标准布局(standard-layout)的类或结构符合以下定义:

  1. 只有非静态的(non-static)数据成员,且这些成员也是符合标准布局的类型
  2. 对所有non-static成员有相同的访问控制(public, private, protected)
  3. 没有虚函数
  4. 没有虚拟基类
  5. 只有符合标准布局的基类
  6. 没有和第一个定义的non-static成员相同类型的基类
  7. 若非没有带有non-static成员的基类,就是最底层(继承最末位)的类没有non-static数据成员而且至多一个带有non-static成员的基类。基本上,在该类的继承体系中只会有一个类带有non-static成员
 个人观点, 仅供参考. 欢迎达人指点.
------------------------------------------------补正----------------------------------------------------------------

...A POD-struct is an aggregate class that:经过高人指点,标准中这句话的"that"使用不当,应当替换为"and"!

也就是说后面的从句不是解释aggregate 而是 what's more的意思!

 

aggregate 的定义:An aggregate class is a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值