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

kai <nadja at njy dot dev> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nadja at njy dot dev

--- Comment #5 from kai <nadja at njy dot dev> ---
The bug does not depend on the private members or on the default member
initializer of the class in comment 0. It also occurs for the aggregate
below, whose default constructor is implicitly defined and is
non-trivial because the member a has a user-provided default
constructor:

struct A { constexpr A() {} };
struct P { int n; A a; };
static_assert(P().n == 0);

t.cc:3:21: error: non-constant condition for static assertion
t.cc:3:19: error: accessing uninitialized member 'P::n'

This output is from GCC 16.2.0 with -std=c++20. GCC 10.1, 13.3, 15.3
and trunk report the same error. Clang 18.1, Clang trunk and current
MSVC accept the code.

Comments 2 to 4 suggest that a core language issue made this code
valid. It may instead be the C++20 paper P1331R2. Zero-initialization
of the object before the constructor call is required by
[dcl.init]/8.1.2 (N4861). What C++20 changed is that the implicitly
defined default constructor of P is constexpr: C++17
[dcl.constexpr]/4.5 required a constexpr constructor to initialize
every member, and P1331R2 removed that requirement. With -std=c++17 GCC
rejects the code too, and there the rejection is correct.

The same error is reported when P is value-initialized in a constant
expression by new P(), std::construct_at(p), optional<P>(in_place),
optional<P>::emplace(), vector<P>(1) or vector<P>::emplace_back(). P{}
is accepted, because it is aggregate initialization. T() is accepted
when the default constructor of T is trivial. Outside constant
evaluation the generated code zero-initializes n correctly.

PR 105407 appears to be the same bug. There std::construct_at
value-initializes an object during constant evaluation, the
zero-initialization is skipped, and the result is a stale value instead
of an error.

In the front end, build_value_init (init.cc) sets AGGR_INIT_ZERO_FIRST
on the constructor call to request zero-initialization before the call.
The only code that acts on that flag is simplify_aggr_init_expr
(semantics.cc). constexpr.cc does not test it: cxx_eval_call_expression
evaluates the AGGR_INIT_EXPR starting from an empty CONSTRUCTOR with
CONSTRUCTOR_NO_CLEARING set, so during constant evaluation the
zero-initialization does not happen and n is left uninitialized.

Reply via email to