https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121068
Bug ID: 121068
Summary: Placement new of array element is rejected at
compile-time
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: tkaminsk at gcc dot gnu.org
Target Milestone: ---
For the following code:
```
consteval int
foo()
{
using T = int;
union { T arr[3]; };
new(arr) T[3];
for (int i = 0; i < 3; ++i)
arr[i].~T();
new (arr + 2) T{10}; // A
return 1;
};
constexpr int g = foo();
```
https://godbolt.org/z/4bdPbqqvz
Placement new at line A is rejected with following error:
<source>:19:22: in 'constexpr' expansion of 'foo()'
<source>:14:23: error: accessing uninitialized member 'foo()::<unnamed
union>::arr'
14 | new (arr + 2) T{10};
| ^
<source>:9:15: note: initializing 'foo()::<unnamed union>::arr' requires a
member access expression as the left operand of the assignment
9 | union { T arr[3]; };
| ^~~
Compiler returned: 1