2

我尝试 boost::phoenix::function使用带参数的 lambda 函数进行调用,但失败了。如果我以这种方式不带参数调用它:

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

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

一切都很好。但是当我声明至少一个 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内部有大量堆栈跟踪

4

1 回答 1

2

假设错误指向 Boost.ResultOf 内部的某个深处(如本演示所示),那将是因为 lambda 表达式的闭包类型没有实现 ResultOf 协议。

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

是一个演示如何使用带有 -powered 的decltypeBoost.Phoenix boost::result_of。我不得不将其更改为int&int const&因为i显然被转发为const int. 这似乎是一个基本的限制boost::phoenix::function,使用boost::phoenix::val没有这个问题。

于 2012-10-21T03:21:33.450 回答