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

            Bug ID: 120261
           Summary: GCC rejects valid constexpr value initialization
           Product: gcc
           Version: 15.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: danny321974345 at gmail dot com
  Target Milestone: ---

Here is a minimal example:
class Foo {
    int a = 0;
    int b;
};

int main() {
    constexpr Foo foo{};
}

GCC Output:
<source>: In function 'int main()':
<source>:7:23: error: 'Foo{0}' is not a constant expression
    7 |     constexpr Foo foo{};
      |                       ^
<source>:7:23: error: 'Foo()' is not a constant expression because it refers to
an incompletely initialized variable
Compiler returned: 1

This code compiles successfully with both Clang and MSVC under the C++20
standard.

According to C++20, Foo is a literal type because it has an implicitly declared
constexpr default constructor. Value-initialization via Foo{} zero-initializes
all members, making the object fully initialized and suitable for use in a
constexpr context.

Reply via email to