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+⬑ * 默认成员函数 原来C++类中,有6个默认成员函数: 构造函数 析构函数 拷贝...
#pragma once // 1. 设计一个不能被拷贝的类/* 解析:拷贝只会放生在两个场景中:拷贝构造函数以及赋值运...
C类型转换 C语言:显式和隐式类型转换 隐式类型转化:编译器在编译阶段自动进行,能转就转,不能转就编译...
//异常的概念/*抛出异常后必须要捕获,否则终止程序(到最外层后会交给main管理,main的行为就是终止) try...
#pragma once /*Smart pointer 智能指针;灵巧指针 智能指针三大件//1.RAII//2.像指针一样使用//3.拷贝问...
目录&lt;future&gt;future模板类成员函数:promise类promise的使用例程:packaged_task模板类例程...