Thread View: comp.std.c++
4 messages
4 total messages
Started by Robert Martin
Mon, 25 Sep 1995 16:56
Scope of variables declared in if/while/for
Author: Robert Martin
Date: Mon, 25 Sep 1995 16:56
Date: Mon, 25 Sep 1995 16:56
51 lines
1890 bytes
1890 bytes
In reading the WP I see that the scope of variables declared in 'for'
statements is now identical with the scope of variables declared in
'if' and 'while' statements. i.e. any variable declared in a 'for',
'if' or 'while' statement is local to the statement, and to the
controlled statement, but not to the enclosing scope.
e.g.
if ((Circle* c = dynamic_cast<Circle*>(s)) != 0)
{
// c is local to this scope.
}
// c is unknown to this scope.
-------------------------
And the same for:
for (Iterator<Shape*>i(s); i; i++)
{
// i is local to this scope
}
// i is unknown in this scope.
------------------------------
Do I have this right? If so, doesn't this break a lot of old C++
programs that depended upon variables declared in the 'for' statement
being local to the enclosing scope.
e.g. the following used to be legal, but now is illegal.
for (int i=0; i<100; i++)
{
// expressions using i;
}
if (i == 100) // loop ran to end.
--
Robert Martin | Design Consulting | Training courses offered:
Object Mentor Assoc.| rmartin@oma.com | OOA/D, C++, Advanced OO
2080 Cranbrook Rd. | Tel: (708) 918-1004 | Mgt. Overview of OOT
Green Oaks IL 60048 | Fax: (708) 918-1023 | Development Contracts.
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html.
]
Re: Scope of variables declared in if/while/for
Author: Matt Austern
Date: Mon, 25 Sep 1995 20:43
Date: Mon, 25 Sep 1995 20:43
46 lines
2785 bytes
2785 bytes
In article <MATT.95Sep25095657@physics2.Berkeley.EDU> rmartin@rcmcon.com (Robert Martin) writes:
> Do I have this right? If so, doesn't this break a lot of old C++
> programs that depended upon variables declared in the 'for' statement
> being local to the enclosing scope.
>
> e.g. the following used to be legal, but now is illegal.
>
> for (int i=0; i<100; i++)
> {
> // expressions using i;
> }
>
> if (i == 100) // loop ran to end.
That's right. The change does break existing code. In fact, I
suspect that the change will break almost every nontrivial C++
program. (Except for programs that were written with this change in
mind.)
Just about the only good news is that most programs will break
noisily: it's only when you're already playing dangerous games with
variables of the same name in different scopes that a legal program
that does one thing will turn into a legal program that does something
else. The other piece of good news is that this is a simple enough
change that programs can probably be converted by a simple mechanical
translator. (Has anyone written one yet?) I'm sure, also, that all
major compiler vendors will provide backward compatibility switches.
The latest version of gcc implements the new rule about the scope of
variables declared in for loops. You can try your code out using
gcc 2.7.0 to find out just how many things break.
I still don't quite understand the politics behind the change. The
new rule is clearly an improvement, and I even think it's enough of an
improvement so that breaking so many programs is an acceptable cost,
but I hadn't expected anythig of the sort from the standards
committee. I'd thought they were much more conservative than that.
--
Matt Austern He showed his lower teeth. "We
matt@physics.berkeley.edu all have flaws," he said, "and
http://dogbert.lbl.gov/~matt mine is being wicked."
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Scope of variables declared in if/while/for
Author: John Max Skaller
Date: Wed, 27 Sep 1995 06:12
Date: Wed, 27 Sep 1995 06:12
51 lines
2416 bytes
2416 bytes
rmartin@rcmcon.com (Robert Martin) wrote:
>
>In reading the WP I see that the scope of variables declared in 'for'
>statements is now identical with the scope of variables declared in
>'if' and 'while' statements. i.e. any variable declared in a 'for',
>'if' or 'while' statement is local to the statement, and to the
>controlled statement, but not to the enclosing scope.
In fact, the WP is not clear, and is probably inconsistent
about all these cases. It isn't clear which scope
such a declared variable is in. Consider:
if(int i=0)int i; //#1 ambiguous wording in the WP
if(int i=0){ int i; } //#2 ambiguous wording in the WP
if(int i=0){{int i; }} // definitely legal
The WP mumbles incomprehensibly about not being allowed to
redeclare the control variable in the outer scope of the
controlled statement. This is not supported by the grammar:
if-statement:
"if" "(" cond ")" statement
in which statement can be a compound statement. By this grammar
#2 cannot be sensibly disallowed. Instead one would need
an extra production:
"if" "(" cond ")" "{" statement-list "}"
in which the {} extend the scope of the condition rather
than introducing a new scope NESTED in the scope of the
condition.
--
John Max Skaller voice: 61-2-566-2189
81 Glebe Point Rd fax: 61-2-660-0850
GLEBE NSW 2037 email: maxtal@suphys.physics.oz.au
AUSTRALIA email: skaller@maxtal.com.au
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Scope of variables declared in if/while/for
Author: Michael Cook
Date: Wed, 27 Sep 1995 14:48
Date: Wed, 27 Sep 1995 14:48
26 lines
1691 bytes
1691 bytes
>>>>> "MA" == Matt Austern <matt@godzilla.EECS.Berkeley.EDU> writes:
MA> Just about the only good news is that most programs will break
MA> noisily: it's only when you're already playing dangerous games with
MA> variables of the same name in different scopes that a legal program
MA> that does one thing will turn into a legal program that does something
MA> else.
A quality compiler would offer a warning about code like this:
int i;
// ...
{
for (int i = 0; i < 10; i++)
f(i);
g(i); // warning: which instance of `i' did you mean?
}
because the code has two (entirely different) valid meanings depending on
whether your compiler is up to date.
Michael.
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads