Repository navigation
Description
LWG-3436 "std::construct_at should support arrays" (#5110) was voted into the WP in Nov 2024. We implemented it with #5920 on 2026-02-02. We still have a workaround:
Lines 651 to 656 in da52dc0
| #if defined(__clang__) || defined(__EDG__) // TRANSITION, LLVM-117294, DevCom-10798145 | |
| ::new (static_cast<void*>(_Location)) _Ty(); | |
| return __builtin_launder(_Location); // per old resolution of LWG-3436 | |
| #else // ^^^ workaround / no workaround vvv | |
| _MSVC_CONSTEXPR return ::new (static_cast<void*>(_Location)) _Ty[1](); | |
| #endif // ^^^ no workaround ^^^ |
We currently test with Clang 22, while llvm/llvm-project#117294 was resolved in Clang 23. And A. Jiang (@frederick-vs-ja) reported DevCom-10798145 "EDG doesn't support LWG-3436's resolution yet", but MS mishandled it (by wontfixing it due to "staleness" 😾) so it wasn't reported to EDG. Now that EDG has been open-sourced, I wanted to properly report this bug, but instead it seems that the Standard itself may be broken.
Reduced repro where the MSVC Compiler 19.52 accepts but EDG 6.9 rejects:
C:\Temp>type meow.cpp
#include <new>
// N5054 [specialized.addressof]:
template <class T>
constexpr T* addressof(T& r) noexcept {
return __builtin_addressof(r);
}
// N5054 [specialized.algorithms.general]/4:
template <class T>
constexpr void* voidify(T& obj) noexcept {
return addressof(obj);
}
// N5054 [tab:meta.unary.cat]:
template <class>
constexpr bool is_array_v = false;
template <class T, decltype(sizeof(int)) N>
constexpr bool is_array_v<T[N]> = true;
template <class T>
constexpr bool is_array_v<T[]> = true;
// MSVC uses this attribute to implement "extended constexpr":
#ifdef _MSVC_CONSTEXPR_ATTRIBUTE
#define MSVC_CONSTEXPR [[msvc::constexpr]]
#else
#define MSVC_CONSTEXPR
#endif
// N5054 [specialized.construct]:
template <class T, class... Args>
constexpr T* construct_at(T* location, Args&&... args) {
if constexpr (is_array_v<T>) {
MSVC_CONSTEXPR return ::new (voidify(*location)) T[1]();
} else {
MSVC_CONSTEXPR return ::new (voidify(*location)) T(static_cast<Args&&>(args)...); // std::forward<Args>
}
}
constexpr bool test() {
int a[1];
construct_at(&a);
return a[0] == 0;
}
static_assert(test());C:\Temp>cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.52.36815 for x64 (PREVIEW)
Copyright (C) Microsoft Corporation. All rights reserved.
usage: cl [ option... ] filename... [ /link linkoption... ]
C:\Temp>cl /EHsc /nologo /W4 /std:c++latest /c meow.cpp
meow.cpp
C:\Temp>cl /EHsc /nologo /W4 /std:c++latest /c /BE /dE--version meow.cpp
meow.cpp
Edison Design Group C/C++ Front End, version 6.9 (Sep 15 2026 13:46:37)
Copyright 1988-2026 Edison Design Group, Inc.
"meow.cpp", line 46: error: expression must have a constant value
static_assert(test());
^
"meow.cpp", line 34: note: a placement new-expression cannot be
constant-evaluated
MSVC_CONSTEXPR return ::new (voidify(*location)) T[1]();
^
"meow.cpp", line 42: note: called from:
construct_at(&a);
^
And Clang 23.1.0 rejects https://godbolt.org/z/sYGhGGf49 with:
<source>:46:15: error: static assertion expression is not an integral constant expression
46 | static_assert(test());
| ^~~~~~
<source>:34:31: note: placement new would change type of storage from 'int[1]' to 'int[1][1]'
34 | MSVC_CONSTEXPR return ::new (voidify(*location)) T[1]();
| ^
<source>:42:5: note: in call to 'construct_at<int[1]>(&a)'
42 | construct_at(&a);
| ^~~~~~~~~~~~~~~~
<source>:46:15: note: in call to 'test()'
46 | static_assert(test());
| ^~~~~~
It looks like Clang 23 might be correct, and that LWG-3436's resolution was actually bogus. That might also be what EDG 6.9 is complaining about (with a much less clear diagnostic).
We should determine whether the Standard is wrong (in which case this is
LWG issue needed
Activity
StephanTLavavej commentedon Oct 12, 2026
Jonathan Wakely (@jwakely) Sorry to bother you, but as you submitted LWG-3436 and were involved with improving its wording, and libstdc++ supports Clang 23, have you encountered this?
(It doesn't look like you crafted the specific wording that's problematic here though; the history says "During Core review in Varna, Hubert suggested creating
T[1]for the array case.")StephanTLavavej commentedon Oct 12, 2026
On the STL Discord, seishun observed that CWG-2922 "constexpr placement-new is too permissive" was simultaneously accepted as a DR at the Nov 2024 meeting, and it looks like that's what broke LWG-3436's resolution. (Can't make this stuff up! 🤪)
StephanTLavavej commentedon Oct 12, 2026
And Julian Müller (@muellerj2) observed that the Core wording was further updated by CWG-3010 "constexpr placement-new should require transparent replaceability" in March 2026.
frederick-vs-ja commentedon Oct 13, 2026
I'd say the adopted resolution of LWG-3436 was usually object-structure-breaking, since the newly created
T[1][N]object generally can't transparently replace the intendedly createdT[N]object. The strategy from the resolution from zhihaoy / Zhihao Yuan (@lichray) looked more well-defined.cpplearner commentedon Oct 20, 2026
I guess a concern with the
launder(location)approach is that laundering is no-op in constant expressions. That is, ifTis a const array type, and an object is created bynew(p) T, it is impossible to get a pointer to the newTobject in a constant expression.Though, this concern is moot after LWG-3870, because
std::construct_atdoesn't support cv-qualified types any more.frederick-vs-ja commentedon Oct 20, 2026
Hmm... IMO there's another reason. An array member subobject can be potentially overlapping, in e.g.
Considering the following case:
As
s.ais a potentially-overlapping subobject, we can't make the newly created object transparently replace it, solaunderis necessary for adjustment of the returned pointer.Such use of
[[no_unique_address]]looks pointless. IIUC an array element suobject can't be potentially-overlapping, which forbids saving space. On the other hand, the use[[no_unique_address]]makes UB easier to occur.I attempted to file CWG-2527, after understanding such a use case of
launderin the initial resolution of LWG-3436, to make marking array[[no_unique_address]]have no effect. But it was rejected as NAD.cpplearner commentedon Oct 21, 2026
I'd say, replacing potentially-overlapping subobjects is problematic regardless of laundering.