加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:01:04 所属栏目:百科 来源:网络整理
导读:该代码在Coliru中编译,其警告[在std :: cout中的统一成员a [1] .i和a [2] .i main()]中的表达式,但在 Ideone中正常编译. #include iostreamstruct A{ int i; A(int j) : i{j} {}; A() = default;};int main() { A a[3] = { A(1) }; std::cout a[1].i ' ' a[2
该代码在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-quali?ed) 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-quali?ed) 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上的输出,我不知道如何确定使用的编译器命令行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读