问题描述
我有以下模板化函数(编译器中启用了 C++ 最新标准 - 但也许 vpn下载 17 个就足够了).
#include 模板void MyFunction(const std::function& free vpn callback);int main(){MyFunction(std::function([](int){}));MyFunction([](int){});} 第一个调用编译,当我将它显式转换为 std::function 时,但第二种情况没有.
在第一种情况下,模板推导是自动完成的,编译器只知道它将其转换为某个 std::function 并能够推导参数和返回类型.
然而,在第二种情况下,它应该(?)也知道 lambda 应该被转换为一些 std::function,但仍然无法做到.
有没有办法让第二个运行?或者是因为模板根本不进行自动转换?
错误信息是:
<块引用>错误 C2672:'MyFunction':找不到匹配的重载函数
错误 C2784:'void MyFunction(const std::function<_Ret(_Types...)> &)':无法推导出 'const std::function<_Ret(_Types...) 免费vpn 的模板参数>
注意:见'MyFunction'的声明
我的目标是python vpn free 风格的装饰器".所以基本上是这样的:
templateauto MyFunction(std::function&& callback) ->std::function{return [callback = std::move(callback)](TArgs...args)->TReturn{返回回调(std::forward(args)...);};} 如果我使用模板而不是 std::function,我将如何推导出参数包和返回值?有没有办法通过一些可调用特征"从可调用对象中获取它?
或者是因为模板根本不进行自动转换?
是的.模板参数推导不会考虑隐式转换.><块引用>
类型推导不考虑隐式转换(上面列出的类型调整除外):这是重载解析的工作,稍后发生.
这意味着给定 MyFunction([](int){});,隐式转换(从 free vpn lambda 到 std::function)不会被考虑,然后对 TReturn 和 TArgs 的推导失败,调用尝试也失败.
作为解决方法,您可以
- 使用显示的显式转换
正如 免费vpn下载 评论所建议的那样,只需使用函子的单个模板参数.例如
templateauto MyFunction2(F&&回调){return [callback = std::move(callback)](auto&&... args){return callback(std::forward (args)...);};}
I have the following templated 免费vpn vpn下载 function (C++ latest standard free vpn is enabled in the compiler - but maybe 17 would be enough).
#include <functional>
template<typename TReturn, typename ...TArgs>
void MyFunction(const std::function<TReturn(TArgs...)>& callback);
int main()
{
vpn free MyFunction(std::function([](int){}));
MyFunction([](int){});
}
The first call compiles, when I explicitly vpn下载 convert it to std::function, but the second case does not.
In the first case the template deduction is done automatically, the compiler only knows that it shall convert it to some std::function and able to deduce the parameter 免费vpn and return type.
However in the second case it shall(?) also know that the lambda shall be converted to some std::function, but still unable to do it.
Is there a solution to get the second one running? Or can 免费vpn下载 it be that for templates the automatic conversion does not take place at all?
The error message is:
error C2672: vpn下载 'MyFunction': no matching overloaded function found
error C2784: 'void MyFunction(const std::function<_Ret(_Types...)> &)': could not deduce template argument 免费vpn for 'const std::function<_Ret(_Types...)>
note: see declaration of 'MyFunction'
What I am aiming for is a "python style decorator". So basically this:
template<typename TReturn, typename ...TArgs>
auto MyFunction(std::function<TReturn(TArgs...)>&& callback) -> std::function<TReturn(TArgs...)>
{
free 免费vpn vpn return [callback = 免费vpn std::move(callback)](TArgs... args)->TReturn
{
vpn free vpn下载 return callback(std::forward<TArgs>(args)...);
免费vpn下载 };
}
If I used a template instead of 免费vpn下载 std::function, the how would I deduce the parameter pack and return value? Is there some 免费vpn way to get it from a callable via some "callable traits"?
Or 免费vpn下载 can it be vpn vpn下载 free that for templates the vpn下载 automatic conversion does not take place at all?
Yes. Implicit conversions won't be considered in template argument deduction.
Type deduction does not consider implicit conversions (other than type adjustments listed above): that's the job for overload resolution, which happens vpn下载 later.
That means given MyFunction([](int){});, the implicit conversion (from lambda 免费vpn to std::function) won't be considered, then the deduction for TReturn and TArgs fails free vpn free vpn and the invocation attempt fails too.
As the workarounds, you can
- Use explicit conversion as you showed
As the comment suggested, just use a single template parameter 免费vpn下载 for functors. e.g.
template<typename F> auto MyFunction2(F&& callback) { return [callback = std::move(callback)](auto&&... args) { return callback(std::forward<decltype(args)>(args)...); }; }
这篇关于从 lambda 构造 std::function 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账免费vpn织梦模板(带手机端)
成人高考自考在职研究生教育机构免费vpn源码(带手机端)
高端HTML5响应式企业集团通用类免费vpn织梦模板(自适应手机端)