std::terminate() 是否触发堆栈展开?

Does std::terminate() trigger stack unwinding?

我一直在尝试实现 Exception class,对于程序终止我决定使用 std::terminate(),但我不确定是否 std::terminate() 触发堆栈展开过程。

例如,如果我编译 运行 这段代码:

struct Test {
    Test() {
        std::cout << "Constructed\n";
    }
    ~Test() {
        std::cout << "Destructed\n";
    }
};

int main() {
    Test t;
    std::terminate();
    return 0;
}

它会输出这个:

Constructed
terminate called without an active exception

似乎没有调用析构函数。

std::terminate() 的标准处理程序直接调用 std::abort

如果你看一下here,你会发现std::abort()没有调用任何析构函数。

Destructors of variables with automatic, thread local (since C++11) and static storage durations are not called. Functions registered with std::atexit() and std::at_quick_exit (since C++11) are also not called. Whether open resources such as files are closed is implementation defined. An implementation defined status is returned to the host environment that indicates unsuccessful execution.