I used to think that in C ++, if a constructor throws an exception, the destructor of this "partially constructed" class is not called.
But it seems like this is not the case in C ++ 11
This is still the case. Nothing has changed since C ++ 03 (for some value, nothing ;-))
That, in your opinion, is still true, but there is no partial object when an exception is thrown.
The C ++ 03 standard TC1 says (focus):
An object, partially constructed or partially destroyed, will have destructors executed for all its fully constructed subobjects, that is, for subobjects for which the constructor has completed execution and the destructor has not yet started execution.
i.e. Any object that completed its constructor will be destroyed by executing the destructor. This is a simple rule.
Basically, the same rule applies in C ++ 11: as soon as X(int) returns, the constructor object has completed execution, therefore it is completely constructed and therefore its destructor will work at the appropriate time (when it goes beyond the scope or exception arises at some later stage of its construction.) Still the same rule.
The body of the delegation constructor is executed after another constructor and can do additional work, but this does not change the fact that the construction of the object is completed, therefore it is completely built. The delegation constructor is similar to the constructor of a derived class, which executes more code after the constructor of the base class completes. In a sense, you can consider your example like this:
class X { public: X(int a) { cout << "X::X(" << a << ")" << endl; } ~X() { cout << "X destructor" << endl; } }; class X_delegating : X { public: X_delegating() : X(10) { throw runtime_error("Exception thrown in X::X()"); } };
This is not entirely true, there is only one type, but it is similar to the X(int) constructor, then additional code is run in the delegation constructor, and if it returns X "base class" (which is not really the base class) is destroyed .