How is that possible that it is allowed to delete object with private destructor in the following code? I\'ve reduced real program to the following sample, but it still comp
This code causes undefined behavior (UB). It is UB in C++ to delete
an object of incomplete type having a non-trivial destructor. And in your code the type SomeClass
is incomplete at the point of delete
, and it has a non-trivial destructor. Compilers usually issue a warning about this, since in C++ formally this is not a constraint violation.
So, strictly speaking, your code doesn't "work". It simply compiles and does something undefined when run.
The compiler is just not required to catch this error. The reason for this is that this could be perfectly fine if your object has a trivial destructor. The compiler has no way of knowing what kind of destructor this type will eventually have, so it can't say for sure whether this is an error or not.