当使用lambda作为模板参数时,这个编译器错误是什么? [英] What is this compiler error when using a lambda as a template parameter?

查看:278
本文介绍了当使用lambda作为模板参数时,这个编译器错误是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已被报告为Microsoft Connect上的VS2012 C ++编译器错误(link)

This has been reported as a VS2012 C++ compiler bug on Microsoft Connect (link).

11月。 2014年11月11日:Microsoft已回应说,此错误的修复程序应显示在Visual C ++的下一个主要版本中。

Nov. 11, 2014: Microsoft has responded saying the fix for this bug should show up in the next major release of Visual C++.

我一直在努力与一个VS2012编译器错误消息我不明白,所以我修剪掉了这个问题,似乎是最低限度。

I've been struggling with a VS2012 compiler error message I don't understand, so I trimmed down the problem to what seems like the bare minimum.

使用VS2012构建以下 main.cpp

I'm building the following main.cpp using VS2012:

#include <utility>

template <typename T>
struct A
{
    T x;
    A(A&& other) : x(std::move(other.x)) { }
    A(T&& x) : x(std::move(x)) { }
};

template <typename T>
A<T> build(T&& x)
{
    return A<T>(std::move(x));
}

int main(int argc, char* argv[])
{
    auto f = []()
    {
        return build([](){}); //error here
    };
    return 0;
}

重点是我试图使用lambda作为模板键入 build 函数的 T 。我得到的错误消息是:

The salient point is that I'm trying to use a lambda as the template type T of the build function. The error message I get is:

1>  main.cpp
1>C:\test\main.cpp(21): error C2664: 'A<T>::A(A<T> &&)' : cannot convert parameter 1 from 'A<T>' to 'A<T> &&'
1>          with
1>          [
1>              T=void (__cdecl *)(void)
1>          ]
1>          and
1>          [
1>              T=main::<lambda_c3c618d445b3cb24eede9bf304860ad7>::()::<lambda_4240e93016e3e420ff8383c9350ae130>
1>          ]
1>          and
1>          [
1>              T=void (__cdecl *)(void)
1>          ]
1>          Reason: cannot convert from 'A<T>' to 'A<T>'
1>          with
1>          [
1>              T=main::<lambda_c3c618d445b3cb24eede9bf304860ad7>::()::<lambda_4240e93016e3e420ff8383c9350ae130>
1>          ]
1>          and
1>          [
1>              T=void (__cdecl *)(void)
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

I我做了我的研究,并查找错误消息的页面(链接),但我仍然不能弄清楚这是什么问题。您能解释这个编译器错误吗?

I've done my research and looked up the page for the error message (link), but I still can't figure out what the problem is. Could you please explain this compiler error?

在这里肯定是奇怪的。如果我更改 main 中的代码如下:

Something is definitely weird here. If I change the code in main to look like this:

auto f = []()
{
    int* n = new int(0);
    auto g = [=](){ return *n; };
    *n++;
    return build<decltype(g)>(std::move(g));
};



我收到一条错误消息,表明 T = int(__cdecl *) void)在调用构建 - 这将意味着 decltype(g)给我一个函数指针?嗯?我通过值捕获指针,然后修改它 - 不应该 创建一个函子 - 没有转换函数指针?也许我不懂事。

I get an error message suggesting that T=int (__cdecl *)(void) in the call to build - which would mean that decltype(g) is giving me a function pointer? Huh? I'm capturing a pointer by value and then modifying it afterwards - shouldn't it have to create a functor - and one that has no cast to function pointer? Maybe I'm not understanding something.

查看相关: Lambda表达式:n3290草稿

此外,如果此

Also, if this is a bug in the VS2012 compiler, can you think of a workaround?

推荐答案

我可以确认使用GCC(在linux上),这个代码编译就好了。
所以我想说VisualStudio似乎是错误的根源。

I can confirm that using GCC (on linux), this code compiles just fine. So I'd say that VisualStudio seems to be the source of the error.

这篇关于当使用lambda作为模板参数时,这个编译器错误是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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