在C ++ 11中弃用的throw-list [英] Deprecated throw-list in C++11

查看:237
本文介绍了在C ++ 11中弃用的throw-list的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在 cppreference 中看到的,经典的throw声明列表现已弃用C ++ 11。离开这个机制的原因是什么,以及如何指定什么异常抛出我的函数?

解决方案

详细推理,请参阅: http://www.open-std.org /jtc1/sc22/wg21/docs/papers/2010/n3051.html


如上述国家机构评论中所述,异常规范在实践中没有被证明是有用的。有很多关于C ++中的异常规范问题的讨论(参见例如[Sutter02],[Boost03]),但是主要的问题是:




  • 运行时检查:C ++异常规范在运行时而不是在编译时检查,因此它们不提供程序员保证已处理所有异常。

  • 运行时开销:运行时检查需要编译器生成额外的代码,

  • 在通用代码中不可用:在通用代码中,通常不可能知道从模板参数的操作抛出什么类型的异常,因此精确的异常规范不能


  • 在实践中,只有两种形式的异常抛出保证是有用的:操作可能抛出异常(任何异常)或者一个操作不会抛出任何异常。前者通过完全省略异常规范来表示,而后者可以表示为throw(),但很少出于性能考虑。



    [N3050]介绍一种新的异常规范,noexcept,指定该函数不会抛出任何异常。与throw()不同,noexcept不需要编译器引入代码来检查是否抛出异常。相反,如果指定为noexcept的函数通过异常退出,则结果是对std :: terminate()的调用。



    随着noexcept的引入,程序员可以现在表达两种在实践中有用的异常保证,而没有额外的开销。因此,本文提议废弃动态异常规范,即写为throw(type-id-listopt)。



Just as I can see in cppreference, the classic "throw" declaration lists is now deprecated in C++11. What is the reason of leaving this mechanism and how should I have to specify what exceptions throws a function of mine?

解决方案

For more detailed reasoning, see: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3051.html

As expressed in the national body comment above, exception specifications have not proven useful in practice. There are numerous discussions of the problems with exception specifications in C++ (see, e.g., [Sutter02], [Boost03]), but the main issues are:

  • Run-time checking: C++ exception specifications are checked at runtime rather than at compile time, so they offer no programmer guarantees that all exceptions have been handled. The run-time failure mode (calling std::unexpected()) does not lend itself to recovery.
  • Run-time overhead: Run-time checking requires the compiler to produce additional code that also hampers optimizations.
  • Unusable in generic code: Within generic code, it is not generally possible to know what types of exceptions may be thrown from operations on template arguments, so a precise exception specification cannot be written.

In practice, only two forms of exception-throwing guarantees are useful: an operation might throw an exception (any exception) or an operation will never throw any exception. The former is expressed by omitting the exception-specification entirely, while the latter can be expressed as throw() but rarely is, due to performance considerations.

[N3050] introduces a new kind of exception specification, noexcept, the specifies that the function will not throw any exceptions. Unlike throw(), noexcept does not require the compiler to introduce code to check whether an exception is thrown. Rather, if a function specified as noexcept is exited via an exception, the result is a call to std::terminate().

With the introduction of noexcept, programmers can now express the two kinds of exception guarantees that are useful in practice, without additional overhead. This paper therefore proposes to deprecate "dynamic" exception specifications, i.e., those that are written as throw(type-id-listopt).

这篇关于在C ++ 11中弃用的throw-list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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