[Bug c++/67036] New: GCC does not warn of throwing destructors in C++11, even when they lack noexcept(false) and std::uncaught_exception

noloader at gmail dot com gcc-bugzilla@gcc.gnu.org
Mon Jul 27 22:14:00 GMT 2015


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67036

            Bug ID: 67036
           Summary: GCC does not warn of throwing destructors in C++11,
                    even when they lack noexcept(false) and
                    std::uncaught_exception
           Product: gcc
           Version: 5.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: noloader at gmail dot com
  Target Milestone: ---

Compiling under -Wall -Wextra does not produce a warning:

$ cat test-throw.cpp 
#include <stdexcept>
#include <iostream>

struct useless
{
    useless() { std::cout << "in" << std::endl; }
    ~useless() { std::cout << "out" << std::endl; throw std::runtime_error("who
knows..."); }
};

int main(int argc, char* argv[])
{
    useless u;
    return argc;
}

$ gcc -O1 -Wall -Wextra -c test-throw.cpp 
test-throw.cpp:10:26: warning: unused parameter 'argv' [-Wunused-parameter]
int main(int argc, char* argv[])
                         ^
1 warning generated.

*****

Same result with C++11:

$ gcc -std=c++11 -O1 -Wall -Wextra -c test-throw.cpp 
test-throw.cpp:10:26: warning: unused parameter 'argv' [-Wunused-parameter]
int main(int argc, char* argv[])
                         ^
1 warning generated.

Microsoft Visual Studio will warn.

*****

The warning is useful because it brings attention to a potential sore spot:
throwing during an unwind due to an exception.

Maybe the gap could be turned into a full fledged diagnostic: if the dtor
throws and (1) lacks noexcept(false) under C++11 or (2) and lacks a check for
std::uncaught_exception(), then the warning should be applied aggressively (say
at -Wall). Otherwise, have the throwing destructor generate a warning at -Wall.

Or maybe something else along similar lines...

I can state from experience the Visual Studio warning alerted us to look at a
particular dtor, and it was not checking for !std::uncaught_exception() before
throwing. (It was one of two classes among hundreds that threw, so it was easy
to miss throughout the day).

And as soon as Visual Studio caught it, two people on the team asked why GCC
did not catch it. (GCC is held in higher regard).



More information about the Gcc-bugs mailing list