非常简单的代码,并出现错误C2712,无法理解原因 [英] very simple code, and getting error C2712, could not understand why

查看:372
本文介绍了非常简单的代码,并出现错误C2712,无法理解原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆