Boost logo

Boost Users :

Subject: Re: [Boost-users] [Proto] Storing an Expression Template as a Data Member
From: Hossein Haeri (powerprogman_at_[hidden])
Date: 2011-01-06 18:48:32


Hi Eric,

> > MyClass mc(_1 >> &f >> &g >>
> &h);
> > mc.set_input(-61.1);
> > cout << mc.value();//returns h(g(f(-61.1)))

Here is my solution:

template<typename Expr>
struct MyClass
{
  MyClass(const Expr& e): formula_(e) {}
  double calculate(double d) const
  {return evaluate(formula_, d);}
private:
  const Expr formula_;//***
};

#define DEFINE_MY_CLASS(name, exp) MyClass<BOOST_TYPEOF(exp)> name(exp)
...

So that my customers can say:

DEFINE_MY_CLASS(mc, _1 >> &f >> &g >> &h);
cout << mc.calculate(-6.1);//prints h(g(f(-61.1)))

Does the fact that I store formula_ by value (as in line ***) shoot the trouble of dangling references?

> I would suggest transforming the expression into a unary
> function object (see my article on cpp-next.com about function
> composition),

I do follow that series of yours and have to say it's superb! :) I don't see how that's connected though. So, if you think your suggested solution above would be better than that of mine, I'd be more than happy to see more explanation. Specifically, what are you exactly referring to when you use the term "expression" above? And, leave it if you'd think my solution is good enough... :D

TIA,
--Hossein


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