c-function-try-block和noexcept
作者:互联网
对于以下代码
struct X
{
int x;
X() noexcept try : x(0)
{
}
catch(...)
{
}
};
Visual Studio 14 CTP发出警告
warning C4297: ‘X::X’: function assumed not to throw an exception but
doesnote: __declspec(nothrow), throw(), noexcept(true), or noexcept was
specified on the function
这是对noexcept的滥用吗?还是Microsoft编译器中的错误?
解决方法:
Or is it a bug in Microsoft compiler?
不完全的.
像这样的所谓的function-try-block不能阻止异常进入外部.考虑到对象永远不会完全构造,因为构造函数无法完成执行. catch块必须抛出其他东西,否则当前异常将被重新抛出([except.handle] / 15):
The currently handled exception is rethrown if control reaches the end
of a handler of the function-try-block of a constructor or
destructor.
因此,编译器推断出构造函数确实可以抛出异常.
struct X
{
int x;
X() noexcept : x(0)
{
try
{
// Code that may actually throw
}
catch(...)
{
}
}
};
应该在没有警告的情况下进行编译.
标签:noexcept,c,c11,c14,visual-c 来源: https://codeday.me/bug/20191010/1887695.html