Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2005-05-02 09:56:39


"Andy Little" <andy_at_[hidden]> wrote

> FWIW A naive method to do the above is as:
>
> #define FOR_EACH(cont) \
> for (BOOST_TYPEOF( cont )>::iterator _ = cont ## .begin();\
> _ != cont ## .end();\
> ++ _ )

FWIW the following seems to do the job including for c-style strings and const
containers. Is the BOOST_FOREACH complexity necessary?

 (Again I prefer the lambda style iterator, for versatlity ... )

#include <boost/typeof/typeof.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>

#define FOR_EACH(cont) \
    for (BOOST_AUTO( _ , boost::begin(cont) );\
            _ != boost::end(cont);\
            ++ _ )

int main()
{
    const char * test = "hello";
    FOR_EACH(test){
        if ( _ == boost::begin(test) ){
            std::cout<< "[";
        }
        std::cout << *_;
        if( std::distance( _ , boost::end(test) ) > 1){
            std::cout <<',';
        }
        else {
            std::cout << ']';
        }
    }
}

Are there advantages to the BOOST_FOREACH approach?

regards
Andy Little


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk