Boost logo

Boost :

From: Giovanni Bajo (giovannibajo_at_[hidden])
Date: 2002-06-27 02:11:16

----- Original Message -----
From: "Douglas Gregor" <gregod_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Wednesday, June 26, 2002 4:09 PM
Subject: [boost] MPL algorithms vs. type lists

> // --------------STLize outer loop-------------
> struct Merge {
> typedef void result_type;
> void operator()(Location& loc) const
> {
> ValueIterator val = loc.begin();
> Value init = *val++;
> loc.merged = accumulate(val, loc.end(), init, &merge);
> }
> };
> for_each(locations.begin(), locations.end(), Merge());

Well, I'd use a member function:

void Location::Merge(void)
    ValueIterator val = begin();
    Value init = *val++;
    merged = accumulate(val, end(), init, &merge);

for_each(locations.begin(), locations.end(), bind(&Location::Merge, _1));

which is slightly more correctly encapsulated, since you're actually doing
an action over a Location object and you probably want to be able to call
loc.Merge() somewhere else in the code even outside this specific example.
So, from my point of view, there is no code bloat at all in using STL for
loops. Location::Merge() is a short 3-liners that acts on a Location, and I
can call it either manually or repeated for a whole container using

> Not that I'm complaining -- I find the
> last version most preferable (indeed, that's a slightly simplified version
> what I wrote a few days ago), because it separates the complexity nicely:
> for_each line obviously says 'we're merging values at each location'. Want
> know how the merge works? Look up the Merge function object to see how
> merging is done.

Yes, I agree, and I think that putting Merge() within Location makes even
more sense.
I always prefer STL algorithms over hand-coded loops.

Giovanni Bajo

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