tun*_*nuz 7 c++ tags compilation exception noexcept
我很难理解这一点.
double compute(double x, double y) noexcept
{
if (y == 0)
throw std::domain_error("y is zero");
return x / y;
}
Run Code Online (Sandbox Code Playgroud)
这在clang中编译得很好(我没有检查过gcc),但这对我来说似乎是胡说八道.为什么编译器允许noexcept函数包含throw语句?
And*_* DM 10
一个声称不会抛出的函数实际上可能会抛出。
如果noexcept
函数确实抛出,terminate
则调用,从而强制承诺在运行时不抛出。
// The compiler does not check the `noexcept` specification at compile time.
void f() noexcept // Promises to not throw any exception
{
throw runtime_error("error"); // Violates the exception specification
}
Run Code Online (Sandbox Code Playgroud)
指定函数不会抛出向非抛出函数的调用者承诺,他们将永远不需要处理异常。
要么函数不会抛出,要么整个程序将终止。
会发生什么是std::terminate()
触发,因为您的异常规范不允许这种情况发生(参见[except.spec/9]).
至于为什么允许这样做,根本无法彻底检查是否有违反规范的内容.考虑类似的事情:
double f(double );
double compute(double x, double y) noexcept
{
return x / f(y);
}
Run Code Online (Sandbox Code Playgroud)
可以f
扔?不能说.
归档时间: |
|
查看次数: |
2452 次 |
最近记录: |