Using a union to prevent destruction?

Solution 1:

[class.dtor]/15 says that a program is ill-formed if a potentially invoked destructor is defined as deleted.

[class.base.init]/12 says that a destructor of a potentially constructed subobject of a class is potentially invoked in a non-delegating constructor.

[special]/7 says that all non-static data members are potentially constructed subobjects, although the sentence is qualified a bit weirdly I think ("For a class, [...]").

I don't see any exceptions in this for single-member classes, unions or noexcept. So it seems to me that GCC is correct for S1.

Clang is also the only compiler out of Clang, GCC, ICC and MSVC that doesn't reject this version.

For S0 however, this reasoning would also apply, making it ill-formed as well. However none of the four compilers agrees on that, so maybe I am wrong.


The quoted wording comes mostly from CWG issue 1424. Clang lists its implementation status currently as unknown (https://clang.llvm.org/cxx_dr_status.html), as does GCC (https://gcc.gnu.org/projects/cxx-dr-status.html).