Topic: Scope of variable defined in 'for' statements
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 7 Nov 2001 18:26:43 GMT Raw View
In article <3BE8BB3C.127B9185@wizard.net>, James Kuyper Jr.
<kuyper@wizard.net> writes
>> Is this in accordance with the C++ standard? ...
>
>Yes.
Knowing your level of expertise, I can only assume that you have been
confused by the confusing quoted text. I think the author was writing
about the earlier rule in which the variable was deemed as having the
scope of the enclosing block. But I could be wrong (but what he wrote is
subject to interpretation)
>
>> ... I thought that g++ would let
>> you reuse (re-declare and re-define) variables declared/defined in this way.
>> Am I wrong about that?
>
>You can re-use the variable name, but only as long as you do so in a
>different scope. If that scope is inside the scope of the original
>variable, it would hide the original. However, the reason you can re-use
>the name, is that what you'd be defining is a different variable. You
>can't re-use the variable itself.
The question is simply what is the scope. However it is almost
impossible to declare a second i within the same scope. Let me give an
example which is as close as we can get, which none-the-less I think
only hides the loop control:
for(int i=0; i<10; ++i){
int i =0; // this is a new i that hides the other one
// during each iteration
// do work
// inner i is destroyed
}
//loop control i is destroyed
I am not sure about this one:
for(int i=0; int i =0; ++i){ ... }
I hope that is ill-defined but I am not sure.
Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: Thu, 8 Nov 2001 02:22:12 GMT Raw View
In article <3BE8BB3C.127B9185@wizard.net>,
James Kuyper Jr. <kuyper@wizard.net> wrote:
>Grant P wrote:
>>
>> I am fairly new to C++ and I am reading The Waite Group's "Object-Oriented
>> Programming in C++" Third Edition. On page 76 there is a section titled
>> "Variables Defined in for Statements." The author, Robert Lafore states
>> that, "The loop variable j is defined inside the for statement:
>> for(int j=numb; j>0; j--)
>> This is a common construction in C++. It defines the variable as closely
>> as possible to its point of use in the listing. Variables defined in the
>> loop statement this way are visible from the point of definition onward in
>> the listing (unlike variables defined within a block, which are visible only
>> within the block)."
>>
>> Is this in accordance with the C++ standard? ...
>
>Yes.
This is not ok:
for(int j=numb; j>0; j--)
;
std::cout << j; // error: j no longer in scope
>> ... I thought that g++ would let
>> you reuse (re-declare and re-define) variables declared/defined in this way.
>> Am I wrong about that?
>
>You can re-use the variable name, but only as long as you do so in a
>different scope. If that scope is inside the scope of the original
>variable, it would hide the original. However, the reason you can re-use
>the name, is that what you'd be defining is a different variable. You
>can't re-use the variable itself.
IOWs, this is ok (at least as far as j being in scope is concerned)
for(int j=numb; j>0; j--)
;
for(int j=numb; j>0; j--)
;
But this is not:
for(int j=numb; j>0; j--)
for(int j=numb; j>0; j--)
;
--
Greg Comeau export ETA: December See our Oct 31st special
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Thu, 8 Nov 2001 02:22:43 GMT Raw View
Francis Glassborow wrote:
>
> In article <3BE8BB3C.127B9185@wizard.net>, James Kuyper Jr.
> <kuyper@wizard.net> writes
> >> Is this in accordance with the C++ standard? ...
> >
> >Yes.
>
> Knowing your level of expertise, I can only assume that you have been
> confused by the confusing quoted text. I think the author was writing
> about the earlier rule in which the variable was deemed as having the
> scope of the enclosing block. But I could be wrong (but what he wrote is
> subject to interpretation)
Actually, I found it so confusing I ignored it. I intended my "yes" to
apply only to the example code, and not to the explanation that went
with it. I should have made that clearer, by quoting the code, and only
the code, in my reply. Other people who read his question understood the
incorrect explanation it contained better than I did, and when I saw
their answers, I got embarrased about mine. I was hoping no one would
notice. :-)
...
> >> ... I thought that g++ would let
> >> you reuse (re-declare and re-define) variables declared/defined in this way.
> >> Am I wrong about that?
> >
> >You can re-use the variable name, but only as long as you do so in a
> >different scope. If that scope is inside the scope of the original
> >variable, it would hide the original. However, the reason you can re-use
> >the name, is that what you'd be defining is a different variable. You
> >can't re-use the variable itself.
>
> The question is simply what is the scope. However it is almost
> impossible to declare a second i within the same scope. Let me give an
> example which is as close as we can get, which none-the-less I think
> only hides the loop control:
>
> for(int i=0; i<10; ++i){
> int i =0; // this is a new i that hides the other one
> // during each iteration
> // do work
> // inner i is destroyed
> }
> //loop control i is destroyed
Yes, that's exactly what I was referring to about an inner scope.
> I am not sure about this one:
>
> for(int i=0; int i =0; ++i){ ... }
>
> I hope that is ill-defined but I am not sure.
It is - 6.5.3p1: "... names declared in the _for-init-statement_ are in
the same declarative region as those declared in the _condition_, ...".
According to 3.3p4, the only possibility is that the two 'i's are
duplicate definitions of the same object, but that makes it a violation
of the one definition rule (3.2).
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: Thu, 8 Nov 2001 16:13:19 GMT Raw View
In article <tuh3vj5vvhm4f1@corp.supernews.com>,
Grant P <ghpaxton@hotmail.com> wrote:
>I am fairly new to C++ and I am reading The Waite Group's "Object-Oriented
>Programming in C++" Third Edition. On page 76 there is a section titled
>"Variables Defined in for Statements." The author, Robert Lafore states
>that, "The loop variable j is defined inside the for statement:
>for(int j=numb; j>0; j--)
> This is a common construction in C++. It defines the variable as closely
>as possible to its point of use in the listing. Variables defined in the
>loop statement this way are visible from the point of definition onward in
>the listing (unlike variables defined within a block, which are visible only
>within the block)."
identifiers declared in a for loop in the example above are no
longer in scope when the for loop is finished.
>Is this in accordance with the C++ standard?
Lafore seems to imply the identifiers are still in scope even
after the for loop. If this is really the context, then that
is incorrect.
Note that pre-standard compilers allowed this, and some even support it
in some fashion, but as far as Standard C++ is concerned to allow it
would be an extension.
>I thought that g++ would let
>you reuse (re-declare and re-define) variables declared/defined in this way.
>Am I wrong about that?
What did it say? Did you run it in strict mode?
(You may want to try http://www.comeaucomputing.com/tryitout too)
--
Greg Comeau export ETA: December See our Oct 31st special
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: Thu, 8 Nov 2001 16:14:30 GMT Raw View
In article <9sco8g$a0s$1@panix3.panix.com>,
Greg Comeau <comeau@comeaucomputing.com> wrote:
>But this is not:
>
> for(int j=numb; j>0; j--)
> for(int j=numb; j>0; j--)
> ;
Responding to myself: I don't know why I posted this was
not allowed, of course it is.
--
Greg Comeau export ETA: December See our Oct 31st special
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Thu, 8 Nov 2001 16:01:44 CST Raw View
In article <3BE9D786.25C4B056@wizard.net>, James Kuyper Jr.
<kuyper@wizard.net> writes
>It is - 6.5.3p1: "... names declared in the _for-init-statement_ are in
>the same declarative region as those declared in the _condition_, ...".
>According to 3.3p4, the only possibility is that the two 'i's are
>duplicate definitions of the same object, but that makes it a violation
>of the one definition rule (3.2).
Thanks for digging that one out. Which leaves the interesting
circumstance that they are in the same declarative region but have
different scopes and lifetimes. While none of us would want to be so
parsimonious with variable names, I still find it odd that:
for(int j=0;int i=1;){
//do something
}
and
for(int j=0; ;){
int i=1;
// do something
}
Are not interchangeable in all circumstances.
Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Grant P" <ghpaxton@hotmail.com>
Date: Wed, 7 Nov 2001 01:46:57 GMT Raw View
I am fairly new to C++ and I am reading The Waite Group's "Object-Oriented
Programming in C++" Third Edition. On page 76 there is a section titled
"Variables Defined in for Statements." The author, Robert Lafore states
that, "The loop variable j is defined inside the for statement:
for(int j=numb; j>0; j--)
This is a common construction in C++. It defines the variable as closely
as possible to its point of use in the listing. Variables defined in the
loop statement this way are visible from the point of definition onward in
the listing (unlike variables defined within a block, which are visible only
within the block)."
Is this in accordance with the C++ standard? I thought that g++ would let
you reuse (re-declare and re-define) variables declared/defined in this way.
Am I wrong about that?
Thanks.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Wed, 7 Nov 2001 10:51:50 GMT Raw View
Grant P wrote:
>
> I am fairly new to C++ and I am reading The Waite Group's "Object-Oriented
> Programming in C++" Third Edition. On page 76 there is a section titled
> "Variables Defined in for Statements." The author, Robert Lafore states
> that, "The loop variable j is defined inside the for statement:
> for(int j=numb; j>0; j--)
> This is a common construction in C++. It defines the variable as closely
> as possible to its point of use in the listing. Variables defined in the
> loop statement this way are visible from the point of definition onward in
> the listing (unlike variables defined within a block, which are visible only
> within the block)."
>
> Is this in accordance with the C++ standard? ...
Yes.
> ... I thought that g++ would let
> you reuse (re-declare and re-define) variables declared/defined in this way.
> Am I wrong about that?
You can re-use the variable name, but only as long as you do so in a
different scope. If that scope is inside the scope of the original
variable, it would hide the original. However, the reason you can re-use
the name, is that what you'd be defining is a different variable. You
can't re-use the variable itself.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Michiel Salters<Michiel.Salters@cmg.nl>
Date: Wed, 7 Nov 2001 10:52:47 GMT Raw View
In article <tuh3vj5vvhm4f1@corp.supernews.com>, Grant P says...
>
>I am fairly new to C++ and I am reading The Waite Group's "Object-Oriented
>Programming in C++" Third Edition. On page 76 there is a section titled
>"Variables Defined in for Statements." The author, Robert Lafore states
>that, "The loop variable j is defined inside the for statement:
>for(int j=numb; j>0; j--)
> This is a common construction in C++. It defines the variable as closely
>as possible to its point of use in the listing. Variables defined in the
>loop statement this way are visible from the point of definition onward in
>the listing (unlike variables defined within a block, which are visible only
>within the block)."
>
>Is this in accordance with the C++ standard? I thought that g++ would let
>you reuse (re-declare and re-define) variables declared/defined in this way.
>Am I wrong about that?
The statement is nonsense. It suggests that a variable in a for-loop within
one function would be visible in other functions further down the listing !
The C++ scope-rule is actually quite simple : variables defined IN a for loop
are only visible IN that for loop. You can write
for (int i=0; i!=10; ++i ) ;
for (int i=0; i!=10; ++i ) ;
and the second definition defines a second integer unrelated to the first.
W.r.t. g++, it has an extension such that if you use a name of a variable
which is defined in a for loop outside of that for-loop, and the name is
not defined otherwise in that outer scope, the name does refer to the
(out of scope) variable. This results in a warning, not an error.
This is not standard C++, but some unportable code relies on it.
HTH,
--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel.Salters@cmg.nl
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Wed, 7 Nov 2001 10:52:32 GMT Raw View
"Grant P" <ghpaxton@hotmail.com> wrote in message
news:tuh3vj5vvhm4f1@corp.supernews.com...
> I am fairly new to C++ and I am reading The Waite Group's "Object-Oriented
> Programming in C++" Third Edition. On page 76 there is a section titled
> "Variables Defined in for Statements." The author, Robert Lafore states
> that, "The loop variable j is defined inside the for statement:
> for(int j=numb; j>0; j--)
> This is a common construction in C++. It defines the variable as
closely
> as possible to its point of use in the listing. Variables defined in the
> loop statement this way are visible from the point of definition onward in
> the listing (unlike variables defined within a block, which are visible
only
> within the block)."
>
> Is this in accordance with the C++ standard? I thought that g++ would let
> you reuse (re-declare and re-define) variables declared/defined in this
way.
> Am I wrong about that?
The quoted statement is wrong. The scope of a variable defined in a for
statement is limited to the for statement, as you thought.
Some popular compilers still don't do this, though.
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optical Components Ltd
The opinions expressed in this message are not necessarily those of my
employer
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: =?iso-8859-1?Q?Andr=E9_P=F6nitz?= <poenitz@gmx.de>
Date: Wed, 7 Nov 2001 10:53:02 GMT Raw View
Grant P <ghpaxton@hotmail.com> wrote:
> [...]
> Is this in accordance with the C++ standard?
Yes.
> I thought that g++ would let you reuse (re-declare and re-define)
> variables declared/defined in this way. Am I wrong about that?
I guess if you dig out an old enough version of g++ your assumption would
be right.
In 2.95.2 without extra options you get
d.C:2: name lookup of `i' changed for new ANSI `for' scoping
d.C:1: using obsolete binding at `i'
and no binary.
You could obtain the old non-standard behaviour if you pass the
-fno-for-scope option.
Andre'
--=20
Andr=E9 P=F6nitz .............................................. poenitz@g=
mx.de
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 7 Nov 2001 16:10:22 GMT Raw View
In article <tuh3vj5vvhm4f1@corp.supernews.com>, Grant P
<ghpaxton@hotmail.com> writes
>I am fairly new to C++ and I am reading The Waite Group's "Object-Oriented
>Programming in C++" Third Edition. On page 76 there is a section titled
>"Variables Defined in for Statements." The author, Robert Lafore states
>that, "The loop variable j is defined inside the for statement:
>for(int j=numb; j>0; j--)
> This is a common construction in C++. It defines the variable as closely
>as possible to its point of use in the listing. Variables defined in the
>loop statement this way are visible from the point of definition onward in
>the listing (unlike variables defined within a block, which are visible only
>within the block)."
I have looked at the most recent edition of this book but it seems from
what you quote that the author has not made much attempt to bring the
book up to date. Even at the time he wrote the second edition (1995)
there were better books around but unless he did a radical review of the
contents and presentation the book you have is far behind the idioms and
style of modern C++. From what you quote it has also not been updated to
cover Standard C++
Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]