Boost logo

Boost :

From: Daniel Wallin (dalwan01_at_[hidden])
Date: 2004-07-01 06:22:22


Dan wrote:

> I'm trying to use iter_fold with a vector_c to compute the sum of all of the
> integers in the container.
>
> What I have is:
>
> template<typename Sequence>
> struct sum
> {
> typedef typename mpl::iter_fold<
> Sequence,
> typename mpl::begin<Sequence>::type,
> mpl::plus< mpl::deref<_1>, mpl::deref<_2> >;
> // ^ what should this really be?
> > // what do i put after here?
> };
>
> Which obviously won't work. I don't know how to keep a running total as I
> iterate over the sequence with plus.

I don't know why you are using iter_fold instead of just fold here, but
anyway:

   template<typename Sequence>
   struct sum
   {
       typedef typename mpl::iter_fold<
           Sequence
         , mpl::int_<0>
         , mpl::plus<mpl::_1, mpl::deref<mpl::_2> >
>::type type;
   };

HTH,

-- 
Daniel Wallin

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk