Hi,
Is this possible at all?
struct A : boost::phoenix::function{
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
wheel?
template<class Self>
struct lazy{
boost::phoenix::function const* const lazy_;
lazy(Self const& self) : lazy_(new boost::phoenix::function(self)){}
template<class Arg>
const
boost::phoenix::actor,
Arg
>, 2l> >
operator()(Arg const& arg) const{return (*lazy_)(arg);}
~lazy(){delete(lazy_);}
};
struct A : lazy<A>{
using lazy<A>::operator();
A() : lazy<A>(*this){}
A(A const& other) : lazy<A>(*this){}
typedef
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
code:
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.)
Thanks,
Alfredo