constexpr new/delete: non‑trivial destructor support?
0 reputation · 22 Feb 2022, 10:30 UTC
constexpr new/delete behavior
The C++20 standard introduced constexpr new and constexpr delete to enable compile‑time dynamic allocation. The specification requires that objects allocated with constexpr new be trivially destructible, but it does not explicitly forbid non‑trivial destructors; instead, it leaves the behavior of constexpr delete ambiguous. Many local builds succeed because compiler extensions (e.g., GCC 13’s -fconstexpr-allocations) allow non‑trivial types, whereas production builds on stricter compilers reject the same code. The unresolved questions include whether constexpr delete may be invoked for non‑trivially destructible objects, whether the deletion must occur at compile time or can be deferred, and how exceptions thrown during construction or destruction affect the lifetime of constexpr‑allocated objects.
- Does the standard permit
constexpr deletefor non‑trivially destructible types? - Should
constexpr deletebe performed at compile time or can it be deferred to runtime? - How do exceptions during constexpr construction or destruction influence object lifetime and deallocation?