#include <iostream> struct A { int i; A(int j) : i{j} {}; A() = default; }; int main() { A a[3] = { A(1) }; std::cout << a[1].i << ' ' << a[2].i << '\n'; }
根据我对iso§8.5 p7的解释,Ideone是正确的,因为这个条款的第四个要点.
这是N3797的第8.5 p7节
To value-initialize an object of type T means:
- if T is a (possibly cv-qualified) class type (Clause 9) with either no default constructor (12.1) or a default constructor that is
user-provided or deleted,then the object is default-initialized;- if T is a (possibly cv-qualified) class type without a user-provided or deleted default constructor,then the object is zero-initialized
and the semantic constraints for default-initialization are checked,
and if T has a non-trivial default constructor,the object is
default-initialized;- if T is an array type,then each element is value-initialized;
- otherwise,the object is zero-initialized.
An object that is value-initialized is deemed to be constructed and
thus subject to provisions of this International Standard applying to
“constructed” objects,objects “for which the constructor has
completed,” etc.,even if no constructor is invoked for the object’s
initialization.
解决方法
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,the object is zero-initialized.
N3797§8.5 / 8:
To value-initialize an object of type T means:
- if T is a (possibly cv-qualified) class type (Clause 9) with either no default constructor (12.1) or a
default constructor that is user-provided or deleted,then the object is default-initialized;- if T is a (possibly cv-qualified) class type without a user-provided or deleted default constructor,then
the object is zero-initialized and the semantic constraints for default-initialization are checked,and if
T has a non-trivial default constructor,the object is default-initialized;- if T is an array type,the object is zero-initialized.
你的struct A有一个用户声明的默认构造函数A()= default;和用户提供的非默认构造函数A(int j):i {j} {}.在C 11中,它受第一个子弹:它有一个用户提供的构造函数,所以默认构造函数被调用(它什么都不做:A的默认构造函数是微不足道的).在N3797中,由于A是“没有用户提供或删除的默认构造函数”,所以第二个项目符号适用,因此对象为零初始化.
简单地说,在具有任何用户提供的构造函数的类的对象的C 11中的值初始化将不会在默认初始化之前执行零初始化.在N3797中,没有用户提供的默认构造函数的类的对象的值初始化将在默认初始化之前执行零初始化.
看来the version of clang on Coliru一直在跟踪标准C-11,但是GCC 4.8 has not.
编辑:This test program表明GCC 4.8实际上遵循N3797规则进行值初始化.问题似乎是默认 – 初始化不提供初始化程序的数组元素,而不是根据标准所要求的值初始化它们.请注意第二个数组元素之间的行为差异,第二个数组元素明确提供了一个空的初始化器,而第三个不提供初始化器.
这看起来像一个可能的GCC错误.
编辑:The same test program compiled by the same GCC version on Ideone没有显示错误.不知道这里发生了什么也许不同的编译器标志影响了Ideone上的输出,我不知道如何确定使用的编译器命令行.