【发布时间】:2019-08-02 17:36:31
【问题描述】:
为什么外部的temp 在捕获到第一个异常后变为空?
#include <iostream>
int main()
{
std::string temp("exception");
int value;
while(std::cin>> value && value != 0)
{
try{
if(value > 9) throw temp;
else std::cout << value << "\n";
}
catch(std::string temp)
{
std::cout << temp << "\n";
}
}
return 0;
}
输入:
1
2
11
13
输出:
1
2
exception
// Printing Empty string
预期输出:
1
2
exception
exception
我用 g++ 7.3.0 编译我的代码。
【问题讨论】:
-
似乎可以正常工作,并且与
clang 7一样。可使用gcc 8重现,将temp声明为const std::string temp("exception");似乎可以解决此问题。 -
我可以用 "gcc (GCC) 7.4.0" 重现它,cygwin 版本
-
似乎与刷新输出有关的问题.. 我会在 GCC 上尝试一下并为您发布答案
-
@lubgr 谢谢它的工作。
-
我也可以在 GCC 8.3,MSYS2 版本上重现。