是否允许C ++标准库实现增强noexcept规范? [英] Are C++ standard library implementations allowed to strengthen noexcept specifications?

查看:98
本文介绍了是否允许C ++标准库实现增强noexcept规范?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据C ++标准,C ++标准库的实现是否可以增强标准定义的C ++标准库的方法和其他功能的noexcept规范?

According to the C++ standard, are implementations of the C++ standard library allowed to strengthen noexcept specifications of methods and other functions of the C++ standard library as defined by the standard?

例如,如果C ++标准将某些功能std::f指定为void f();,是否允许标准库实现将其作为void f() noexcept;实现?

For example, if the C++ standard specifies some function std::f as void f(); are standard library implementations allowed to implement it as void f() noexcept; instead?

推荐答案

标准回答是:

§17.6.5.12.1对异常处理的限制 [res.on.异常处理]

§ 17.6.5.12.1 Restrictions on exception handling [res.on.exception.handling]

  1. C ++标准库中定义的任何函数都可以通过引发其 Throws:段落中描述的类型的异常来报告失败.一个实现可以通过添加非抛出的 noexcept-specification来增强非虚函数的异常规范.
  1. Any of the functions defined in the C++ standard library can report a failure by throwing an exception of a type described in its Throws: paragraph. An implementation may strengthen the exception specification for a non-virtual function by adding a non-throwing noexcept-specification.

[...]

    在C ++标准库中定义的
  1. 析构函数操作不应引发异常. C ++标准库中的每个析构函数都应表现为具有非抛出异常规范.任何其他 除非另有说明,否则C ++标准库中定义的不具有 exception-specification 的函数可能会抛出实现定义的异常.实施可能会加强这一点 通过添加显式的隐式 exception-specification .
  1. Destructor operations defined in the C++ standard library shall not throw exceptions. Every destructor in the C++ standard library shall behave as if it had a non-throwing exception specification. Any other functions defined in the C++ standard library that do not have an exception-specification may throw implementation-defined exceptions unless otherwise specified. An implementation may strengthen this implicit exception-specification by adding an explicit one.

(逗号4似乎只允许对异常规范进行明确说明,并警告说缺少明确的异常规范意味着允许实现抛出任何异常).

(Comma 4 seems to just allow to be explicit about the exception specification, and to warn that the lack of an explicit exception specification means that the implementation is allowed to throw anything).

说实话,我不明白为什么允许这样做,而不允许添加constexpr(第17.6.5.6节).它们看起来像是同一枚勋章的两面-通过使用trait traits和SFINAE,您可以拥有根据所使用的标准库实现显示不同行为的代码(如果它将某些功能标记为noexcept/constexpr,或者如果不是这样的话,那首先就违背了建立标准的目的...

To be honest, I don't understand why this is allowed and adding constexpr is not (§ 17.6.5.6). They look like the two sides of the same medal -- by using type traits and SFINAE you can have code which shows different behaviours depending on which Standard Library implementation you use (if it marks some functions as noexcept/constexpr, or if it doesn't), and that defeats the purposes of having a standard in the first place...

这篇关于是否允许C ++标准库实现增强noexcept规范?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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