Lambda 函数无法在 Visual Studio 2010 中编译

发布于 2024-12-29 14:26:44 字数 398 浏览 5 评论 0原文

我更感兴趣的是知道为什么它不能编译而不是修复代码。

致命错误 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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里°也失望 2025-01-05 14:26:44

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

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

在这些情况下,就其发生时间而言,没有“原因”(除非资源耗尽)。或者至少,不是一个容易确定的问题。最好的选择是尝试重新排列代码以使编译器正常运行。

如果您使用的不是 VC2010 SP1,请升级到该服务包并重试。

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.

北方。的韩爷 2025-01-05 14:26:44

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

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

如果 class MyClass 定义移出 main()< /code>,x86 和 x64 编译器都可以工作。

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

我目前无法访问 VS 11 Dev Preview 来测试问题是否仍然存在。

VS 11 Dev Preview(cl.exe 版本 17.00.40825.2)在本地 < lambda 中使用的 code>MyClass 类型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文