Boost logo

Boost Users :

Subject: Re: [Boost-users] foreach question
From: Jerry Jeremiah (jerry.jeremiah_at_[hidden])
Date: 2010-10-17 16:49:08


Pete Bartlett <pete <at> pcbartlett.com> writes:

>
> See this thread http://thread.gmane.org/gmane.comp.lib.boost.user/54162
> And the resulting Trac ticket: https://svn.boost.org/trac/boost/ticket/3469
>
> for a essentially negative answer.
>

Personally, I like the answer in that thread that said:

#define INDEX_FOREACH(index,a,b) \
    for(unsigned int index = static_cast<unsigned int>(-1); \
        index == static_cast<unsigned int>(-1);) \
            BOOST_FOREACH(a,b) if(++index,true)

The for loop just does a variable declaration in a way that means you won't
get bit by using it somewhere that doesn't allow variable declarations.
Convoluted but important.

Of course, it isn't that generic - it always assumes you want an int index.
But then that is what the question said...

Jerry


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