int * array = new int [size]() 的有效性;
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 C++ 标准的一部分;如果它在 g++ 中无效,则 g++ 不合格。从 C++ 标准 (ISO/IEC 14882:2003) 中,有几个相关部分:
关于新表达式的 5.3.4/15 说:
8.5/5,关于初始值设定项表示:
因此,对于标量类型的整数数组,适用第三个和第四个要点。
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:
8.5/5 concerning initializers says:
So, for an array of ints, which are a scalar type, the third and fourth bullet points apply.
自 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.这是来自“工作草案,编程标准
语言 C++”,日期为 2009-11-09:
8.5 初始化器
...
7 对类型 T 的对象进行值初始化意味着:
调用 T 的默认构造函数(如果 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:
default constructor for T is called (and the initialization is ill-formed if T has no accessible default
constructor);
is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is
called.
...