使用 Boost.Fusion 函数列表

发布于 2024-10-26 16:09:29 字数 825 浏览 1 评论 0原文

我试图将函数对象列表应用于以下代码中的某个值。 但是这段代码导致 错误
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潜移默化 2024-11-02 16:09:29

您的问题与 boost.fusion 完全无关。相反,您的问题是由于尝试从 boost.lambda 仿函数调用非惰性仿函数(不使用 bind)而引起的。将 boost::fusion::for_each 与适当的函子而不是 boost.lambda 函子一起使用可以得到您期望的结果:

#include <iostream>
#include <boost/function.hpp>
#include <boost/fusion/include/at_c.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/for_each.hpp>

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; }

struct proper_functor
{
    typedef void result_type;

    proper_functor(double x, double y) : x_(x), y_(y) { }

    template<typename F>
    void operator ()(F const& f) const { std::cout << f(x_, y_) << '\n'; }

private:
    double x_, y_;
};

int 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, proper_functor(10., 100.));
}

顺便说一句,在以下情况中遇到 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). Using boost::fusion::for_each with a proper functor instead of a boost.lambda functor nets the results you expect:

#include <iostream>
#include <boost/function.hpp>
#include <boost/fusion/include/at_c.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/for_each.hpp>

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; }

struct proper_functor
{
    typedef void result_type;

    proper_functor(double x, double y) : x_(x), y_(y) { }

    template<typename F>
    void operator ()(F const& f) const { std::cout << f(x_, y_) << '\n'; }

private:
    double x_, y_;
};

int 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, proper_functor(10., 100.));
}

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.

阳光下的泡沫是彩色的 2024-11-02 16:09:29

正如 ildjarn 提到的,直接应用函数调用运算符 ()
lambda::placeholders 似乎无法正常工作。
在这种情况下,lambda::bind 可能会满足目的。
例如:

fusion::for_each(tt, cout << lambda::bind( lambda::_1, 10, 100 ) << '\n')

希望这有帮助

As ildjarn mentioned, applying function call operator () directly to
lambda::placeholders seems not to work somehow.
Probably lambda::bind will meet the purpose in this case.
For example:

fusion::for_each(tt, cout << lambda::bind( lambda::_1, 10, 100 ) << '\n')

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文