Topic: Scope of variables defined in C++ for loop


Author: jason@cygnus.com (Jason Merrill)
Date: Fri, 8 Jul 1994 22:18:40 GMT
Raw View
>>>>> Douglas C Schmidt <schmidt@tango.ics.uci.edu> writes:

> I'm curious whether these changes have resulted in any modification to
> the original semantics of the C++ for loop?  In particular, can
> someone please tell me whether the following is now legal?

>  {
>   for (int i = 0; i < 10; i++)
>    // ...

>   for (int i = 0; i < 20; i++)
>    // ...
>  }

Yes.

Jason




Author: schmidt@tango.ics.uci.edu (Douglas C. Schmidt)
Date: 8 Jul 1994 08:52:13 -0700
Raw View
Hi,

 Traditionally, the scope of variables (such as loop counters)
defined in a C++ for loop has extended until the end of the enclosing
block, e.g.:

 {
  for (int i = 0; i < 10; i++)
   // ...

  // Error, redefinition of i
  for (int i = 0; i < 20; i++)
   // ...
 }

However, with the relative recent addition of RTTI into the working
paper of the ANSI/ISO C++ standard draft, it is now possible to define
variables within the expression portion of while, if, and switch,
i.e.:

 if (Derived1 *dp = dynamic_cast <Derived1 *> (bp))
  // use dp
 else if (Derived2 *dp = dynamic_cast <Derived2 *> (bp))
  // use dp

Note that the scope of dp is limited to within the body of the if
statement.

I'm curious whether these changes have resulted in any modification to
the original semantics of the C++ for loop?  In particular, can
someone please tell me whether the following is now legal?

 {
  for (int i = 0; i < 10; i++)
   // ...

  for (int i = 0; i < 20; i++)
   // ...
 }

Thanks!

 Doug
--
Douglas C. Schmidt
Department of Information and Computer Science
University of California, Irvine
Irvine, CA 92717. Work #: (714) 856-4105; FAX #: (714) 856-4056




Author: clamage@taumet.Eng.Sun.COM (Steve Clamage)
Date: 8 Jul 1994 17:34:45 GMT
Raw View
In article 467@tango.ics.uci.edu, schmidt@tango.ics.uci.edu (Douglas C. Schmidt) writes:
> ...
>I'm curious whether these changes have resulted in any modification to
>the original semantics of the C++ for loop?  In particular, can
>someone please tell me whether the following is now legal?
>
> {
>  for (int i = 0; i < 10; i++)
>   // ...
>
>  for (int i = 0; i < 20; i++)
>   // ...
> }

The C++ Committee has voted to change the semantics of defining a
variable in the first statement of a for-loop header. Under the new
rules, the scope of the variable is the header and the controlled
statement. In other words, under the new rule, the above code
is well-formed, and the previously-correct code
 {
  for (int j = 0; ... ; ... ) ... ;
  for (    j = 0; ... ; ... ) ... ;
 }
becomes ill-formed. (The second j is now an undefined identifier.)

I don't know whether this change is supported by any compilers yet.

During the (long) transition period, the safest thing to do is to
write code which always has and always will be valid:
 {
  int j;
  for ( j = 0; ... ; ... ) ... ;
  for ( j = 0; ... ; ... ) ... ;
 }

Another always-valid alternative which I consider too ugly to use:

 { for (int j = 0; ... ; ... ) ... ; }
 { for (int j = 0; ... ; ... ) ... ; }

(Note the extra braces surrounding each entire for-loop.)
---
Steve Clamage, stephen.clamage@eng.sun.com