15

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;
}

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;
       ^

LIVE DEMO

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

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

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.

6
  • 1
    [dcl.init.list]/5 does not apply because you are doing aggregate initialization of a. Commented Apr 29, 2019 at 13:28
  • Even if a copy can be elided, the copy-constructor needs to exist. Commented Apr 29, 2019 at 13:28
  • @NathanOliver - A is not an aggregate, as it has a constructor. Commented Apr 29, 2019 at 13:30
  • 1
    But A[2] is an aggregate Commented Apr 29, 2019 at 13:31
  • 1
    Possible duplicate of Does copy list initialization invoke copy ctor conceptually? Commented Apr 29, 2019 at 14:48

1 Answer 1

3

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.