当我调用 std::abort() 时 cout 未显示

cout not shown when I call std::abort()

当我 运行 这段代码在 Visual Studio 2015 on Windows 10 上看不到输出

That was unexpected

它适用于 Linux 上的 gcc 5.3。

class X {};
class Y {};
class Z : public X {};
class W {};

void f() throw(X, Y)    // list what exceptions can be thrown
{
    int n = 0;
    if (n) throw X(); // OK
    if (n) throw Z(); // also OK
    throw W(); // will call std::unexpected()
}

int main() {
    std::set_unexpected([] {
        std::cout << "That was unexpected" << std::endl;
        std::abort();
    });
    f();
}

Visual C++ 从未实现异常规范,尽管语法 被接受。

无论如何,它们在 C++11 及更高版本中已被弃用。

可能是由于主要 Windows C++ 编译器的不一致性。


同样,我记得 Visual C++ 从未实现过 std::uncaught_exception,但对此我不太确定。在使用和依赖之前值得检查。 … Doc-checkedthe documentation for Visual C++ 2015 指出

On devices, uncaught_exception is only supported on Windows CE 5.00 and higher versions, including Windows Mobile 2005 platforms

因此在桌面平台上显然支持它。