Boost logo

Boost Users :

Subject: [Boost-users] [phoenix] function object that is also lazy
From: alfC (alfredo.correa_at_[hidden])
Date: 2011-04-15 21:55:55


 Is this possible at all?

struct A : boost::phoenix::function<A const&>{
        double operator()(double const& d) const{return 5.;}

such that an object function it is not only a function but also a lazy
function of itself?
        std::clog << a(4.) << std::endl;
        std::clog << a(arg1 + 1.)(4.) << std::endl;

the code above doesn't work because A is not complete when the type
function<A> is instatiated. However after some experimentation I got
the following. The question is, does it really work in more exotic
context? Is there a possible simplification? Am I reinventing the

template<class Self>
struct lazy{
        boost::phoenix::function<Self const&> const* const lazy_;
        lazy(Self const& self) : lazy_(new boost::phoenix::function<Self
        template<class Arg>
boost::proto::argsns_::term<Self>, 0l>,
>, 2l> >
        operator()(Arg const& arg) const{return (*lazy_)(arg);}

struct A : lazy<A>{

        using lazy<A>::operator();
        A() : lazy<A>(*this){}
        A(A const& other) : lazy<A>(*this){}

        double result_type; result_type
        operator()(double const& d) const{return d;}


so, any function object B can be a lazy function of it self provided
that we use the correct CRTP argument (lazy<B>) and manually add the

        using lazy<A>::operator();
        B() : lazy<A>(*this){}
        B(A const& other) : lazy<A>(*this){}

inside the class. This feature seems to make the usage of the function
object b more flexible, since it can we used in many different
contexts. For example, as

  integrate ( b, 1., 2.);

or as

 integrate ( 2.*b(arg1 + 1.) , 1., 2.)


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at