C ++ 17异常说明符类型系统将如何工作? [英] How will C++17 exception specifier type system work?

查看:118
本文介绍了C ++ 17异常说明符类型系统将如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

研究"noexcept说明符(和运算符)",我编写了一个简单的代码.我很惊讶这段代码:

Studying about "noexcept specifier(and operator)", I wrote a simple code. And I am surprised that this piece of code:

void asdf() noexcept {}
int main() 
{
    auto f = asdf;
    std::cout << std::boolalpha << noexcept(f()) << std::endl;
}

打印false,即使函数"asdf"也没有指定例外.

prints false, even function "asdf" is noexcept-specified.

因此,在搜寻为什么发生这种神秘现象的过程中,我发现了C ++ 17的异常说明符类型系统"-

So while searching why this mysterious phenomenon is happening, I found C++17's "exception specifier type system"- P0012R1.

根据该(接受的)建议,自C ++ 17起;由于noexcept是函数类型的一部分,因此上面的代码会打印true吗?

According to this (accepted) proposal, since C++17; as noexcept is part of function type, will the code above print true?

还有一个问题,在问题的一行中:

And one more, in this question's one line:

std::function<void() noexcept> f

在C ++ 14或11中似乎忽略了noexcept的指定. 这段代码能按C ++ 17的预期运行吗?

The noexcept specifying seems ignored in C++14 or 11. Will this code work as intended in C++17?

推荐答案

根据该(接受的)建议,自C ++ 17起;因为noexcept是函数类型的一部分,所以上面的代码会打印true吗?

是的.

f的类型将推导为void(*)() noexcept,因为应用于asdf的函数到指针的转换将保留noexcept属性.除非对它的子表达式之一进行调用,否则对noexcept函数指针的调用肯定不会引发异常.

The type of f will be deduced to void(*)() noexcept since the function-to-pointer conversion applied to asdf will preserve the noexcept property. A call to a noexcept function pointer certainly cannot throw an exception, unless one of its subexpressions does.

有关确切的措辞,请参见 [expr.unary.noexcept]/3 [expect.spec]/13 .请注意,C ++ 17草案后面的段落中的新措词来自于OP中链接的P0012R1.

For the exact wording, see [expr.unary.noexcept]/3 and [expect.spec]/13. Note that the new wording in the latter paragraph from the C++17 draft comes from P0012R1, which is linked in the OP.

如果表达式的潜在异常集([except.spec])为空,则noexcept运算符的结果为true,否则为false.

The result of the noexcept operator is true if the set of potential exceptions of the expression ([except.spec]) is empty, and false otherwise.

...

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