Which parts of a modern C++ standard (C++20 or C++23 are fine) say that it’s not okay to do the following things using placement new and explicit destructor calls?
-
Double destructor call
alignas(T) std::byte storage[sizeof(T)]; T* const p = new (storage) T(...); p->~T(); p->~T();
-
Reuse storage without running destructor
alignas(T) std::byte storage[sizeof(T)]; T* p = new (storage) T(...); p = new (storage) T(...) p->~T();
Moreover, are these allowed if T
has a trivial destructor, and which additional parts of the standard are relevant to that question?
I found this previous answer to a semi-related question that gives a relatively clear answer the the trivial destructor part of the question for (1), but it uses C++17 and to my surprise the wording in C++20 seems to have changed to also forbid this for trivial destructors. This seems like it should be fine for trivial destructors, so I suspect I’m missing something.
4