In C++, if a constructor throws an exception, what destructors are run?
In particular, does it make any difference if the exception is during the initialization list or the body?
Also, what about inheritance and members? Presumably all completed constructions get destructed. If only some members are constructed, do only those get destructed? If there is multiple inheritance, do all completed constructors get destructed? Does virtual inheritance change anything?
if a constructor throws an exception, what destructors are run?
Destructors of all the objects completely created in that scope.
Does it make any difference if the exception is during the initialization list or the body?
All completed objects will be destructed.
If constructor was never completely called object was never constructed and hence cannot be destructed.
what about inheritance and members? Presumably all completed constructions get destructed. If only some members are constructed, do only those get destructed? If there is multiple inheritance, do all completed constructors get destructed? Does virtual inheritance change anything?
All completed constructions do get destructed. Yes, only the completely created objects get destructed.
Good Read:
Constructor Failures by Herb Sutter
Especially, love the part where he explains: