
Dan <dan@eloff.info> 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.
You are very close: template<typename Sequence> struct sum { typedef typename mpl::iter_fold< mpl::iterator_range< // [begin + 1, end) typename mpl::next<typename mpl::begin<Sequence>::type>::type , typename mpl::end<Sequence>::type > , typename mpl::deref<typename mpl::begin<Sequence>::type>::type , mpl::plus<mpl::deref<mpl::_2>, mpl::_1> >::type type; }; Note that there must be a special case for empty sequences. -- Maxim Yegorushkin