Topic: scope of variables declared in e.g. 'f


Author: stephen.clamage@Eng (Steve Clamage)
Date: 1997/01/03
Raw View
In article l8d@panix.com, comeau@panix.com (Greg Comeau) writes:
>In article <5abicd$jdu@erawan.cognex.com> Michael R Cook <mcook@cognex.com> writes:
>>...  Apparently there was some ambiguity
>>about the scope of `i' at one point, but the latest draft of the standard is
>>unambiguous:
>>
>>|  6.5.3  The for statement                                    [stmt.for]
>>|...
>>|3 If  the  for-init-statement is a declaration, the scope of the name(s)
>>|  declared extends to the end of the for-statement.  [Example:
>>|          int i = 42;
>>|          int a[10];
>>|
>>|          for (int i = 0; i < 10; i++)
>>|                  a[i] = i;
>>|
>>|          int j = i;        // j = 42
>>|   --end example]
>
>Although the [draft] spec has been changed, it was never ambiguous on
>this issue, nor was the identifier.  I think it is important to clarify
>this misconception.  It was always quite clear.  The issue was always
>one of inconsistency as well as a "simple" matter of programming style.

It is true that this example code was never underspecified or ambiguous,
but there were some cases unspecified in the ARM. Example:

 for( int i=0; i < m; ++i ) // no curly braces
  for( int j=0; j < n; ++j )
   a[i, j] = 0;
 cout << j;   // what happens here?

The ARM didn't specify the scope of j, and compilers varied in how
they handled the scope. One might expect that under the ARM rules
the scope of j is the same as the scope of i, but if i>=m the
statement that allocates j never gets executed. There didn't seem to
be any good way to preserve the ARM rules and also specify the
scope of j in a way that made sense.

The new rules specify unambiguously the scope of j, and in addition
eliminate the confusion of things like
 for( int k= 0; ... )  // define k here
  ...
 for( k = 0; ... )     // don't define k here
  ...

If you want the scope of k to extend beyond the controlled statement,
you define k outside the for statement.
 int k=0;
 for( k; ... ) ...

---
Steve Clamage, stephen.clamage@eng.sun.com




[ 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: comeau@panix.com (Greg Comeau)
Date: 1997/01/03
Raw View
In article <199701031835.KAA01122@taumet.eng.sun.com> stephen.clamage@Eng (Steve Clamage) writes:
>In article l8d@panix.com, comeau@panix.com (Greg Comeau) writes:
>>In article <5abicd$jdu@erawan.cognex.com> Michael R Cook <mcook@cognex.com> writes:
>>>...  Apparently there was some ambiguity
>>>about the scope of `i' at one point, but the latest draft of the standard is
>>>unambiguous:
>>>
>>>|  6.5.3  The for statement                                    [stmt.for]
>>>|...
>>>|3 If  the  for-init-statement is a declaration, the scope of the name(s)
>>>|  declared extends to the end of the for-statement.  [Example:
>>>|          int i = 42;
>>>|          int a[10];
>>>|
>>>|          for (int i = 0; i < 10; i++)
>>>|                  a[i] = i;
>>>|
>>>|          int j = i;        // j = 42
>>>|   --end example]
>>
>>Although the [draft] spec has been changed, it was never ambiguous on
>>this issue, nor was the identifier.  I think it is important to clarify
>>this misconception.  It was always quite clear.  The issue was always
>>one of inconsistency as well as a "simple" matter of programming style.
>
>It is true that this example code was never underspecified or ambiguous,
>but there were some cases unspecified in the ARM. Example:
>
> for( int i=0; i < m; ++i ) // no curly braces
>  for( int j=0; j < n; ++j )
>   a[i, j] = 0;
> cout << j;   // what happens here?
>
>The ARM didn't specify the scope of j

I seem to recall the ARM had a rewrite rule as well as some wording
which said "what happens here" is an error even though no curly braces.
OTOH, I do not recall if this meant j was not in scope, just that it
was an error.  OTOOH, it's been so long who knows!! ;-)

>The new rules specify unambiguously the scope of j, and in addition
>eliminate the confusion of things like
> for( int k= 0; ... )  // define k here
>  ...
> for( k = 0; ... )     // don't define k here
>  ...
>
>If you want the scope of k to extend beyond the controlled statement,
>you define k outside the for statement.
> int k=0;
> for( k; ... ) ...

Certainly they are more detailed.

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
               Producers of Comeau C++ 4.0 front-end pre-release
****WEB: http://www.comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
 Here:comeau@comeaucomputing.com / BIX:comeau or comeau@bix.com / CIS:72331,3421


[ 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                             ]