Lambda函数未在Visual Studio 2010中编译

Lambda Function does not compile in Visual Studio 2010

本文关键字:2010 编译 Studio Visual 函数 Lambda      更新时间:2023-10-16

比起修复代码,我更感兴趣的是了解它为什么不编译。

致命错误C1001:编译器中发生内部错误。

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;
}

致命错误C1001:编译器中发生内部错误。

每当您看到内部编译器错误时,您看到的都是编译器本身的错误。基本上,编译器没有给出编译器错误;它崩溃

在这些情况下,就发生的时间而言,没有"为什么"(除非资源耗尽)。或者至少,不是一个容易确定的。最好的办法是尝试重新排列代码以使编译器正常工作。

如果您没有使用VC2010 SP1,请升级到service pack,然后重试。

作为另一个数据点,VS 2010编译器的64位版本没有崩溃,但它给出了以下错误:

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

如果将class MyClass定义移到main()之外,则x86和x64编译器都可以工作。

显然,x86编译器崩溃是一个错误。我认为在lambda中使用本地MyClass类型应该很好(带有-std=gnu++0x的GCC 4.6.1对此没有问题),所以我认为x64编译器抛出的错误也是一个错误。

我目前无法访问VS 11开发人员预览版来测试问题是否仍然存在

VS 11开发人员预览版(cl.exe版本17.00.40825.2)对lambda中使用的本地MyClass类型没有问题。