微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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上的输出,我不知道如何确定使用的编译器命令行.

原文地址:https://www.jb51.cc/c/112668.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐