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

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

问题描述

我很好奇 noexcept 背后的理由/papers/2010/n3092.pdfrel =noreferrer> 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天全站免登陆