mas*_*tis 4 c++ exception noexcept c++11
鉴于以下类声明:
class phone_number
{
public:
explicit phone_number( std::string number ) noexcept( std::is_nothrow_move_constructible< std::string >::value );
}
phone_number::phone_number( std::string number ) noexcept( std::is_nothrow_move_constructible< std::string >::value )
: m_originalNumber{ std::move( number ) }
{
}
Run Code Online (Sandbox Code Playgroud)
std::terminate()如果从字符串构造函数抛出异常,下面的代码行是否会因为noexcept规范而立即调用?
const phone_number phone("(123) 456-7890");
Run Code Online (Sandbox Code Playgroud)
由于在调用函数之前评估所有参数,因此参数的构造函数发出的异常不会违反noexcept函数本身的约定.
为了证实这一点,这是我尝试过的,近似你的例子:
class A
{
public:
A(const char *)
{
throw std::exception();
}
};
void f(A a) noexcept
{
}
int main()
{
try
{
f("hello");
}
catch(std::exception&)
{
cerr<< "Fizz..." << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
毫不奇怪,输出是Fizz...,并且程序正常退出.
| 归档时间: |
|
| 查看次数: |
387 次 |
| 最近记录: |