gcc exception specification of default destructor

孤者浪人 提交于 2019-12-04 04:38:57

I presume that in the real code either ~A() or ~B() is declared virtual? (The error message is complaining about a virtual destructor, but in the code as written none of the destructors are virtual.)

I believe the virtual inheritance is what is triggering your problem. C's (implicitly defined) destructor is required to first call ~B() and then, because C is the most-derived class, to call ~A(). (12.4/6)

The generated exception specification for ~C() is required to allow any exception to propagate, because it directly calls ~A() which has no exception specification. (15.4/13)

And that then triggers your error - you can't override a virtual function with a throw() specification (B's destructor) with a version that may throw. (15.4/3)

The solution would be to put throw() on A's destructor. (If you can't do that, then why are you doing it on B's?)

The error also wouldn't happen without the virtual inheritance - because then C's destructor would only call B's destructor. (B's destructor would still call A's - and you're still skating on thin ice, because if A's destructor throws you're going straight to terminate().)

GCC 4.X is stricter than previous versions, and therefore may not state it implicitly. Try stating it explicitly.

The way I understand it, if class B had a destructor which explicitly threw nothing (ie.

class B: public A
{
public:
virtual ~B() throw { }
}

It should be ok.

Regardless, last time I checked it was very poor practice to throw exceptions from d'tors.

Hope that helps ya some!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!