构造函数抛出异常时运行哪些析构函数?

在 C + + 中,如果构造函数抛出异常,那么会运行哪些析构函数?

特别是,异常是在初始化列表期间还是在主体期间,这有什么区别吗?

还有,继承和成员呢?大概所有完工的建筑都会被毁掉。如果只构造了一些成员,是否只有这些成员被破坏?如果存在多重继承,是否所有已完成的构造函数都会被销毁?虚继承改变了什么吗?

20866 次浏览

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:

In biological terms, conception took place -- the constructor began --, but despite best efforts it was followed by a miscarriage -- the constructor never ran to term(ination).

Incidentally, this is why a destructor will never be called if the constructor didn't succeed -- there's nothing to destroy. "It cannot die, for it never lived." Note that this makes the phrase "an object whose constructor threw an exception" really an oxymoron. Such a thing is even less than an ex-object... it never lived, never was, never breathed its first.

Any object created in a local scope left because of the constructor will be destructed. The runtime handling walks back up the stack, calling destructors, until it finds a handler.

If the exception is thrown from a constructor, the destructors of all completely constructed subobjects will be called. In addition, if the constructor was part of a new expression, the appropriate placement delete operator will be called if it exists.

In C++, if a constructor throws an exception, what destructors are run?

All objects that have had constructors run to completion.

In particular, does it make any difference if the exception is during the initialization list or the body?

No. All members that are fully constructed prior to the exception will have their destructors run. The member that threw during construction and all other non constructed members will not have their destructors run. The order of member construction is well defined and thus you know exactly what will happen given you know the point of the exception throw.

Also, what about inheritance and members?

Same rule applies.

Presumably all completed constructions get destructed.

Yes

If only some members are constructed, do only those get destructed?

Yes

If there is multiple inheritance, do all completed constructors get destructed?

Yes

Does virtual inheritance change anything?

No.
But note: Virtual inheritance does affect the order the constructors are called. If you are not familiar with how the order is defined this may be non intuitive until you look up the exact rules.