Given the following class declaration:
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 ) }
{
}
Will the following line of code end up calling std::terminate()
immediately due to the noexcept specification if an exception is thrown from the string constructor?
const phone_number phone("(123) 456-7890");
Since all the parameters are evaluated before the function is invoked, an exception, emitted by a parameter's constructor, would not violate noexcept
contract of the function itself.
To confirm this, here's what I've tried, approximating your example:
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;
}
The output, unsurprisingly, was Fizz...
and the program exited normally.