简体   繁体   English

int * array = new int [size]()的有效性;

[英]Validity of int * array = new int [size]();

int * array = new int [size]();

The operator() allow to set all values of array to 0 (all bits to 0). operator()允许将数组的所有值设置为0(所有位都为0)。 it's called value-initialization. 它被称为值初始化。

Since which version of g++ is it valid? 由于哪个版本的g ++有效?

What about other compilers? 那么其他编译器呢?

Where can I find it in standard? 我在哪里可以找到它的标准?

This is part of the C++ standard; 这是C ++标准的一部分; if it was invalid in g++ then g++ was nonconforming. 如果它在g ++中无效,那么g ++是不合格的。 From the C++ standard (ISO/IEC 14882:2003), several sections are relevant: 根据C ++标准(ISO / IEC 14882:2003),有几个部分是相关的:

5.3.4/15 concerning the new expression says: 关于新表达的5.3.4 / 15说:

If the new-initializer is of the form (), the item is value-initialized 如果new-initializer的格式为(),则该项将进行值初始化

8.5/5 concerning initializers says: 8.5 / 5关于初始化者说:

To value-initialize an object of type T means: 对值类型T的对象进行值初始化意味着:

— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); - 如果T是具有用户声明的构造函数(12.1)的类类型(第9节),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是错误的);

— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized; - 如果T是没有用户声明的构造函数的非联合类类型,则T的每个非静态数据成员和基类组件都是值初始化的;

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

— otherwise, the object is zero-initialized - 否则,对象被零初始化

So, for an array of ints, which are a scalar type, the third and fourth bullet points apply. 因此,对于一组标量类型的整数,第三和第四个要点适用。

Initialization with () (including your example) was always a part of standard C++, since C++98. 使用() (包括您的示例)初始化始终是标准C ++的一部分,因为C ++ 98。 Although there were some changes in the newer versions of the standard, they don't apply to your example. 虽然标准的较新版本有一些变化,但它们并不适用于您的示例。

GCC compilers were known to handle () initializers incorrectly in versions from 2.xx family. 已知GCC编译器在2.xx系列的版本中错误地处理()初始化器。 MSVC++ compiler is known to handle () initializers incorrectly in VC6. 已知MSVC ++编译器在VC6中错误地处理()初始化器。 Newer versions of MSVC++ handle () initializers in accordance with C++98 specification. 较新版本的MSVC ++ handle ()初始化程序符合C ++ 98规范。

This is from "Working Draft, Standard for Programming Language C++" dated 2009-11-09: 这是来自2009-11-09的“编程语言C ++标准工作草案”:

8.5 Initializers 8.5初始化器
... ...
7 To value-initialize an object of type T means: 7要初始化T类型的对象,意味着:

  • if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); 如果T是具有用户提供的构造函数(12.1)的(可能是cv限定的)类类型(第9节),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是错误的) ;
  • if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T's implicitly-declared default constructor is non-trivial, that constructor is called. 如果T是一个(可能是cv限定的)非联合类类型而没有用户提供的构造函数,那么该对象是零初始化的,如果T的隐式声明的默认构造函数是非平凡的,则调用该构造函数。
  • if T is an array type, then each element is value-initialized; 如果T是数组类型,那么每个元素都是值初始化的;
  • otherwise, the object is zero-initialized. 否则,该对象被零初始化。

... ...

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

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