Can g ++ check throw specifiers?

Two questions:

  • Is there a way to get g++ ignore throw specifiers?
    (for example, as I recall, Visual Studio ignores throw specifiers other than throw() )

  • Is it possible to force g++ to check the correctness of the throw specifiers - I mean the check (and this can be done using single-pass compilers ) if functions with throw specifier functions call functions that can be thrown just by observing their throw specifiers and observing the throw execution for exceptions that qualifiers will violate? (Note: this should not look for functions without throw specifiers, because it can cause a ton of warnings)


EDIT:. I will add some examples for my second question.

Let's pretend that:

 // sorry for the coding style here, but I don't want it to be unnecessary long class A { /* .. */ }; class B : public A { /* .. */ }; class C { /* .. */ }; void no_throw_spec() { /* .. */ } void no_throw_at_all() throw() { /* .. */ } void throws_A() throw( A ) { /* .. */ } // this is fine, don't do anything void f() { no_throw_spec(); no_throw_at_all(); throws_A(); } void g() throw() { no_throw_spec(); no_throw_at_all(); // OK throws_A(); // warning here - throws_A() may throw A, but g() has throw()! } void h() throw( A ) { no_throw_spec(); no_throw_at_all(); throws_A(); // OK if( /* .. */ ) throw B(); // OK, B inherits A, it OK /* .. */ throw C(); // C does not inherit A, so WARNING! } 
+4
source share
2 answers
  • gcc has the -fno-enforce-eh-specs option, see the documentation and make sure that it does what you want.

  • I don't remember how to statically check exception specifications with gcc.

Note that (dynamic) exception specifications are deprecated in C ++ 0X, which add the noexcept exception noexcept , replacing the empty case of the exception specification (it is also checked dynamically and contains provisions to help use it in templates).

+3
source

Yes, you can do g ++ ignore throw by:

 #define throw(x) 

For the rest, you need to change the compiler code or make your own script / program during the build process, which will check these things, it can be easily done with a regular expression.

Edit:

about your comment, finding an exception hierarchy is easy. use regexp like:

 class ([^ ]*) : ([^ ]*) 

and enter it in the hash, and then execute the hierarchical data.
To match exceptions in functions that use them, use:

 ([^\(\s]*)[\s]*([^\)])[\s]*(throw[\s]*\([^\)]*\)){((throw[\s]*[^;])|*)*} 

it was not tested and might have some errors, but a good place to run

+2
source

All Articles