明确将lambda转换为函数指针 [英] Explicitly convert a lambda to function pointer

查看:115
本文介绍了明确将lambda转换为函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用一个遗留模板函数,该函数具有大量专门化的函数指针,这些函数指针具有不同数量的参数.我正在编写自己的新模板函数,该函数需要某种任意类型的非捕获lambda,并且需要将其传递给其他库.如果直接执行此操作,则模板解析将失败.但是,如果我明确将其强制转换为相关的函数指针类型,则一切正常.

I have a legacy template function I'm trying to call that has a slew of specializations for function pointers with different numbers of arguments. I'm writing a new template function of my own that take some arbitrary kind of non-capturing lambda and needs to pass it to the other library. If I do this directly, then the template resolution fails. If however I explicitly cast it to be related function pointer type, things work.

然后的问题是如何使我的模板代码从lambda的类型中获取该函数指针的类型,或者在不显式引用该类型的情况下强制进行显式转换.

The problem then is how to make my template code get that function pointer type from the lambda's type or force the explicit conversion without explicitly referencing the type.

推荐答案

只需使用一元加号将lambda转换为函数指针,如下所示:

Just use unary-plus to convert a lambda to a function pointer, as in:

 auto* f = +[]{ std::cout << "Hello, world!\n"; }; // f is of type void (*)()

这篇关于明确将lambda转换为函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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