可以 g++检查抛出说明符?
关于此的两个问题:
有没有办法强制
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!
}
Two questions about this :
Is there a way to force
g++
to ignore thethrow
specifiers ?
(for example, as I remember, Visual Studio ignores the throw specifiers, different fromthrow()
)Is it possible to force
g++
to check if the throw specifiers are correct - what I mean is to check ( and this can be done by a one-pass compilers ) if the functions with throw specifiers call functions, that may throw by just watching theirs throw specifiers and watch for executingthrow
for exceptions, that will violate the specifiers ? (Note: this should not watch the functions without throw specifiers, because this could cause tons of warnings )
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!
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
gcc 有一个选项
-fno-enforce-eh-specs
,请参阅文档并检查它是否符合您的要求。我不记得任何使用 gcc 静态检查异常规范的方法。
请注意,(动态)异常规范在 C++0X 中已被弃用,它添加了一个
noexcept
异常规范来替换空的异常规范情况(它也是动态检查的,并且具有帮助在模板中使用它的规定)。gcc has an option
-fno-enforce-eh-specs
, see the documentation and check that it does what you want.I don't remember any way to statically check exception specifications with gcc.
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).是的,您可以通过以下方式使 g++ 忽略抛出:
关于其余的内容,您需要更改编译器代码或在构建过程中制作自己的脚本/程序来检查这些事情,可以使用 regexp 轻松完成。
编辑:
关于您的评论,找到异常的层次结构非常容易。使用正则表达式,例如:
并将其输入到哈希中,然后生成分层数据。
要匹配抛出它们的函数中的异常,请使用:
它未经测试,可能有一些错误,但很好的起点
Yes, you can make g++ ignore throw by:
About the rest you need to change the compiler code or make your own script/program in build process that will check for those things, it can be easily done with regexp.
Edit:
about your comment, finding the hierarchy of exceptions is really easy. use regexp like:
and input that to a hash, and later make hierarchical data.
To match exceptions in functions that throw them use:
its not tested and might have some errors, but good place to start