First, we see that an object of type foo has non-trivial initialization, because its constructor is nontrivial (§3.8 / 1):
It is said that an object has non-trivial initialization if it belongs to a class or an aggregate, and it or one of its members is initialized by a constructor other than the default trivial constructor.
Now we see that an object of type foo lifetime begins after the constructor completes (§ 3.8 / 1):
The lifetime of an object of type T begins when:
- stores with proper alignment and size for type T and
- If the object has non-trivial initialization, its initialization is complete.
Now this behavior is undefined if you delete object before the end of the constructor, if the type foo has a nontrivial destructor (§3.8 / 5):
Before the lifetime of the object was started, but after the storage that will occupy the object was allocated [...], any pointer that refers to the storage location in which the object was or was located, but only in limited paths. For the construction or destruction of an object, see 12.7. Otherwise, [...]
So, since our object is under development, we consider §12.7:
Member functions, including virtual functions (10.3), can be called during construction or destruction (12.6.2).
This means that when creating the self_destruct object self_destruct this is normal. However, this section does not specifically say anything about the destruction of an object during its construction. Therefore, I suggest looking at the work of delete-expression .
First, it "will call the destructor (if any) for the object [...] to be deleted." A destructor is a special case of a member function, so it can be called. However, §12.4 Destructors do not say anything about whether it is well defined when the destructor is called during construction. No luck here.
Secondly, "expression-expression will call the release function" and "release function will free the storage location referenced by". Again, nothing is said about how to do this for storage, which is currently being used as an object under construction.
Therefore, I argue that this behavior is undefined in that the standard did not define it very accurately.
Just note: the lifetime of an object of type foo ends when you start the call to the destructor, because it has a nontrivial destructor. Therefore, if delete this; It occurs until the end of the construction of the object, its life time ends before it is launched . It plays with fire.