|
Boost Users : |
Subject: Re: [Boost-users] [Proto] Higher order function
From: Eric Niebler (eric_at_[hidden])
Date: 2010-04-27 12:57:33
On 4/27/2010 9:31 AM, noman javed wrote:
> Hi
>
> I have just started experimenting with proto. I have the following grammer
>
> G ::= Apply f G | Arrray
That's BNF. You have to pick C++ syntax that corresponds to that.
> where the code of the simple array and apply classes are given below: How can I transform this code in the proto
>
> template<typename T>
> class Array
> {
> public:
> Array(int size) : size(size)
> {
> data = new T[size];
> }
>
> operator=() { ... }
>
> private:
> int size;
> T* data;
> };
Rename Array as ArrayImpl, and then define Array as:
// untested
template<typename T>
class Array
: proto::extends<
typename proto::terminal<ArrayImpl<T> >::type
, Array<T>
>
{
Array(int size)
: proto::extends<
typename proto::terminal<ArrayImpl<T> >::type
, Array<T>
>( proto::terminal<ArrayImpl<T> >::type::make(
ArrayImpl<T>(size)))
{}
Array& operator=(Array const &) {
... do copy if you want Array to have value
semantics. If you want it to build an expression
template representing a lazy assignment, do something
else.
}
};
Now Array is a full proto terminal type, and expressions involving them
will build expression trees.
>
> I have another class apply that applies the function (its first argument) on its other arguments
>
> template<typename F, typename EXP>
> struct apply
> {
> Array<typename F::result_type>& operator()(EXP & exp)
> { ... }
>
> typename F::result_type operator[](typename F::input_type i)
> {
> return f(i);
> }
>
> };
>
> The Apply class can be a unary or binary or infact an n-ary class.
> How can I protofy this expression template?
It doesn't look to me like there is anything lazy about apply. So it
isn't part of your expression template. Rather, it accepts expressions
and an evaluator, and evaluates it.
There may be a more elegant solution. What are your evaluators?
-- Eric Niebler BoostPro Computing http://www.boostpro.com
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