37

I am a little bit confused with destructors and noexcept. My understanding was that in C++11 any destructor, including user-defined, is implicitly noexcept(true), even if we throw from it. And one has to specify explicitly noexcept(false) if they want it to be that way for some reason.

I'm seeing quite the opposite - with GCC 4.7.2, the user-defined destructor, no matter how primitive the class and destructor are, is implicitly noexcept(false). What am I missing here? Is there some hidden gotcha with user-defined destructors?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lapk
  • 3,838
  • 1
  • 23
  • 28
  • 8
    12.4/3: "A declaration of a destructor that does not have an exception-specification is implicitly considered to have the same exception-specification as an implicit declaration (15.4)." i.e. a destructor is only `noexcept(true)` if all the members and bases have a noexcept destructor. – ipc Mar 30 '13 at 18:12

1 Answers1

22

This is a known bug (credits to the OP for finding the bug report), and it seems it has been fixed in GCC 4.8.0. For instance, the static assertion below will fire on GCC 4.7.2, but not on GCC 4.8.0:

struct X
{
    ~X() { };
};

int main()
{
    X x;

    // This will not fire even in GCC 4.7.2 if the destructor is
    // explicitly marked as noexcept(true)
    static_assert(noexcept(x.~X()), "Ouch!");
}
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • 5
    I actually didn't think of using "bug" as an additional search keyword. Now that I did (thanks to you) this came up [Bug 56191](http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56191). Thanks for the prompt response! – lapk Mar 30 '13 at 18:04
  • @PetrBudnik: Thank you for the link, I will add it to the answer! – Andy Prowl Mar 30 '13 at 18:05