Boost logo

Geometry :

Subject: [ggl] pre-/post-increment op on iterators
From: Mateusz Loskot (mateusz)
Date: 2009-04-28 08:27:21


Bruno Lalande wrote:
> Hi,
>
>> Yes, boost::foreach is a good idea but perhaps it's too big revolution
>> in terms of C++ programmers habits, similarly why std::for_each is
>> rarely used (in general) :-) for/while loops still are preferred.
>
> Personally, the reason why I don't use it much is that I don't find it
> convenient to have to explicit declare the value_type. For example, to
> iterate on a vector<pair<int, pair<float, float> > > I have to write
> something like:
>
> BOOST_FOREACH(pair<int, pair<float, float> > p, v)

Why not to use typedef shortcut

typedef vector<pair<int, pair<float, float> > > v_t;
// use v_t later in the code
...
BOOST_FOREACH(v_t::value_type p, v)

> I find it much more convenient to use std::for_each and a functor, or
> a hand-written loop with BOOST_AUTO.
> But maybe I missed something that
> would ease the use of boost::foreach is that case?

Yes, I found it convenient too.
However, I can understand the rationale behind BOOST_FOREACH.
Even if it does require explicit use of value_type, on the other
hand it frees you from knowledge about how container/collection is
being iterated. Iteration is hidden as it is by algorithms.
Second, if functor introduces extra entities into code,
and in some cases it may make code less readable/understandable.
BOOST_FOREACH is handy as replacement for simple loop, plus it
provides nice genericness.

Best regards,

-- 
Mateusz Loskot, http://mateusz.loskot.net

Geometry list run by mateusz at loskot.net