Bug 87097 - value-initialization of an array of more than 1 element not treated as a constant initializer
Summary: value-initialization of an array of more than 1 element not treated as a cons...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: 14.3
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2018-08-24 19:36 UTC by Richard Smith
Modified: 2025-07-17 14:06 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 14.3.0
Known to fail:
Last reconfirmed: 2021-12-07 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Smith 2018-08-24 19:36:01 UTC
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.
Comment 1 Marek Polacek 2018-08-24 19:39:14 UTC
Confirmed.  Doesn't seem to be a regression.
Comment 2 Drea Pinski 2021-12-07 11:51:17 UTC
MSVC has the same bug :).
Comment 3 Drea Pinski 2021-12-07 11:52:07 UTC
(In reply to Andrew Pinski from comment #2)
> MSVC has the same bug :).

Well MSVC rejects it even for array size of 1.
Comment 4 Jason Merrill 2025-07-17 14:00:05 UTC
This seems to have been fixed by r15-7260 for PR118285.
Comment 5 GCC Commits 2025-07-17 14:05:10 UTC
The trunk branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:b38382e36410a9a649b904a2d0a0abcb90f9c418

commit r16-2326-gb38382e36410a9a649b904a2d0a0abcb90f9c418
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Jul 17 09:46:35 2025 -0400

    c++: constexpr array testcase [PR87097]
    
    This seems to have been fixed by r15-7260 for PR118285, but is sufficiently
    different to merit its own test.
    
            PR c++/87097
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/constexpr-array29.C: New test.
Comment 6 GCC Commits 2025-07-17 14:05:38 UTC
The releases/gcc-15 branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:10846d82a895cbc95b7819a5c44cd0216bb21bf4

commit r15-9990-g10846d82a895cbc95b7819a5c44cd0216bb21bf4
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Jul 17 09:46:35 2025 -0400

    c++: constexpr array testcase [PR87097]
    
    This seems to have been fixed by r15-7260 for PR118285, but is sufficiently
    different to merit its own test.
    
            PR c++/87097
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/constexpr-array29.C: New test.
Comment 7 GCC Commits 2025-07-17 14:06:00 UTC
The releases/gcc-14 branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:d7dd08411bc3b4e8431174d036021ecf482e7d61

commit r14-11894-gd7dd08411bc3b4e8431174d036021ecf482e7d61
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Jul 17 09:46:35 2025 -0400

    c++: constexpr array testcase [PR87097]
    
    This seems to have been fixed by r15-7260 for PR118285, but is sufficiently
    different to merit its own test.
    
            PR c++/87097
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/constexpr-array29.C: New test.