Proposed BOOST_FOREACH

I like the BOOST_FOREACH macro, and I'm going to be spending a lot of time playing around with it. For working with iterator ranges, however, this sort of thing seems to be the best that I can come up with: //pFirst and pLast are iterators that return an int on dereference //they could be some_containter<int>.rbegin() and some_container<int>.rend(), for example BOOST_FOREACH(int i, vector<int>(pFirst,pLast)); It's not that ugly, but creating a new vector to do a loop is quite an efficiency hit. Is there an better way to do this? Joel Eidsath

Joel Eidsath <jeidsath@gmail.com> writes:
I like the BOOST_FOREACH macro, and I'm going to be spending a lot of time playing around with it.
For working with iterator ranges, however, this sort of thing seems to be the best that I can come up with:
//pFirst and pLast are iterators that return an int on dereference //they could be some_containter<int>.rbegin() and some_container<int>.rend(), for example BOOST_FOREACH(int i, vector<int>(pFirst,pLast));
It's not that ugly, but creating a new vector to do a loop is quite an efficiency hit. Is there an better way to do this?
I think BOOST_FOREACH(int i, make_iterator_range(whatever.rbegin(), whatever.rend())) does it (?) http://www.boost.org/libs/range/doc/utility_class.html#iter_range -- Dave Abrahams Boost Consulting www.boost-consulting.com

Works great for me. This sort of macro really gives me a lot of hope for C++. I didn't think it was possible, actually. Generic programming is just crying out to be combined with functional programming. Lambda is great for small stuff, but it's trouble when things get complicated. BOOST_FOREACH, on the other hand, only works for one (important) task but handles added complexity very well. I'm already thinking of places to combine the two. Joel Eidsath
I think
BOOST_FOREACH(int i, make_iterator_range(whatever.rbegin(), whatever.rend()))
does it (?)
http://www.boost.org/libs/range/doc/utility_class.html#iter_range
participants (2)
-
David Abrahams
-
Joel Eidsath