没有临时列表的数组的列表初始化-在GCC中不起作用 [英] List-initialization of an array without temporaries - not working in GCC

查看:48
本文介绍了没有临时列表的数组的列表初始化-在GCC中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下人为设计的示例

Consider the following contrived example

struct A {
    A(int) {}
    A(const A&) = delete;
    ~A() {}
};

struct B {
    A a[2] = {{1}, {2}};
};

int main() {
    B b;
}

它可以在 clang (任何版本)中很好地编译,但在 GCC (任何版本,任何标准> = C ++ 11)中都不能编译

It compiles fine in clang (any version) but not in GCC (any version, any standard >= C++11)

<source>: In constructor 'constexpr B::B()':
<source>:7:8: error: use of deleted function 'A::A(const A&)'
 struct B {
        ^
<source>:3:5: note: declared here
     A(const A&) = delete;
     ^
<source>: In function 'int main()':
<source>:12:7: note: synthesized method 'constexpr B::B()' first required here
     B b;
       ^

实时演示

当A的析构函数被注释掉时,它在GCC中也可以正常编译.

When A's destructor is commented out, it compiles fine also in GCC.

问题是-谁是正确的人,lang或GCC,为什么?

Question is - who is right, clang or GCC, and why?

最初我以为GCC是错的,但是后来我看到了dcl.init.list]/5 指出已创建临时文件.尽管我不确定这是否适用于此,或者是否有其他规则可以覆盖此规则.

Initially I thought GCC is wrong, but then I saw [dcl.init.list]/5 which states that temporaries are created. Though I'm not sure if that applies here or if there's another rule that overrides this.

推荐答案

由于一个数组是一个聚合这样的问题已经已回答 –它没有

Since an array is an aggregate and aggregate initialization comes down to copy-initialization of aggregate members from the corresponding initializer-clauses, the question basically is: does copy-list-initialization (of array elements a[0] and a[1] from {1} and {2}, respectively) require copy constructor, but such question has already been answered — it does not.

顺便说一句,GCC接受 A a = {1}; ,即,它对直接"复制列表初始化没有问题,但是在初始化聚合成员时没有正确对待它.

BTW, GCC accepts A a = {1};, i.e. it has no problem with "direct" copy-list-initialization, but does not treat it correctly when members of aggregates are initialized.

这篇关于没有临时列表的数组的列表初始化-在GCC中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆