Lambdas的模板类型推导 [英] Template Type Deduction with Lambdas

查看:85
本文介绍了Lambdas的模板类型推导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题很简单.给出以下代码:

The problem I'm facing is straightforward. Given the following code:

template <typename ReturnType, typename... Args>
auto CallIt( ReturnType( *method )( Args... ) )
{
    return method;
}

auto test = CallIt( [] ( int a, int b )
{
    return a > b;
} );

我得到的错误(将VS13与2013年11月的CTP编译器一起使用)是:

The error that I get (using VS13 with the November 2013 CTP compiler) is:

Could not deduce template argument for ReturnType (__cdecl *)(Args...) from main::<lambda_e795bf5a030334e5fc8f3c26dbe00e0e>

我知道lambda不是函数指针,但是可以将未捕获的lambda分配给匹配签名的函数指针.如果您明确指定模板参数,则可以使用.我希望看到一种无需显式指定模板参数即可工作的方法.预先感谢您的帮助.

I understand that a lambda is not a function pointer, but a lambda that does not capture can be assigned to a function pointer of a matching signature. If you explicitly specify the template arguments, this works. I would love to see a way where this can work without having to explicitly specify the template arguments. Thank you in advance for your help.

正如Marco A.提供的答案的注释中所述,在lambda类上使用一元+运算符可以有效地将其强制转换为函数指针,从而可以解决此问题.但是,在请求的IDE/编译器组合中,我收到以下警告转错误:

As noted in the comments for the answer provided by Marco A., there may be a solution to this using the unary + operator on the lambda class, effectively casting it to a function pointer. However, in the requested IDE/compiler combination I receive the following warning-turned-error:

来自"lambda [] bool(int a,int b)-> bool"的一个以上转换函数.内置类型适用:

more than one conversion function from "lambda []bool (int a, int b)->bool" to a built-in type applies:

function"lambda [] bool(int a,int b)-> bool :: operator bool(*)(int a,int b)()const"

function "lambda []bool (int a, int b)->bool::operator bool (*)(int a, int b)() const"

function"lambda [] bool(int a,int b)-> bool :: operator bool(*)(int a,int b)()const"

function "lambda []bool (int a, int b)->bool::operator bool (*)(int a, int b)() const"

function"lambda [] bool(int a,int b)-> bool :: operator bool(*)(int a,int b)()const"

function "lambda []bool (int a, int b)->bool::operator bool (*)(int a, int b)() const"

function"lambda [] bool(int a,int b)-> bool :: operator bool(*)(int a,int b)()const"

function "lambda []bool (int a, int b)->bool::operator bool (*)(int a, int b)() const"

此智能感知错误说明了指定以下情况生成的编译错误:

This intellisense error sheds light on the compile error generated specifying:

错误C2593:运算符+"不明确

error C2593: 'operator +' is ambiguous

推荐答案

1)添加适当的 2)如果您希望传递函数指针,请使lambda 与此相符

2) If you wish to pass a function pointer, have the lambda conform to that

template <typename ReturnType, typename... Args>
auto CallIt( ReturnType( *method )( Args... ) ) -> ReturnType(*)(Args...)
{
    return method;
}

auto test = CallIt( +[] ( int a, int b )
{
    return a > b;
} );

> 实时示例

MSVC2013似乎有问题.作为解决方法,您可以暂时尝试以下方法:

it seems there's something wrong with MSVC2013. As a workaround you might try if the following works for the time being:

#include <iostream>
#include <functional>
using namespace std;

template <typename ReturnType, typename... Args>
auto CallIt( std::function<ReturnType( Args... )> method ) -> std::function<ReturnType( Args... )>
{
    return method;
}

int main() {

    std::function<bool(int,int)> lambda = [] ( int a, int b ) { return a > b; };

    auto test = CallIt( lambda );
    cout << test(4,1);


    return 0;
}

在线示例

这篇关于Lambdas的模板类型推导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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