Search code examples
c++gcccompiler-errorswarningscompiler-flags

Make a specific gcc warning into an error


So I know I can turn warnings into errors using -Werror=... but I want to make the following warning into an error:

"Class xxx has virtual functions but non-virtual destructor"

The only way I know you can get this error is by turning on the overly obnoxious -Weffc++ flag. Is there a way (or what is the sub-flag in -Weffc++ for this warning) to just print this warning and then turn it into an error?

Thanks!


Solution

  • -Wnon-virtual-dtor is the name of the specific warning that is turned on by -Weffc++. To turn any warning into an error, you use -Werror=.... So if the warning were -Wspam, making it into an error would be -Werror=spam. So in this case, you would use -Werror=non-virtual-dtor.

    However, I don't feel that this warning is especially useful if you are on GCC 4.8 and up. Then you have access to the superior -Wdelete-non-virtual-dtor:

    Warn when delete is used to destroy an instance of a class that has virtual functions and non-virtual destructor. It is unsafe to delete an instance of a derived class through a pointer to a base class if the base class does not have a virtual destructor. This warning is enabled by -Wall.

    Note that g++ -Wspam -Werror=spam is the same thing as g++ -Werror=spam. Turning a warning into an error automatically turns that warning on.

    On a related note, you're not the only one who thinks that -Weffc++ is a little overzealous.