Lambda Function does not compile in Visual Studio 2010

。_饼干妹妹 提交于 2019-12-24 19:01:48

问题


I am more interested in knowing WHY it does not compile than fixing the code.

fatal error C1001: An internal error has occurred in the compiler.

int main()
{
    class MyClass
    {
    public:
        MyClass(const std::string & name)
            : name_(name) {}
        std::string name_;
    };

    auto creator = []() -> MyClass *
    {
        return new MyClass("Hello World");
    };

    MyClass * pMyClass = creator();

    return 0;
}

回答1:


fatal error C1001: An internal error has occurred in the compiler.

Anytime you see an internal compiler error, you're looking at a bug in the compiler itself. Basically, the compiler didn't give a compiler error; it crashed.

In these cases, there is no "why" as far as when they occur (not unless it was running out of resources). Or at least, not one that is easily determinable. Your best bet is to try to rearrange the code to make the compiler behave.

If you're not using VC2010 SP1, upgrade to the service pack and try again.




回答2:


As another data point, the 64-bit version of the VS 2010 compiler doesn't crash, but it gives this error instead:

test.cpp(16) : error C2061: syntax error : identifier 'MyClass'

If the class MyClass definition is moved outside of main(), both the x86 and x64 compilers will work.

Clearly, the x86 compiler crashing is a bug. I believe that using the local MyClass type should be fine in a lambda (and GCC 4.6.1 with -std=gnu++0x has no problem with it), so I think the error that the x64 compiler is throwing is also a bug.

I don't have access to VS 11 Dev Preview at the moment to test if the problem is still there.

VS 11 Dev Preview (cl.exe version 17.00.40825.2) has no problem with the local MyClass type being used in the lambda.



来源:https://stackoverflow.com/questions/9044061/lambda-function-does-not-compile-in-visual-studio-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!