【发布时间】:2017-12-06 19:48:55
【问题描述】:
C++ standard 说 ([except.handle]/9):
如果没有找到匹配的处理程序,则调用函数 std::terminate();在此调用 std::terminate() 之前是否展开堆栈是实现定义的
例如,下面代码的行为(是否打印S::~S())是实现定义的:
struct S {
S() { std::cout << "S::S()" << std::endl; }
~S() { std::cout << "S::~S()" << std::endl; }
};
int main() {
S s;
throw std::runtime_error("exception");
}
我想深入了解:为什么要定义实现?如果未捕获异常(类似于顶级函数中的try{ ... } catch(...) { throw; }),为什么在调用std::terminate() 之前不能将上下文展开到其条目?乍一看,这种行为与 RAII 一致,更清晰、更安全。
【问题讨论】: