
"Andy Little" <andy@servocomm.freeserve.co.uk> 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?
It doesn't require type/template registration. Regards, Arkadiy