0

I encountered this problem while initializing a member array variable (c-style). Interestingly converting the member into a std::array<> solves the problem. See below:

struct A {
  A(int aa) : a(aa) {}

  A(const A &a) = delete;
  A &operator=(const A &a) = delete;

private:
  int a;
  std::vector<int> v;
};

struct B1 {
  B1() : a1{{{1}, {2}}} {} // -> compiles OK (gcc 9.2)

private:
  std::array<A, 2> a1;
};

struct B2 {
  B2()
    : a2{{1}, {2}} // -> error: use of deleted function 'A::A(const A&)' (gcc 9.2)
  {} 

private:
  A a2[2];
};

Demo

My question is why is this difference in (compiler) behavior? I was assuming that functionality-wise they are pretty much the same (I understand std::array<> is more of an aggregate instead of a pure container, like vector - I might be wrong though).

Additional observation:

  • The compiler allows c-style array if I remove the vector<> member in A
  • GCC 9.5 does not raise any issues. Does it mean it's a GCC bug (I couldn't find anything on the release notes)?

Update:
It is a GCC bug: Brace initialization of array sometimes fails if no copy constructor (reported in 4.9 and resolved in 9.4)

avikpram
  • 47
  • 5
  • 1
    Change the compiler version you're using. The program works starting from gcc 9.4. [Demo](https://godbolt.org/z/sYrb1cEj8) – Jason Aug 31 '22 at 11:57
  • @JasonLiam I noticed that the program works in GCC > 9.5 and onwards. Does that mean it was a GCC bug or improvement? I could not find any official explanation for this (bug report and/or release notes). – avikpram Aug 31 '22 at 12:00
  • Yes, this seems to be a gcc bug which has been submitted as: [GCC rejects valid program involving initialization of array in member initializer list](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106788) – Jason Aug 31 '22 at 12:12

1 Answers1

1

Does it mean it's a GCC bug

Yes, this seems to be a gcc bug that has been fixed in gcc 9.4. Demo.

A bug for the same has been submitted as:

GCC rejects valid program involving initialization of array in member initializer list

Jason
  • 36,170
  • 5
  • 26
  • 60