Recently I tried to answer what I thought would be a simple question on the noexcept
exception specification. The end result being that I found that my fundamental understanding of noexcept
was wrong.
While reading the current draft standard to correct my misunderstanding, I found myself asking a few questions about noexcept
that weren't answered here.
- Should
noexcept
be considered a safety guarantee, that the function when called will not only not throw but will not corrupt state? - Assuming that (1.) is false: Is it correct to use
noexcept
as a portable FailFast to terminate the application without cleanup to prevent corruption of saved state?
Clarification to (2.): The intent is ONLY to prevent destructor calls further up the stack from the noexcept
not to prevent unwinding within it. This is based on the assumption that this is a perfect RAII environment and the destructors up the stack could flush out global state to persistence thus corrupting it.
Example of how unwinding is not preformed:
#include <iostream>
#include <exception>
namespace{
struct foo{
void change_state() noexcept
{
// change state and fail
throw std::exception();
}
~foo(){
std::cout << "Destructor called, saved state corrupted!" <<std::endl;
}
};
}
int main(){
::std::set_terminate([](){
std::cout<< "Terminate called" <<std::endl;
});
foo f;
f.change_state();
return 0;
}