Boost logo

Boost :

Subject: Re: [boost] nested BOOST_FOREACH and -Wshadow
From: John Bytheway (jbytheway+boost_at_[hidden])
Date: 2009-02-07 07:51:37


Dustin Spicuzza wrote:
> Hey,
>
> I really enjoy using BOOST_FOREACH on containers, saves a ton of typing.
> However, one semi-annoying problem that I've ran into is that nesting
> instances of BOOST_FOREACH causes the gcc argument -Wshadow to complain
> quite a bit.
<snip>
> Now, I know that some people don't believe in getting rid of warnings
> just for warnings sake, but the fix for this would be extremely trivial
> and useful (given the amount of noise that it generates) -- just add a
> new macro, BOOST_FOREACH_NESTED, which has a third parameter denoting
> the level of nesting. That parameter would be token-pasted onto the end
> of the variable name, and get rid of the warnings. One could define up
> to N more macros of the form BOOST_FOREACH_NESTED_N that get rid of the
> third parameter, by calling that macro appropriately. This wouldn't
> break any existing code, and would allow those that choose to get rid of
> the warnings.

I'd suggest a simpler (from the user perspective) solution would be to
have BOOST_FOREACH paste __LINE__ into its variable names. Then the
problem would only occur if nested BOOST_FOREACHs were used on the same
line. Asking users not to do that seems a lot more reasonable than
asking them not to use -Wshadow or not to use nested BOOST_FOREACHs at all.

John Bytheway


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