非常简单的代码,并出现错误C2712,无法理解原因

用户1438233

error C2712: Cannot use __try in functions that require object unwinding在解决问题后,我遇到了一阵子麻烦,剩下的代码非常简单,我不明白为什么会导致此错误。我在Windows下使用Visual Studio。

我正在使用/ EHa进行编译(我不使用/ EHsc)

我之所以__try/__except使用try/catch是因为我想捕获所有错误,并且不希望该程序在任何情况下都崩溃,例如除以0,try-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
Nayana adassuriya

这是解决方案。有关更多详细信息,请阅读编译器错误C2712

#include <string>
struct myStruct
{
    static std::string foo() {return "abc";}
};

void koo()
{
    __try 
    { }
    __except (true)
    { }
}

int main ()
{
    myStruct::foo();   
    koo();
    return 0;
}

额外注意static如果不需要使用结构(myStruct)进行声明,则不需要。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章