【问题标题】:cout not shown when I call std::abort()当我调用 std::abort() 时未显示 cout
【发布时间】:2016-03-29 17:02:51
【问题描述】:

当我在 Windows 10 上的 Visual Studio 2015 中运行此代码时,看不到输出

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();
}

【问题讨论】:

  • 您可以尝试写信给std::cerr
  • 我刚才试了std::cerr还是不行。

标签: c++ exception visual-studio-2015 iostream abort


【解决方案1】:

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

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

可能是由于主 Windows C++ 编译器不符合要求。


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

在设备上,uncaught_exception 仅在 Windows CE 5.00 及更高版本(包括 Windows Mobile 2005 平台)上受支持

所以在桌面平台上它显然是受支持的。

【讨论】:

  • 另请注意,在 Windows Clang 上也是如此
  • 好的,也许 MS 文档是错误的,因为它似乎不适用于 Windows 10 桌面平台。感谢您找到这个!
  • @Damian:你的意思是std::uncaught_exception
  • 是的,std::uncaught_exception 似乎没有被调用。
猜你喜欢
  • 2020-09-24
  • 2023-02-06
  • 1970-01-01
  • 2021-06-11
  • 2014-12-31
  • 2017-10-05
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多