考虑以下人为设计的示例
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;
^
当A的析构函数被注释掉时,它在GCC中也可以正常编译。
问题是-谁是对的,lang语或GCC,为什么?
最初,我认为GCC是错误的,但是后来我看到[dcl.init.list]/5指出创建了临时文件。尽管我不确定这是否适用于此,或者是否有其他规则可以覆盖此规则。
答案 0 :(得分:3)
自an array is an aggregate和aggregate 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}
,即“直接”复制列表初始化没有问题,但是在初始化聚合成员时没有正确处理它。