c – 代码段在Coliru中编译警告,但在Ideone中正常编译.哪一个是正确的?

代码在Coliru中编译,其警告[在std :: cout中的统一成员a [1] .i和a [2] .i<< main()]中的表达式,但在 Ideone中正常编译.
#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.

解决方法

对于具有认构造函数和另一个认构造函数的类,C 11和N3797之间的值初始化行为有明显的区别. C 11§8.5 / 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,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上的输出,我不知道如何确定使用的编译器命令行.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...