使用 Boost.Fusion 函数列表
我试图将函数对象列表应用于以下代码中的某个值。 但是这段代码导致 错误
boost_1_44\boost\fusion\algorithm\iteration\detail\for_each.hpp(82): error C2064:
如何将函数对象列表应用于某个值?
double doublef2(double x,double y){return 2*x+y; }
double doublef3(double x,double y){return 3*x*y; }
double doublef4(double x,double y){return 4+x*y; }
main(){
boost::fusion::vector<
boost::function<double (double,double)>,
boost::function<double (double,double)>,
boost::function<double (double,double)>
> tt;
boost::fusion::at_c<0>(tt)= & doublef2;
boost::fusion::at_c<1>(tt)= & doublef3;
boost::fusion::at_c<2>(tt)= & doublef4;
boost::fusion::for_each(tt, std::cout << boost::lambda::_1(10,100) << '\n');
}
I am trying to apply list of function object to some value in the following code.
But this code cause
err
boost_1_44\boost\fusion\algorithm\iteration\detail\for_each.hpp(82): error C2064:
How to apply list of function object to some value?
double doublef2(double x,double y){return 2*x+y; }
double doublef3(double x,double y){return 3*x*y; }
double doublef4(double x,double y){return 4+x*y; }
main(){
boost::fusion::vector<
boost::function<double (double,double)>,
boost::function<double (double,double)>,
boost::function<double (double,double)>
> tt;
boost::fusion::at_c<0>(tt)= & doublef2;
boost::fusion::at_c<1>(tt)= & doublef3;
boost::fusion::at_c<2>(tt)= & doublef4;
boost::fusion::for_each(tt, std::cout << boost::lambda::_1(10,100) << '\n');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题与 boost.fusion 完全无关。相反,您的问题是由于尝试从 boost.lambda 仿函数调用非惰性仿函数(不使用
bind
)而引起的。将 boost::fusion::for_each 与适当的函子而不是 boost.lambda 函子一起使用可以得到您期望的结果:顺便说一句,在以下情况中遇到 boost.fusion 的这种用法会很奇怪真实代码;融合容器适用于同类类型,如果您使用相同的类型,请使用 std::array/std::tr1::array/boost ::array 相反,可以节省一些编译时间。
Your problem is entirely unrelated to boost.fusion. Rather, your problem is caused by trying to invoke a non-lazy functor from a boost.lambda functor (without using
bind
). Usingboost::fusion::for_each
with a proper functor instead of a boost.lambda functor nets the results you expect:As an aside, it would be quite strange to encounter this usage of boost.fusion in real code; fusion containers are meant for homogeneous types, if you're using all the same type, use
std::array
/std::tr1::array
/boost::array
instead and save yourself some compile time.正如 ildjarn 提到的,直接应用函数调用运算符
()
lambda::placeholders 似乎无法正常工作。
在这种情况下,
lambda::bind
可能会满足目的。例如:
希望这有帮助
As ildjarn mentioned, applying function call operator
()
directly tolambda::placeholders
seems not to work somehow.Probably
lambda::bind
will meet the purpose in this case.For example:
Hope this helps