简体   繁体   English

POD与非POD类类型的默认初始化

[英]Default initialization of POD vs. non-POD class types

The C++ standard says (8.5/5): C ++标准说(8.5 / 5):

To default-initialize an object of type T means: 默认初始化T类型的对象意味着:

  • If T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor). 如果T是非POD类类型(第9节),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是错误的)。

  • If T is an array type, each element is default-initialized. 如果T是数组类型,则每个元素都是默认初始化的。

  • Otherwise, the object is zero-initialized. 否则,该对象是零初始化的。

With this code 有了这段代码

struct Int { int i; };

int main()
{
    Int a;
}

the object a is default-initialized, but clearly ai is not necessarily equal to 0 . 对象a是默认初始化的,但显然ai不一定等于0。 Doesn't that contradict the standard, as Int is POD and is not an array ? 这与标准是否相矛盾,因为Int是POD而不是数组?

Edit Changed from class to struct so that Int is a POD. 编辑class更改为struct以便Int是POD。

From 8.5.9 of the 2003 standard: 从2003年标准的8.5.9:

If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; 如果没有为对象指定初始化程序,并且该对象是(可能是cv限定的)非POD类类型(或其数组),则该对象应默认初始化; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor. 如果对象是const限定类型,则底层类类型应具有用户声明的默认构造函数。 Otherwise, if no initializer is specified for a nonstatic object, the object and its subobjects, if any, have an indeterminate initial value ); 否则,如果没有为非静态对象指定初始化程序,则该对象及其子对象(如果有)具有不确定的初始值 ); if the object or any of its subobjects are of const-qualified type, the program is ill-formed. 如果对象或其任何子对象是const限定类型,则程序格式错误。

The class you show is a POD, so the highlighted part applies, and your object will not be initialized at all (so section 8.5/5, which you quote, does not apply at all). 您显示的类是POD,因此突出显示的部分适用,并且您的对象根本不会被初始化(因此您引用的第8.5 / 5节根本不适用)。

Edit: As per your comment, here the quote from section 8.5/5 of the final working draft of the current standard (I don't have the real standard, but the FDIS is supposedly very close): 编辑:根据您的评论,这里是当前标准最终工作草案第8.5 / 5节的引用(我没有真正的标准,但据说FDIS非常接近):

To default-initialize an object of type T means: 默认初始化T类型的对象意味着:

— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); - 如果T是一个(可能是cv限定的)类类型(第9节),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是错误的);

— if T is an array type, each element is default-initialized; - 如果T是数组类型,则每个元素都是默认初始化的;

otherwise, no initialization is performed. - 否则,不执行初始化。

Your variable is not initialized. 您的变量未初始化。 Use 使用

Int a = Int();

to initialize your POD or declare a standard constructor to make it non POD; 初始化你的POD或声明一个标准的构造函数,使其成为非POD; But you can also use your POD uninitialized for performance reasons like: 但您也可以出于性能原因使用未初始化的POD,例如:

Int a;
a.i = 5;

No, the object a is not default-initialized. 不,对象a未默认初始化。 If you want to default-initialize it, you have to say: 如果你想默认初始化它,你必须说:

Int a = Int() ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM