为什么C ++ 0x的`noexcept`被动态检查? [英] Why is C++0x's `noexcept` checked dynamically?

查看:101
本文介绍了为什么C ++ 0x的`noexcept`被动态检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇 noexcept 背后的理由/papers/2010/n3092.pdf\"> C++0x FCD throw(X)已被弃用,但 noexcept 似乎也做同样的事情。有没有理由 noexcept 在编译时没有检查?看起来,如果这些函数被静态检查,他们只在 try 块中调用抛出函数会更好。

I am curious about the rationale behind noexcept in the C++0x FCD. throw(X) was deprecated, but noexcept seems to do the same thing. Is there a reason that noexcept isn't checked at compile time? It seems that it would be better if these functions were checked statically that they only called throwing functions within a try block.

推荐答案

如果我记得throw已经被弃用,因为没有办法指定模板函数可以抛出的所有异常。即使对于非模板函数,您将需要throw子句,因为您已经添加了一些跟踪。

If I remember throw has been deprecated because there is no way to specify all the exceptions a template function can throw. Even for non-template functions you will need the throw clause because you have added some traces.

另一方面,编译器可以优化不会抛出异常的代码。请参阅关于noexcept的辩论,第一部分(以及 II III )进行详细讨论。主要点似乎是:

On the other hand the compiler can optimize code that doesn't throw exceptions. See "The Debate on noexcept, Part I" (along with parts II and III) for a detailed discussion. The main point seems to be:


过去二十年的丰富经验表明,在实践中,只有两种形式的异常规范是有用的:

The vast experience of the last two decades shows that in practice, only two forms of exceptions specifications are useful:


  • 缺少明确的异常规范,指定一个可以引发任何类型异常的函数:

  • The lack of an overt exception specification, which designates a function that can throw any type of exception:

int func(); //might throw any exception


  • 这样的函数可以由throw()规范指示:

  • A function that never throws. Such a function can be indicated by a throw() specification:

    int add(int, int) throw(); //should never throw any exception
    


  • 这篇关于为什么C ++ 0x的`noexcept`被动态检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆