Is gcc allowed to eliminate writes in a destructor?

Jonathan Wakely jwakely.gcc@gmail.com
Wed Jun 10 07:39:00 GMT 2015


On 10 June 2015 at 08:16, Norbert Lange wrote:
> But I`d be interested what should/could happen if derived destructors
> already did their work, and foo tries to use RTTI (dynamic_cast,
> typeid). I suspect this is undefined behaviour anyway given that these
> could throw,

That's not undefined behaviour, just ill-advised.

Since C++11 destructors are implicitly noexcept(true) by default, so
if a destructor exits via an exception the runtime will call
std::terminate(). That's perfectly well defined.

> and similarly dangerous as accessing virtual functions
> that might have overrides in the derived classes.

Nothing undefined there either, the dynamic type of the object is
CMyClass after the derived destructor runs, so as long as it isn't
pure virtual you can call the function, but it won't dispatch to the
derived override.



More information about the Gcc-help mailing list