C++ 标准库实现是否允许加强 noexcept 规范?
Are C++ standard library implementations allowed to strengthen noexcept specifications?
根据 C++ 标准,是否允许 C++ 标准库的实现加强 noexcept
标准定义的 C++ 标准库的方法和其他函数规范?
例如,如果 C++ 标准将某些函数 std::f
指定为 void f();
,是否允许标准库实现将其实现为 void f() noexcept;
?
标准说是:
§ 17.6.5.12.1 Restrictions on exception handling [res.on.exception.handling]
- 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.
[...]
- 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 似乎只允许明确说明异常规范,并警告说缺少明确的异常规范意味着允许该实现抛出任何东西)。
老实说,我不明白为什么允许添加 constexpr
而不允许添加(§ 17.6.5.6)。它们看起来像同一枚勋章的两侧——通过使用类型特征和 SFINAE,您可以拥有根据您使用的标准库实现显示不同行为的代码(如果它将某些函数标记为 noexcept
/constexpr
,或者如果没有),这首先违背了制定标准的目的...
根据 C++ 标准,是否允许 C++ 标准库的实现加强 noexcept
标准定义的 C++ 标准库的方法和其他函数规范?
例如,如果 C++ 标准将某些函数 std::f
指定为 void f();
,是否允许标准库实现将其实现为 void f() noexcept;
?
标准说是:
§ 17.6.5.12.1 Restrictions on exception handling [res.on.exception.handling]
- 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.
[...]
- 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 似乎只允许明确说明异常规范,并警告说缺少明确的异常规范意味着允许该实现抛出任何东西)。
老实说,我不明白为什么允许添加 constexpr
而不允许添加(§ 17.6.5.6)。它们看起来像同一枚勋章的两侧——通过使用类型特征和 SFINAE,您可以拥有根据您使用的标准库实现显示不同行为的代码(如果它将某些函数标记为 noexcept
/constexpr
,或者如果没有),这首先违背了制定标准的目的...