[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: AW: AW: AW: non-locals are only sometimes global?
- From: Florian Weimer <fw@...>
- Date: Thu, 22 Jul 2010 23:19:45 +0200
* David Kastrup:
>> http://www.wxwidgets.org/develop/standard.htm - "6. Don't declare
>> variables inside for() ".
>
> [...] or, even better, use different names for the variables in the
> different for loops (in particular, avoid mute variable names like i
> above) -- then you can declare them in the loop statement and don't
> pollute the outer name space with local loop variables.
Here's another one, which also gives the reason:
| Do not declare loop variables in a for loop.
|
| for (int i = 0; i < 4; ++i) { // do NOT do this
|
| Although the C++ standard states that the variable i is considered to
| be defined inside the scope of the loop, some compilers (including
| Microsoft Visual C++) incorrectly consider the variable to be declared
| outside the loop. This causes problems if you have two for loops which
| use the same variable name as a loop index. For example, the following
| will not work in Visual C++:
| The easiest thing to do is to declare your loop index variable once
| outside of the loops:
|
| int i; // this will work in any compiler
| for (i = 0; i < 4; ++i) printf("foo\n");
| for (i = 0; i < 4; ++i) printf("bar\n");
<http://www.isi.edu/nsnam/ns/codestyle.html>
The Visual C++ version which this guide is referencing is version 6,
which used to be really popular and is probably still used to maintain
legacy applications. Therefore, I expect anti-for-scope style rules
to be somewhat popular in the industry (comparable to anti-generics
rules in the Java world).
- References:
- non-locals are only sometimes global?, Warlich, Christof
- Re: non-locals are only sometimes global?, steve donovan
- AW: non-locals are only sometimes global?, Warlich, Christof
- Re: AW: non-locals are only sometimes global?, Peter Odding
- AW: AW: non-locals are only sometimes global?, Warlich, Christof
- Re: AW: AW: non-locals are only sometimes global?, Peter Odding
- AW: AW: AW: non-locals are only sometimes global?, Warlich, Christof
- Re: AW: AW: non-locals are only sometimes global?, Matthew Wild
- AW: AW: AW: non-locals are only sometimes global?, Warlich, Christof
- Re: AW: AW: AW: non-locals are only sometimes global?, David Kastrup
- Re: AW: AW: AW: non-locals are only sometimes global?, Florian Weimer
- Re: AW: AW: AW: non-locals are only sometimes global?, David Kastrup
- Re: AW: AW: AW: non-locals are only sometimes global?, Peter Cawley
- Re: AW: AW: AW: non-locals are only sometimes global?, David Kastrup