6

I have C++ code that used to compile (and work), now I get lots of warnings. This happened after I did a dist-upgrade to Ubuntu-Mate.

warning: dynamic exception specifications are deprecated in C++11

It happens on lines as simple as this (in a header):

    static Value getPriorityValue(const std::string& priorityName)
    throw(std::invalid_argument);

I got 2545 warning related to this! Is there anyway to tell the compiler to ignore this warning? What is the easiest way to make changes to the code.

Most of the errors are in a 3rd party package so I don't want to make too many modifications to this package.

I do have the -std=c++11 flag on in my compiler.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
user846566
  • 373
  • 1
  • 3
  • 12
  • 3
    Easiest way to fix it is to remove `throw(std::invalid_argument)` from the function declaration (since they are deprecated as the compiler is telling you). Also a warning is not the same thing as an error – UnholySheep May 09 '18 at 13:30
  • I think you should edit your question, since they aren't actually errors but just warnings. – Ben May 09 '18 at 13:30
  • You could compile with `#define throw(X) noexcept(false)`... (this is a joke, don't do it) – YSC May 09 '18 at 13:43
  • @YSC that would potentially break all the code throwing exceptions. – user7860670 May 09 '18 at 13:46
  • @VTT fixed..... – YSC May 09 '18 at 13:47
  • Just include 3rd party library headers as system ones. – Slava May 09 '18 at 13:54
  • 2
    Dynamic exception specifications are deprecated. That's a warning from the standards committee that they **might** be removed in a future version of the standard. In the meantime, they are legal and their behavior is well defined. Most compilers have mechanisms for turning off particular warnings, and that's what you should use. If you get errors from this code (i.e., the compiler refuses to compile it) then your compiler does not conform to the language definition. – Pete Becker May 09 '18 at 14:34

1 Answers1

13

You should remove or comment out these exception specifications wherever you can1, e.g.:

static Value getPriorityValue(const std::string& priorityName);
static Value getPriorityValue(const std::string& priorityName) /* throw(...) */;

You can use the -Wno-deprecated option to turn-off depreciation warnings for places where you cannot edit the code. I would recommend only using it when compiling third-party libraries. If you need to include a third-party header that raise such warning, you could do2:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
#include "thirdparty.h"
#pragma GCC diagnostic pop

This should work with both gcc and clang and will only disable -Wdeprecated for specific includes.

1 Dynamic exception specifications are deprecated since C++11, and are illegal since C++17, so you might want to get rid of them and upgrade third-party libraries you are using as soon as possible.

2 If you include these headers using a -I argument, you could switch to -isystem to disable all warnings for these headers, as mentioned by @Yakk - Adam Nevraumont. See also How to suppress GCC warnings from library headers?.

Holt
  • 36,600
  • 7
  • 92
  • 139
  • 4
    There is also a way to mark directories as system headers and not generate any warnings. `-isystem` instead of `-I` -- see https://stackoverflow.com/q/1867065/1774667 – Yakk - Adam Nevraumont May 09 '18 at 13:48
  • @Yakk-AdamNevraumont Suppressing all the warnings is never a good idea. – user7860670 May 09 '18 at 13:49
  • 5
    @VTT if you have no intention to modify 3rd party library it actually is. – Slava May 09 '18 at 13:52
  • 3
    Instead of removing them completely I would rather suggest to move them into comment section. Even though exception specifiers always were somewhat useless part of the language they may be still valuable part of code documentation. – user7860670 May 09 '18 at 13:53
  • 6
    @Slava *"Engineers are forgiven for making mistakes. They are not forgiven for hiding mistakes."* – user7860670 May 09 '18 at 14:35