]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: constexpr union placement new [PR121068]
authorJason Merrill <jason@redhat.com>
Wed, 16 Jul 2025 15:52:45 +0000 (11:52 -0400)
committerJason Merrill <jason@redhat.com>
Tue, 22 Jul 2025 21:34:56 +0000 (17:34 -0400)
commitfdbc5ff61b471076cc9c758fb6c30d62f7ef1c56
tree6d1f08a4a2c0061fd515b78e9b5fdac612811c50
parent9feecd152fd14a7ba1f4d60fbb988864f07f967e
c++: constexpr union placement new [PR121068]

The note and example in [class.union] p6 think that placement new can be
used to change the active member of a union, but we didn't support that for
array members in constant-evaluation even after implementing P1330 and
P2747.

First I tried to address this by introducing a CLOBBER_BEGIN_OBJECT for the
entire array, but that broke the resolution of LWG3436, which invokes 'new
T[1]' for an array T, and trying to clobber a multidimensional array when
the actual object is single-dimensional breaks.  So I've raised that issue
with the committee.  Until that is resolved, this patch takes a simpler
approach: allow initialization of an element of an array to make the array
the active member of a union.

PR c++/121068

gcc/cp/ChangeLog:

* constexpr.cc (cxx_eval_store_expression): Allow ARRAY_REFs
when activating an array member of a union.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/constexpr-union6.C: Expect x5 to work.
* g++.dg/cpp26/constexpr-new4.C: New test.
gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp26/constexpr-new4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-union6.C