关于这个的两个问题:
有没有办法强制
g++
忽略说明throw
符?
(例如,我记得,Visual Studio 忽略了 throw 说明符,不同于throw()
)是否可以强制
g++
检查 throw 说明符是否正确 - 我的意思是检查(这可以通过一次性编译器完成)是否具有 throw 说明符的函数调用函数,这可能只是通过观看他们的 throw说明符并注意执行throw
异常,这会违反说明符吗?(注意:这不应该在没有 throw 说明符的情况下观看函数,因为这可能会导致大量警告)
编辑:我将为我的第二个问题添加一些示例。
假设我们有:
// 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!
}