[Bug c++/87097] New: value-initialization of an array of more than 1 element not treated as a constant initializer

richard-gccbugzilla at metafoo dot co.uk gcc-bugzilla@gcc.gnu.org
Fri Aug 24 19:36:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87097

            Bug ID: 87097
           Summary: value-initialization of an array of more than 1
                    element not treated as a constant initializer
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: richard-gccbugzilla at metafoo dot co.uk
  Target Milestone: ---

Live testcase: https://godbolt.org/z/1JYWJS

For posterity:


struct A {
  constexpr A() : data() {}
  struct X { int n; };
  X data[2];
};

static_assert((A(), true));
static_assert(A().data[0].n == 0);
static_assert(A().data[1].n == 0);
constexpr A x;


GCC rejects this valid code with:

<source>:9:29: error: non-constant condition for static assertion
9 | static_assert(A().data[1].n == 0);
  |               ~~~~~~~~~~~~~~^~~~
<source>:9:29: error: accessing uninitialized member 'A::X::n'

<source>:10:13: error: 'A{A::X [2]{A::X{0}, A::X()}}' is not a constant
expression
10 | constexpr A x;
   |    

Looks like GCC fails to evaluate the initializer of the second and subsequent
elements of a value-initialized array during constant evaluation.


More information about the Gcc-bugs mailing list