Topic: for loop local var scope ?
Author: Steve Rumsby <steve@maths.warwick.ac.uk>
Date: 1997/10/30 Raw View
Roger Onslow wrote in message <639qi8$pf6$3@pumba.class.udg.mx>...
>I was just wondering what the final (?) ruling was on the scope of a
>variable declared in the init part of a for statement.
>
>ie. does this work (in the one block)...
>
> for (int i = 0; i < n; i++) // some statement
> for (int i = 0; i < n; i++) // some other statement
>
>ie. does the scope of the local var declared in the init end after the
>controlled statement?
>
Yes.
>Is this behaviour to be optional at all?
>
No, but I expect most compilers will provide a flag to switch back to the
old behaviour. It will then be operating in a non-conforming mode, of
course.
>Do any compilers support it yet?
>
g++ does. Don't know about any others.
>Does it break any existing code (ie make it not compile)?
>
Yes. Code like the following will fail to compile:
{
for(int i = 0; i < 10 i++) {
// ...
}
for(i = 0; i < 42; i++) { // Error, i undefined.
//...
}
}
Unless, that is...
>Does it (even worse) change the behaviour of and existing code WITHOUT an
>error?
>
Yes, unfortunately. For instance:
int i;
{
for(int i = 0; i < 10 i++) {
// ...
}
for(i = 0; i < 42; i++) { // Oops, picked up the global i
//...
}
}
I would hope compilers already produce a warning for this kind of code, but
especially when they switch to the new scoping rules they should issue a
warning. It is not terribly difficult...
Steve.
--
Steve Rumsby Computer Systems Administrator
mailto:steve@maths.warwick.ac.uk
http://www.maths.warwick.ac.uk/~steve/
tel:+44 1203 524657 fax:+44 1203 524182
PGP public key available from the above web page.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Roger Onslow" <No_Spam@hotmail.com>
Date: 1997/10/30 Raw View
I was just wondering what the final (?) ruling was on the scope of a
variable declared in the init part of a for statement.
ie. does this work (in the one block)...
for (int i = 0; i < n; i++) // some statement
for (int i = 0; i < n; i++) // some other statement
ie. does the scope of the local var declared in the init end after the
controlled statement?
Is this behaviour to be optional at all?
Do any compilers support it yet?
Does it break any existing code (ie make it not compile)?
Does it (even worse) change the behaviour of and existing code WITHOUT an
error?
Roger
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/10/30 Raw View
Roger Onslow wrote:
>
> I was just wondering what the final (?) ruling was on the scope of a
> variable declared in the init part of a for statement.
Yes, really final. It won't change anymore.
> ie. does this work (in the one block)...
>
> for (int i = 0; i < n; i++) // some statement
> for (int i = 0; i < n; i++) // some other statement
>
> ie. does the scope of the local var declared in the init end after the
> controlled statement?
The scope of i is the for loop so the above is correct code.
> Is this behaviour to be optional at all?
No, but most compiler will have a switch. But only one state
for the switch makes the compiler conformant.
> Do any compilers support it yet?
A number of them (CodeWarrior, g++, KAI C++ (= Photon C++) for
example).
> Does it break any existing code (ie make it not compile)?
Yes, I have seen code like that:
for (int i = 0; i < n; i++)
{
do something
if (condition) break;
do annother thing
}
cout << "Work done " << float(i)/n; // old rules: ok;
// new rule: i not in scope at this point
> Does it (even worse) change the behaviour of and existing code WITHOUT an
> error?
Yes, if in the above i is a variable with a wider scope.
g++ can warn about that.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/10/30 Raw View
"Roger Onslow" <No_Spam@hotmail.com> writes:
>I was just wondering what the final (?) ruling was on the scope of a
>variable declared in the init part of a for statement.
>
>ie. does this work (in the one block)...
>
> for (int i = 0; i < n; i++) // some statement
> for (int i = 0; i < n; i++) // some other statement
Yes.
>ie. does the scope of the local var declared in the init end after the
>controlled statement?
Yes.
>Is this behaviour to be optional at all?
Well, compilers are allowed to offer extensions, such as
compiler options for compiling code...
>Do any compilers support it yet?
Yes.
>Does it break any existing code (ie make it not compile)?
Yes.
>Does it (even worse) change the behaviour of and existing code WITHOUT an
>error?
Yes, but only in fairly obscure cases.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]