c++g++compiler-optionsexception-specification

Can g++ check the throw specifiers?


Two questions about this :


EDIT: I'll add some examples for my second question.

Suppose we have:

// 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's OK
    /* .. */
    throw C();    // C does not inherit A, so WARNING!
}

Solution

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