不能通过boost :: phoenix :: function调用带有参数的惰性lambda函数 [英] Can't call a lazy lambda function with parameters via boost::phoenix::function

查看:202
本文介绍了不能通过boost :: phoenix :: function调用带有参数的惰性lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用 boost :: phoenix :: function 基于lambda函数的参数和失败。如果我调用它没有参数以这样的方式:

I tried to call boost::phoenix::function based on lambda function with parameters and failed. If I call it without parameters in such a way:

const auto closure = [](){
    cout<< "test" << endl;
};

typedef decltype(closure) ClosureType;
const boost::phoenix::function<ClosureType> lazyFunc (std::move(closure));
lazyFunc()();

所有编译好。但是当我声明lambda的至少一个参数时:

all compiles nice. But when I declare at least one parameter of lambda:

const auto  closure = [](int& param)  { cout<<"Test" << param << endl; };

typedef decltype(closure) ClosureType;
const boost::phoenix::function<ClosureType> lazyFunc (std::move(closure));
lazyFunc(arg1)(a);

编译失败,巨大的堆栈轨迹深入 boost :: result_of

compilation fails with tremendous stack trace deep inside of boost::result_of

推荐答案

假设错误指向Boost.ResultOf内部某处(如此演示中所示) ),这是因为lambda表达式的闭包类型不实现ResultOf协议。

Assuming the error points to somewhere deep inside Boost.ResultOf (as seen in this demo), that would be because the closure type of a lambda expression does not implement the ResultOf protocol.

一个有点简单的解决方法是定义 BOOST_RESULT_OF_USE_DECLTYPE ,使 boost :: result_of 绕过自己的ResultOf协议,而是使用 decltype 以计算返回类型。默认情况下不启用此选项,因为没有多少编译器(在Boost 1.51发布时)足够符合此功能的工作;计划这个符号被自动定义(通过Boost.Config)那些编译器可以处理它为1.52。

A somewhat simple workaround to that is to define BOOST_RESULT_OF_USE_DECLTYPE, which makes boost::result_of bypass its own ResultOf protocol by instead using decltype to compute return types. This is not enabled by default because not many compilers (at the time of the release of Boost 1.51) are conformant enough for this feature to work; it is planned that this symbol be defined automatically (by Boost.Config) for those compilers that can deal with it for 1.52.

这里是一个演示,它看起来像使用Boost.Phoenix与 decltype -powered boost :: result_of 。我必须将 int& 更改为 int const& ,因为 i 显然是作为 const int 转发的。这似乎是 boost :: phoenix :: function 的基本限制,使用 boost :: phoenix :: val 没有这个问题。

Here is a demo of what it looks like to use Boost.Phoenix with a decltype-powered boost::result_of. I had to change the int& to int const& because i is apparently being forwarded as a const int. This appears to be a fundamental limitation of boost::phoenix::function, using boost::phoenix::val doesn't have this problem.

这篇关于不能通过boost :: phoenix :: function调用带有参数的惰性lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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