Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++1z dynamic exception specification error

I am trying to compile my project with new GCC version 7.2.1 and have a problem with dynamic exception specifications:

error: ISO C++1z does not allow dynamic exception specifications
  MEMORY_ALLOC_OPERATORS(SQLException)

The problem is that these errors come from third-party libraries which I do not control.

Is there a some way to fix it? As far as I know I can't tell compiler to replace errors with warnings. Using --std=c++14 is not an option because I want to use new features from C++1z.

like image 870
Pustovalov Dmitry Avatar asked Nov 14 '17 11:11

Pustovalov Dmitry


People also ask

What is dynamic exception specification?

A dynamic exception specification whose set of adjusted types is empty (after any packs are expanded) (since C++11) is non-throwing. A function with a non-throwing dynamic exception specification does not allow any exceptions. A dynamic exception specification is not considered part of a function's type.

What is an exception specification when is it used?

Exception specifications are a C++ language feature that indicate the programmer's intent about the exception types that can be propagated by a function. You can specify that a function may or may not exit by an exception by using an exception specification.

What is an exception specification Mcq?

Explanation: C++ provides a mechanism to ensure that a given function is limited to throwing only a specified list of exceptions. It is called an exception specification. ADVERTISEMENT.


2 Answers

C++17 removed dynamic exception specifications, as a result of P0003. Before that, they had been deprecated since C++11. They are no longer part of the language, so there isn't really a way to fix it. As long as you need that third party library, until it changes, you're stuck on C++14.


If you're desperate, you could try:

#define throw(...)

but I wouldn't recommend it.

like image 194
Barry Avatar answered Sep 27 '22 20:09

Barry


Well i wrote a little workaround.

#if __cplusplus >= 201703L
    /* MySQL override. This needed to be inclided before cppconn/exception.h to define them */
    #include <stdexcept>
    #include <string>
    #include <memory>

    /* Now remove the trow */
    #define throw(...)
    #include <cppconn/exception.h>
    #undef throw /* reset */
#endif

Short explanation: If we're using c++17, throw is not allowed anymore on allocators. If you take a closer look at the header of the library you'll see that there is a macro defined, which contains the definitions for the default allocator within the library. Sadly it can't be overridden because it gets defined there ignoring that's may already be defined. So somehow you have to override the trow anyway.

A basic trick is to override the trow function with a macro. Doing that leads us to the problem that we also override the trow operator for all includes within the library which isn't a good solution (and also doesn't work). As you may know, if you're includeing a header, it will be just included once (mostly, thanks to the header guards). So the trick there is to include the headers, which are included by the library, than override the throw include the header of the target library, which doesn't actually include their header again because we already did.

like image 31
WolverinDEV Avatar answered Sep 27 '22 21:09

WolverinDEV