Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-10-10 13:23:28


"Eric Niebler" <eric_at_[hidden]> writes:

> Russell Hind wrote:
>> One thing that worries me about the inital example is if you forget
>> to declare i as a reference, then it breaks.
>
> No, it doesn't. Your iteration variable can be a reference or a
> value, whichever you want.
>
>> Could the type of the local not be auto-declared?
>
> That's not a good idea -- putting the type there is more explicit, and
> it makes it look more like a for(;;) loop. Besides, you might want a
> conversion to occur; for example, BOOST_FOREACH( int i,
> vector_of_bools ). Also it's impossible to achieve without typeof()
> AFAICT.

That all sounds unconvincing to me. I believe the conversion should
be more explicit if you want it:

FOR_EACH(b, vector_of_bools)
{
   whatever((int)b);
}

>> BOOST_FOREACH(i, int_list)
>
> This syntax is reserved for when you have predeclared the loop
> variable, as in:
>
> int i;
> BOOST_FOREACH( i, int_list )
> { ... }
>
> That way you can have access to the loop variable after the loop finishes.

It's important to be able to do that; no doubt. It'd be nice to find
a more-verbose syntax for that one, though ;^)

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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