没有临时数组的列表初始化-在GCC中不起作用

时间:2019-04-29 13:23:26

标签: c++ c++11 gcc language-lawyer

考虑以下人为设计的示例

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

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

int main() {
    B b;
}

它可以在 clang (任何版本)中很好地编译,而在 GCC (任何版本,任何标准> = 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;
       ^

LIVE DEMO

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

问题是-谁是对的,lang语或GCC,为什么?

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

1 个答案:

答案 0 :(得分:3)

an array is an aggregateaggregate initialization comes down to copy-initialization of aggregate members from the corresponding initializer-clauses以来,问题基本上是:对a[0]和{中的数组元素a[1]{1}的复制列表初始化吗{1}}分别需要复制构造函数,但是such question已经是answered了-不需要。

顺便说一句,GCC接受{2},即“直接”复制列表初始化没有问题,但是在初始化聚合成员时没有正确处理它。