我有一个程序:
#include<iostream>
using namespace std;
class Test
{
public:
void func()
{
cout << "Inside func" << endl;
throw;
}
};
int myfunc()
{
Test T;
T.func();
return 1;
}
int main()
{
myfunc();
cout << "Main func" << endl;//should not print
getchar();
}
我的预期是该程序将从 main
终止, 但在 VC++ 2015 上 main
cout
正在打印。这违反了我的理解,所以我用 gcc
编译了它在那里工作正常。
这是 VC++ 2015 中的错误还是像这样的程序终止行为是未指定/UB 行为?它是否应该执行 cout << "Main func" << endl;
?
IDE:VS2015 CTP Ultimate Preview(30天)
标志:/GS /analyze- /W3 /Zc:wchar_t /ZI /Gm /Od /sdl /Fd"Debug\vc140.pdb" /fp:precise /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\exception.pch"
最佳答案
throw
没有参数,当在不正确的上下文中调用时,应该调用 terminate
。
根据标准:
A throw-expression with no operand rethrows the currently handled exception
....
If no exception is presently being handled, executing a throw-expression with no operand calls
std::terminate()
然后行为取决于当前安装的 std::terminate_handler
,但无论如何执行都应该终止。
Required behavior: A
terminate_handler
shall terminate execution of the program without returning to the caller.Default behavior: The implementation’s default
terminate_handler
callsabort
. The default implementation callsstd::abort
.
关于c++ - 异常 - VC++2015 CTP Ultimate 中的不正确行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31634829/