[for_each] suggestion for new macro

Hi Eric, Here's an idea for a macro to complement BOOST_FORACH. Use example: BOOST_ITER_FOREACH( i, get_range() ) { std::cout << *i; } It shoul be quite easy to generate a for-loop behind the scene. The motivation for having this macro would be that often you actually need access to the iterator because it provides much more details than the value-type, eg. - the base-iterator - index etc. With the upcoming range adaptors, the creation of ranges/iterators of unknown type could be much more common. -Thoorsten

Thorsten Ottosen wrote:
BOOST_ITER_FOREACH( i, get_range() ) { std::cout << *i; }
Eric Niebler wrote:
It's not implementable in standard C++, unless you know something I don't.
Hello Eric, can you provide some hints why this is not possible? For example, can't we use some (boost::?)typeof logic to fetch the iterators type? ;-) David.

David Gruener wrote:
Thorsten Ottosen wrote:
BOOST_ITER_FOREACH( i, get_range() ) { std::cout << *i; }
Eric Niebler wrote:
It's not implementable in standard C++, unless you know something I don't.
Hello Eric, can you provide some hints why this is not possible? For example, can't we use some (boost::?)typeof logic to fetch the iterators type? ;-)
Even if you were willing to accept that, in order to use this macro, you may have to register types with BOOST_TYPEOF (an interface I'm not overly fond of), there are still problems. Registering types for iterators is notoriously difficult. How would you write the BOOST_TYPEOF registrations for the std::vector iterators, for instance, considering that they could very well be defined as: namespace std { template<class T, class Al> struct vector { struct iterator { //... }; }; } // register std::vector<T,Al>::iterator BOOST_TYPEOF_REGISTER_TYPE(?????) Has anyone solved this problem? -- Eric Niebler Boost Consulting www.boost-consulting.com

"Eric Niebler" <eric@boost-consulting.com> wrote
Even if you were willing to accept that, in order to use this macro, you may have to register types with BOOST_TYPEOF (an interface I'm not overly fond of), there are still problems. Registering types for iterators is notoriously difficult. How would you write the BOOST_TYPEOF registrations for the std::vector iterators, for instance, considering that they could very well be defined as:
namespace std { template<class T, class Al> struct vector { struct iterator { //... }; }; }
// register std::vector<T,Al>::iterator BOOST_TYPEOF_REGISTER_TYPE(?????)
Has anyone solved this problem?
No. But you can write something like following: for (BOOST_TYPEOF(v)::iterator it = v.begin(); ... Of course, this introduces another issue -- the need to choose between "iterator" and "const_iterator" based on the constness of the vector. But this should be possible to do... The registration problem is still there... One thing that could be done (and was discussed before in relation to result_of) is to enable compile-time detection of whether typeof is possible for a given expression (meaning native typeof is used or all needed types are registered) without causing compiler error. In theory then, you could use it to optimize your FOREACH provided typeof is available. Regards, Arkadiy Regards, Arkadiy

Eric Niebler wrote:
David Gruener wrote:
Thorsten Ottosen wrote:
BOOST_ITER_FOREACH( i, get_range() ) { std::cout << *i; }
I'd love to see that included.
It's not implementable in standard C++, unless you know something I don't.
Ah yes, sorry about that. If only was had auto. -Thorsten
participants (4)
-
Arkadiy Vertleyb
-
David Gruener
-
Eric Niebler
-
Thorsten Ottosen