Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does an exception specification affect virtual destructor overriding?
    text
    copied!<p>The C++ Standard states the following about virtual functions that have exception specifications:</p> <blockquote> <p>If a virtual function has an <em>exception-specification</em>, all declarations, including the definition, of any function that overrides that virtual function in any derived class shall only allow exceptions that are allowed by the <em>exception-specification</em> of the base class virtual function (C++03 §15.4/3).</p> </blockquote> <p>Thus, the following is ill-formed:</p> <pre><code>struct B { virtual void f() throw() { } // allows no exceptions }; struct D : B { virtual void f() { } // allows all exceptions }; </code></pre> <p>(1) Does this rule apply to destructors? That is, is the following well-formed? </p> <pre><code>struct B { virtual ~B() throw() { } }; struct D : B { virtual ~D() { } }; </code></pre> <p>(2) How does this rule apply to an implicitly declared destructor? That is, is the following well-formed?</p> <pre><code>struct B { virtual ~B() throw() { } }; struct D : B { // ~D() implicitly declared }; </code></pre> <p>While in the general case one should <a href="http://www.gotw.ca/publications/mill22.htm" rel="noreferrer">never write an exception specification</a>, this question has practical implications because the <code>std::exception</code> destructor is virtual and has an empty exception specification.</p> <p>Since it is good practice not to allow an exception to be thrown from a destructor, let's assume for the sake of simplifying any examples that a destructor either allows all exceptions (that is, it has no exception specification) or it allows no exceptions (that is, it has an empty exception specification).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload