Boost logo

Boost Users :

Subject: Re: [Boost-users] Foreach For iterating two containers
From: Pete Bartlett (pete_at_[hidden])
Date: 2010-02-21 15:45:52


Gokulakannan Somasundaram wrote:

>Meanwhile, a gentleman helped me by providing this code. Posting it here
for any comments
>#include <boost/typeof/typeof.hpp>
>
>#define FOREACH_2(def1, def2, r1, r2)
\
>
> for(std::pair<BOOST_TYPEOF((r1).begin()),
\
> BOOST_TYPEOF((r2).begin())>
_it12_((r1).begin(),(r2).begin()),\
>
> _e12_((r1).end(), (r2).end());
\
[...]

If you don't mind the "weight" of BOOST_TYPEOF just for a for loop, it's not
bad as some of the complexity is hidden in that inner macro. However bear in
mind in falls short of the original FOREACH because it uses its arguments r1
and r2 repeatedly which may be inefficient, or even flat out wrong, if r1
and/or r2 are temporaries.

Also, there is an open ticket against some extra functionality in
Boost.Foreach - BOOST_FOREACH_FIELD I think it is called. If that macro came
to pass, I think you could solve your use case with a make_zip_range.

Pete


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net