Topic: for' scoping problem
Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/12/16 Raw View
References: <348f14cf.11466524@engnews1.eng>
Ryszard Kabatek <kabatek@chemie.uni-halle.de> asked:
>> I have got the german issue of the Stroustrup's
>> "The C++ programming language" 3rd ed.
>> I found in =A7B.3.6:=20
>> void f(vector<char>& v, int m)
>> {
>> for (int i = 0; i <v.size() && i <= m; ++i) cout << v[i];
>> if (i == m) { // Failure: i used after the end of for loop
>> //...
>> }
>> }
>>
>> The gcc, version 2.7.2, said:
>> test.C:16: warning: name lookup of `i' changed for new ANSI `for'
>> scoping
>>
>> Did it changed once more again?
stephen.clamage_nospam@eng.sun.com (Steve Clamage) responded:
> No. The original scoping rule put the declared variable in the same
> scope as the for-loop header. (Actually, the exact scope was not
> well-defined, which is why some sort of fix was required.) A few years
> ago the draft standard changed the scope to be that of the controlled
> statement. The rule has not changed since then.
>
> Compilers vary in how they treat the changed scoping rule. Some
> default to the old rule and provide an option to use the new rule.
> Some default to the new rule and provide an option to use the new
> rule.
If I understand this correctly, the declared variable is visible inside
the controlled loop statement, but not within the loop 'header'. That
implies that the statement:
for (int i; ...etc...) // 'i' is not visible here
func(i); // 'i' is visible here
is equivalent to:
{
int i;
for ( ; ...etc...)
func(i);
}
or perhaps:
for ( ; ...etc...)
{
int i;
func(i);
}
If 'i' can't be used within the '...etc...' part of the loop header,
what good is it? How is it better than just using normal auto variables,
like we did before in C?
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "James Russell Kuyper Jr." <kuyper@wizard.net>
Date: 1997/12/17 Raw View
David R Tribble wrote:
...
> stephen.clamage_nospam@eng.sun.com (Steve Clamage) responded:
> > No. The original scoping rule put the declared variable in the same
> > scope as the for-loop header. (Actually, the exact scope was not
> > well-defined, which is why some sort of fix was required.) A few years
> > ago the draft standard changed the scope to be that of the controlled
> > statement. The rule has not changed since then.
My copy of the draft standard has, in section 6.5.3 "The for statement":
| 1 The for statement
| for ( for-init-statement conditionopt ; expressionopt )
statement
| is equivalent to
|
| {
| for-init-statement
| while ( condition ) {
| statement
| expression ;
| }
| }
Thus, the potential scope of variables defined in the for-init-statement
includes both the condition and the expression, not just the statement.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: sorry@but.spammed.out (Howard Hinnant)
Date: 1997/12/17 Raw View
In article <2.2.32.19971216010402.008f6bac@central.beasys.com>, David R
Tribble <david.tribble@central.beasys.com> wrote:
> If I understand this correctly, the declared variable is visible inside
> the controlled loop statement, but not within the loop 'header'. That
> implies that the statement:
> for (int i; ...etc...) // 'i' is not visible here
> func(i); // 'i' is visible here
No, i is visible in the "for header".
for (int i = 0; i < 10; ++i)
func(i);
is equivalent to
{
int i;
for (i = 0; i < 10; ++i)
func(i);
}
Note braces for restricing scope of i.
-Howard
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: marty Holcomb <mholcomb@BIX.com>
Date: 1997/12/17 Raw View
David R Tribble wrote:
>If I understand this correctly, the declared variable is visible inside
>the controlled loop statement, but not within the loop 'header'. That
>implies that the statement:
> for (int i; ...etc...) // 'i' is not visible here
> func(i); // 'i' is visible here
>
I believer you are incorrect here....such that
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
should work just fine.
Marty
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: stephen.clamage_nospam@eng.Sun.COM (Steve Clamage)
Date: 1997/12/17 Raw View
On 16 Dec 1997 17:15:45 PST, David R Tribble
<david.tribble@central.beasys.com> wrote:
>stephen.clamage_nospam@eng.sun.com (Steve Clamage) responded:
>> No. The original scoping rule put the declared variable in the same
>> scope as the for-loop header. (Actually, the exact scope was not
>> well-defined, which is why some sort of fix was required.) A few years
>> ago the draft standard changed the scope to be that of the controlled
>> statement. The rule has not changed since then.
>
>If I understand this correctly, the declared variable is visible inside
>the controlled loop statement, but not within the loop 'header'.
No. The scope of the variable extends from the point of its definition
until the end of the controlled statement. See section 6.5.3 "The for
statement" for a complete explanation.
---
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Abhay Kanhere <abhay@hp11.cpdc.ece.nwu.edu>
Date: 1997/12/17 Raw View
David R Tribble <david.tribble@central.beasys.com> writes:
> for (int i; ...etc...) // 'i' is not visible here
> func(i); // 'i' is visible here
First comment is erroneous...
> is equivalent to:
> {
> int i;
> for ( ; ...etc...)
> func(i);
> }
Yes.
'i' is ALSO visible in the
<for-init-statement>
<conditionopt> and
<expressionopt>
of the 'for' loop.
<REFERENCE 6.5.3 :>
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]
</REFERENCE>
Note j= 42 (value of i in outer scope) and not 10 (inner scope i ).
> or perhaps:
> for ( ; ...etc...)
> {
> int i;
> func(i);
> }
>
No.
with this, for(int i=0;i<n;i++)
func(i);
would not work...
> If 'i' can't be used within the '...etc...' part of the loop header,
See above REFERENCE.
-Abhay
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/12/18 Raw View
References: <349a0d1b.5429724@engnews1.eng>
David R Tribble <david.tribble@central.beasys.com> (I) wrote:
>
>> stephen.clamage_nospam@eng.sun.com (Steve Clamage) responded:
>> > No. The original scoping rule put the declared variable in the same
>> > scope as the for-loop header. (Actually, the exact scope was not
>> > well-defined, which is why some sort of fix was required.) A few years
>> > ago the draft standard changed the scope to be that of the controlled
>> > statement. The rule has not changed since then.
>>
>> If I understand this correctly, the declared variable is visible inside
>> the controlled loop statement, but not within the loop 'header'.
stephen.clamage_nospam@eng.Sun.COM (Steve Clamage) responded:
> No. The scope of the variable extends from the point of its definition
> until the end of the controlled statement. See section 6.5.3 "The for
> statement" for a complete explanation.
Your original post wasn't clear, hence my misunderstanding. Your statement
(paraphrased) that "the standard changed the scope of the declared variable
from that of the for-loop header to that of the controlled statement"
was a bit confusing (e.g., does the scope of the controlled statement
include the for-loop header expressions?). I assume that the committee
merely tightened up the existing definition a bit.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: stanliao@synopsys.com (Stan Y. Liao)
Date: 1997/12/11 Raw View
In article <348E880B.12DE@chemie.uni-halle.de> Ryszard Kabatek
<kabatek@chemie.uni-halle.de> writes:
>
> void f(vector<char>& v, int m)
> {
> for (int i =3D 0; i <v.size() && i<=3Dm; ++i) cout << v[i];
> if (i =3D=3D m) { // Failure: i used after the end of for loop
> //...
> }
> }
>
> The gcc, version 2.7.2, said:
>
> test.C: In function `void f(class vector<char> &, int)':
> test.C:16: warning: name lookup of `i' changed for new ANSI `for'
> scoping
This is one of those annoying changes, but I think the original rule
was ill-conceived, so the change is welcome even if it causes minor
pains in the short term.
gcc apparently recognizes the new ANSI scoping rule, but allows you to
compile old programs written before the change. Thus it issues the
warning. Stick to the new scoping rule; it makes more sense.
Regards,
Stan
---
[ 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: stephen.clamage_nospam@eng.sun.com (Steve Clamage)
Date: 1997/12/11 Raw View
On 10 Dec 97 13:21:28 GMT, Ryszard Kabatek
<kabatek@chemie.uni-halle.de> wrote:
>I have got the german issue of the Stroustrup's
>"The C++ programming language" 3rd ed.
>I found in =A7B.3.6:=20
>void f(vector<char>& v, int m)
>{
> for (int i =3D 0; i <v.size() && i<=3Dm; ++i) cout << v[i];
> if (i =3D=3D m) { // Failure: i used after the end of for loop
> //...
> }
>}
>
>The gcc, version 2.7.2, said:
>
>test.C: In function `void f(class vector<char> &, int)':
>test.C:16: warning: name lookup of `i' changed for new ANSI `for'
>scoping
>=20
>Did it changed once more again?
No. The original scoping rule put the declared variable in the same
scope as the for-loop header. (Actually, the exact scope was not
well-defined, which is why some sort of fix was required.) A few years
ago the draft standard changed the scope to be that of the controlled
statement. The rule has not changed since then.
Compilers vary in how they treat the changed scoping rule. Some
default to the old rule and provide an option to use the new rule.
Some default to the new rule and provide an option to use the new
rule.
---
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ 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: marty Holcomb <mholcomb@BIX.com>
Date: 1997/12/13 Raw View
Ryszard Kabatek wrote:
>Did it changed once more again?
>
No, I believe they are saying the same thing...you cannot use the 'i' after
the end of the for loop. I know when I was first looking into C++, it was
the 'old' way, and this change has caused me problems, but it has not
changed last time I checked.
Marty
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Ryszard Kabatek <kabatek@chemie.uni-halle.de>
Date: 1997/12/10 Raw View
Hi,
I have got the german issue of the Stroustrup's
"The C++ programming language" 3rd ed.
I found in =A7B.3.6: =
void f(vector<char>& v, int m)
{
for (int i =3D 0; i <v.size() && i<=3Dm; ++i) cout << v[i];
if (i =3D=3D m) { // Failure: i used after the end of for loop
//...
}
}
The gcc, version 2.7.2, said:
test.C: In function `void f(class vector<char> &, int)':
test.C:16: warning: name lookup of `i' changed for new ANSI `for'
scoping
=
Did it changed once more again?
-- =
Ryszard Kabatek ,,,
(o o)
---------------o00--(_)--00o---------------
---
[ 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: Pete Becker <petebecker@acm.org>
Date: 1997/12/10 Raw View
Ryszard Kabatek wrote:
>=20
> Hi,
> I have got the german issue of the Stroustrup's
> "The C++ programming language" 3rd ed.
> I found in =A7B.3.6:
> void f(vector<char>& v, int m)
> {
> for (int i =3D 0; i <v.size() && i<=3Dm; ++i) cout << v[i];
> if (i =3D=3D m) { // Failure: i used after the end of for loop
> //...
> }
> }
>=20
> The gcc, version 2.7.2, said:
>=20
> test.C: In function `void f(class vector<char> &, int)':
> test.C:16: warning: name lookup of `i' changed for new ANSI `for'
> scoping
>=20
> Did it changed once more again?
No. It changed once, several years ago. The example is correct: 'i' is
not in scope at the point of the if statement. That's a change from the
original rule, which was that 'i' was in scope until the end of the
block in which the for statement occurred.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]