🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2035

Re: Scope of variables declared in if/while/for

#2035
From: Matt Austern
Date: Mon, 25 Sep 1995 20:43
46 lines
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. ]
Technical Details
Message-ID: <MATT.95Sep25110939@godzilla.EECS.Berkeley.EDU>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <MATT.95Sep25095657@physics2.Berkeley.EDU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 25 Sep 95 20:43:14 GMT
Approved: fjh@cs.mu.oz.au
From: matt@godzilla.EECS.Berkeley.EDU (Matt Austern)
Newsgroups: comp.std.c++
Subject: Re: Scope of variables declared in if/while/for
X-Original-Date: 25 Sep 1995 18:09:39 GMT
Organization: University of California at Berkeley (computational neuroscience)
Message-ID: <MATT.95Sep25110939@godzilla.EECS.Berkeley.EDU>
References: <MATT.95Sep25095657@physics2.Berkeley.EDU>
Reply-To: matt@physics.berkeley.edu
In-Reply-To: rmartin@rcmcon.com's message of 25 Sep 1995 16:56:57 GMT