我尝试基于带有参数的 lambda 函数调用 boost::phoenix::function
并失败。如果我以这种方式不带参数调用它:
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 深处有大量堆栈跟踪
最佳答案
假设错误指向 Boost.ResultOf 内部深处的某个位置(如 this demo 中所示),那是因为 lambda 表达式的闭包类型未实现 ResultOf 协议(protocol)。
一个稍微简单的解决方法是定义 BOOST_RESULT_OF_USE_DECLTYPE
,这使得 boost::result_of
通过使用 decltype
绕过它自己的 ResultOf 协议(protocol)计算返回类型。默认情况下不启用此功能,因为没有多少编译器(在 Boost 1.51 发布时)足够符合此功能的要求;计划为那些能够在 1.52 中处理该符号的编译器自动定义该符号(由 Boost.Config)。
Here是使用 Boost.Phoenix 与由 decltype
驱动的 boost::result_of
的演示。我必须将 int&
更改为 int const&
,因为 i
显然是作为 const int
转发的。这似乎是 boost::phoenix::function
的基本限制,使用 boost::phoenix::val
则不会出现此问题。
关于c++ - 无法通过 boost::phoenix::function 调用带参数的惰性 lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12994040/