Topic: Scope of object declared within for construct


Author: davisonj@en.ecn.purdue.edu (John M Davison)
Date: 9 Jan 92 17:26:00 GMT
Raw View
        I recall reading in a DECUS publication that it was decided that the
following C++ block

        A b;

        if (condition)
            for (B b; some-condition; some-expr ) stmt;
        else
            for (C b; some-condition; some-expr ) stmt;

should produce legal code because there would be an implied conditional-for
scope produced by the compiler, i.e.

        if (condition)
        {
            for (B b; some-condition; some-expr ) stmt;
        }
        else
        {
            for (C b; some-condition; some-expr ) stmt;
        }

        I am uncomfortable with this notion of the "for" loop, though, because
it will not allow the following code to be legal:

     {
        for (int foo; some-condition; some-expr) stmt;
        ...
        for (double foo; some-condition; some-expr) stmt;
     }

        Could it not be argued that each occurrence of a "for" loop should, for
purposes of cleaner style and more consistent language design, create a scope
specifically for the expressions inside the "for" parentheses?  This would be
consistent with the Pascal idea of having a loop variable undefined outside of
the loop, but only if the loop variable is declared within the parentheses.
That way, the flexibility of being able to use the value of a loop variable
after the termination of a loop would remain, as long as one declared the loop
variable before the occurrence of the loop (which is how it is done in C
anyway).

        In effect, the proposed code would look like this to a compiler:
     {
        {for (int foo; some-condition; some-expr) stmt;}
        ...
        {for (double foo; some-condition; some-expr) stmt;}
     }

        I know that this revision would break some code, but it would be
trivial to fix 99% of the code to comply with the new rule: simply put the
declaration immediately before the "for" keyword.  The above "if" construct
would still work as intended, and things would be simplified in the process.

        Has this idea been discussed and/or rejected before?

John Michael Davison
davisonj@ecn.purdue.edu




Author: ark@alice.att.com (Andrew Koenig)
Date: 11 Jan 92 14:38:36 GMT
Raw View
In article <1992Jan9.172600.12793@en.ecn.purdue.edu> davisonj@en.ecn.purdue.edu (John M Davison) writes:

>         Could it not be argued that each occurrence of a "for" loop should, for
> purposes of cleaner style and more consistent language design, create a scope
> specifically for the expressions inside the "for" parentheses?

Yes.

>         Has this idea been discussed and/or rejected before?

Yes.

The ANSI C++ Committee discussed this in the Core Language Working Group
(which I chair).  We decided that it might well be nice if `for' statements
behaved differently, but that the charter of our working group was to
clarify the definitions of things that were ill-defined in the base
document and not to change things with existing, clear definitions
even if we disliked them.

Indeed, there is no working group in the Committee chartered to change
the base document -- the working groups can add new stuff but changing
things already there requires action from the full Committee.

To date, the full Committee has not taken any action on `for' statements.
--
    --Andrew Koenig
      ark@europa.att.com




Author: jimad@microsoft.com (Jim ADCOCK)
Date: 13 Jan 92 20:54:11 GMT
Raw View
In article <1992Jan9.172600.12793@en.ecn.purdue.edu> davisonj@en.ecn.purdue.edu (John M Davison) writes:
>        Has this idea been discussed and/or rejected before?

Yes.  The general deciding rule in C++ has been that it is better to
break future programs that past programs.  This approach is clearly
compatible with the idea of building an OOPL on top of C.