https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108324
Bug ID: 108324 Summary: Temporary not bound to reference in default member initializer destroyed early from parenthesized expression-list initialization of aggregate Product: gcc Version: 13.0 Status: UNCONFIRMED Keywords: rejects-valid, wrong-code Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: hstong at ca dot ibm.com Target Milestone: --- While the lifetime-extension rules for temporaries bound to a reference member differ between braced and parenthesized aggregate initialization, the full-expression associated with default member initializers used in the aggregate initialization are the same in both cases ([intro.execution]). It seems GCC thinks that the parenthesized expression-list case causes destruction of temporaries from the default member initializer at an earlier point than in the braced initialization case. ICC does not have this problem (at least when doing constant expression evaluation). Online compiler link: https://godbolt.org/z/aPhffr7xT ### SOURCE (<stdin>): struct A { constexpr A(int x) : x(x) {} constexpr ~A() { x = 13; } int x; constexpr A &ref() { return *this; } }; struct B { const A *ap = &A(42).ref(); }; constexpr int f() { return B{}.ap->x; } constexpr int g() { return B().ap->x; } extern char q[f()]; extern char q[42]; extern char k[g()]; extern char k[42]; ### COMPILER INVOCATION COMMAND: g++ -fsyntax-only -std=c++2b -xc++ - ### ACTUAL OUTPUT: <stdin>:15:16: error: size of array 'k' is not an integral constant-expression <stdin>:16:13: error: conflicting declaration 'char k [42]' <stdin>:15:13: note: previous declaration as 'char k [1]' ### EXPECTED OUTPUT: (Clean compile) ### COMPILER VERSION INFO (g++ -v): Using built-in specs. COLLECT_GCC=/opt/wandbox/gcc-head/bin/g++ COLLECT_LTO_WRAPPER=/opt/wandbox/gcc-head/libexec/gcc/x86_64-pc-linux-gnu/13.0.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../source/configure --prefix=/opt/wandbox/gcc-head --enable-languages=c,c++ --disable-multilib --without-ppl --without-cloog-ppl --enable-checking=release --disable-nls --enable-lto LDFLAGS=-Wl,-rpath,/opt/wandbox/gcc-head/lib,-rpath,/opt/wandbox/gcc-head/lib64,-rpath,/opt/wandbox/gcc-head/lib32 Thread model: posix Supported LTO compression algorithms: zlib gcc version 13.0.0 20230106 (experimental) (GCC)