Boost logo

Boost Users :

Subject: Re: [Boost-users] [phoenix] function with reference implementation
From: alfC (alfredo.correa_at_[hidden])
Date: 2010-09-10 20:31:36


On Sep 10, 4:36 pm, Joel de Guzman <j..._at_[hidden]> wrote:
> On 9/11/10 4:37 AM, alfC wrote:
>
>
>
> > I noticed that in some contexts I have both the object class and the
> > corresponding phoenix function
>
> >    class impl;
> >    ...
>
> >    impl f_impl;
> >    boost::phoenix::function<impl>  f(f_impl);
>
> > now to avoid the copying I could use:
>
> >    boost::phoenix::function<impl&>  f(f_impl);
>
> > which works. Although
> >    boost::phoenix::function<impl const&>  f(f_impl); //doesn't work.
> > (error below)
>
> > Is there any problem with using references at all, in the first place?
>
> I'm not sure. Could you provide a minimal cpp file I can try?

Yes, see below. Althougth now that I made this example I realize that
the underlying object implementation is copied many times anyway (at
least 4 times in the example), I guess during the building up of the
expression template. [The final objective was to make an object
function that not only evaluates but also is able to take expression
and build expression templates].

#include<boost/spirit/home/phoenix.hpp>
#include<iostream>
struct F{
        double a_;
        F(double a) : a_(a){std::cout << "F constructed" <<std::endl;}
        F(F const& other) : a_(other.a_){std::cout << "F copied"
<<std::endl;}
        template <typename Arg> struct result{
                typedef double type;
        };
        result<double>::type operator()(double d) const{
                return a_*d*d;
        }
};

int main(){
        using boost::phoenix::function;
        using namespace boost::phoenix::arg_names;
        {
                F f(4.);
                boost::phoenix::function<F> pf(f);
                std::cout << pf(arg1+1.)((double const&)5.) << std::endl;
        }
        {
                F f(4.);
                boost::phoenix::function<F&> pf(f);
                std::cout << pf(arg1+1.)((double const&)5.) << std::endl;
        }
        /* block doesn't work
        {
                F f(4.);
                boost::phoenix::function<F const&> pf(f); //does not compile
                std::cout << pf(arg1+1.)((double const&)5.) << std::endl;
        }
        */
}

prints:
F constructed
F copied
F copied
F copied
F copied
F copied
144
F constructed
F copied
F copied
F copied
F copied
144


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net