非常简单的代码,并出现错误C2712,无法理解原因 [英] very simple code, and getting error C2712, could not understand why
问题描述
我在error C2712: Cannot use __try in functions that require object unwinding
上遇到了一段时间,在缩小问题范围之后,剩下的代码非常简单,我不明白为什么会导致此错误.我正在Windows下使用Visual Studio.
I'm having trouble for a while with error C2712: Cannot use __try in functions that require object unwinding
, after narrowing the problem, I was left with a very very simple code, and i can not understand why it causes this error. I am using Visual Studio under windows.
我正在使用/EHa进行编译(我不使用/EHsc)
I am compiling with /EHa (I do not use /EHsc)
我使用__try/__except
而不是try/catch
的原因是因为我想捕获所有错误,并且不希望程序在任何情况下都崩溃,例如除以0,try-catch不会赶上.
The reason I use __try/__except
and not try/catch
is because I want to catch ALL the errors, and do not want the program to crash under any circumstances, including for example division by 0, that try-catch does not catch.
#include <string>
static struct myStruct
{
static std::string foo() {return "abc";}
};
int main ()
{
myStruct::foo();
__try
{ }
__except (true)
{ }
return 0;
}
输出:
error C2712: Cannot use __try in functions that require object unwinding
推荐答案
以下是解决方案.有关更多详细信息,请阅读编译器错误C2712
Here is the solution. For more details read Compiler Error C2712
#include <string>
struct myStruct
{
static std::string foo() {return "abc";}
};
void koo()
{
__try
{ }
__except (true)
{ }
}
int main ()
{
myStruct::foo();
koo();
return 0;
}
附加说明:如果不使用您的结构(myStruct)进行声明,则无需static
.
Extra Note: no need static
if no declaration using your struct (myStruct).
这篇关于非常简单的代码,并出现错误C2712,无法理解原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!