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

发布于 2024-08-11 03:17:56 字数 176 浏览 1 评论 0原文

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

Operator() 允许将数组的所有值设置为 0(所有位都设置为 0)。这称为值初始化。

从哪个版本的 g++ 开始它有效?

那么其他编译器呢?

我在哪里可以找到它的标准?

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

The operator() allow to set all values of array to 0 (all bits to 0). it's called value-initialization.

Since which version of g++ is it valid?

What about other compilers?

Where can I find it in standard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

镜花水月 2024-08-18 03:17:56

这是 C++ 标准的一部分;如果它在 g++ 中无效,则 g++ 不合格。从 C++ 标准 (ISO/IEC 14882:2003) 中,有几个相关部分:

关于新表达式的 5.3.4/15 说:

如果 new-initializer 的形式为 (),则该项目已进行值初始化

8.5/5,关于初始值设定项表示:

对 T 类型的对象进行值初始化意味着:

—如果 T 是具有用户声明的构造函数 (12.1) 的类类型(第 9 条),则调用 T 的默认构造函数(如果 T 没有可访问的默认构造函数,则初始化格式错误);< /p>

—如果 T 是没有用户声明的构造函数的非联合类类型,则 T 的每个非静态数据成员和基类组件都进行值初始化;

——如果T是数组类型,那么每个元素都是值初始化的;

—否则,对象将被零初始化

因此,对于标量类型的整数数组,适用第三个和第四个要点。

This is part of the C++ standard; if it was invalid in g++ then g++ was nonconforming. From the C++ standard (ISO/IEC 14882:2003), several sections are relevant:

5.3.4/15 concerning the new expression says:

If the new-initializer is of the form (), the item is value-initialized

8.5/5 concerning initializers says:

To value-initialize an object of type T means:

— 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);

— 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;

— if T is an array type, then each element is value-initialized;

— otherwise, the object is zero-initialized

So, for an array of ints, which are a scalar type, the third and fourth bullet points apply.

归途 2024-08-18 03:17:56

自 C++98 以来,使用 () 进行初始化(包括您的示例)始终是标准 C++ 的一部分。尽管该标准的新版本有一些更改,但它们不适用于您的示例。

众所周知,GCC 编译器在 2.xx 系列的版本中会错误地处理 () 初始值设定项。众所周知,MSVC++ 编译器在 VC6 中会错误地处理 () 初始值设定项。较新版本的 MSVC++ 根据 C++98 规范处理 () 初始值设定项。

Initialization with () (including your example) was always a part of standard C++, since 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.x.x family. MSVC++ compiler is known to handle () initializers incorrectly in VC6. Newer versions of MSVC++ handle () initializers in accordance with C++98 specification.

猫性小仙女 2024-08-18 03:17:56

这是来自“工作草案,编程标准
语言 C++”,日期为 2009-11-09:

8.5 初始化器
...
7 对类型 T 的对象进行值初始化意味着:

  • 如果 T 是具有用户提供的构造函数 (12.1) 的(可能是 cv 限定的)类类型(第 9 条),则
    调用 T 的默认构造函数(如果 T 没有可访问的默认值,则初始化是错误的
    构造函数);
  • 如果 T 是一个(可能是 cv 限定的)非联合类类型,没有用户提供的构造函数,则该对象
    是零初始化的,并且如果 T 的隐式声明的默认构造函数不平凡,则该构造函数是
    叫。
  • 如果 T 是数组类型,则每个元素都是值初始化的;
  • 否则,该对象将被零初始化。

...

This is from "Working Draft, Standard for Programming
Language C++" dated 2009-11-09:

8.5 Initializers
...
7 To value-initialize an object of type T means:

  • 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);

  • 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.

  • if T is an array type, then each element is value-initialized;
  • otherwise, the object is zero-initialized.

...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文