Boost logo

Boost :

From: Maxim Yegorushkin (e-maxim_at_[hidden])
Date: 2004-07-02 01:59:58

Dan <dan_at_[hidden]> wrote:

> It seems you start it at begin+1. I don't know why but it's probably related
> to the next question.

mpl::fold algorithms resemble std::accumulate (the version that takes a binary functor) the only difference being that high level mpl algorithms (mpl::fold is among of them) take ranges instead of [first, last) iterators.

I wonder, why mpl creators adhering stl concepts chose 'fold' name instead of 'accumulate' name which is well-known for c++ programmers?

Just like std::accumulate mpl::fold takes an initial value. If I were to provide that initial value in that code snippet by myself I would have to deduce the type of the sequence elements which may have different types. So, I simply took the first element of the sequence as the initial value and then iterated on [first + 1, last).

> What is happening on this line?
> mpl::plus<mpl::deref<mpl::_2>, mpl::_1>

Here is a mpl::lambda expression which results in metufunction that takes two arguments, dereferences the second and then adds them. It might have been written as mpl::plus<mpl::_1, mpl::deref<mpl::_2> > as well, but in the latter case you will have to insert a space between the adjacent '> >' at the end, so I rearranged it :).

As it already was mentioned you might want use mpl::fold instead of mpl::iter_fold. The former will save you from typing that deref in the lambda expression (that is, mpl::plus<mpl::_, mpl::_> using unnamed placeholders as we don't care about the order of arguments any more).

> As near as I can tell you deref the iterator in <_2> and add that with _1.


> How does this _1 and _2 stuff work anyway?

They are placeholders, the same concept like in boost::bind and boost::lambda. You might like reading mpl docs, it explains the stuff.

Maxim Yegorushkin

Boost list run by bdawes at, gregod at, cpdaniel at, john at