For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > for(;;) scope









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author for(;;) scope
Gustavo Fernández

2005-11-24, 7:59 am

Hi everyone!

Revising some code I've found this:

for(int i=0; i<m_aArray.GetSize(); i++)
{
...
if(bCondition)
break;
}

if( i==m_aArray.GetSize())
{
// The condition was not satisfied so do whatever
...
}

That caused an ASSERT(HowCanItBe) into my brain. If "i" is declared into the
"for", how is possible to use it later? The code compile right, so Was I
wrong thinking that "i" _is_declared_ into the "for"?
Then, talking with the partners at work someone pointed out that in VS you
can do it, but in C++Builder it wouldn't compile.
The question is
What is the standard? And how are declared that kinds of variables?

Thank you in advance.

Regards.


dirk

2005-11-24, 7:59 am

http://msdn.microsoft.com/library/d...fzcforscope.asp

"Gustavo Fernández" <gustavofdez@visualchart.com> wrote in message
news:OfsFdEO8FHA.1188@TK2MSFTNGP12.phx.gbl...
> Hi everyone!
>
> Revising some code I've found this:
>
> for(int i=0; i<m_aArray.GetSize(); i++)
> {
> ...
> if(bCondition)
> break;
> }
>
> if( i==m_aArray.GetSize())
> {
> // The condition was not satisfied so do whatever
> ...
> }
>
> That caused an ASSERT(HowCanItBe) into my brain. If "i" is declared into

the
> "for", how is possible to use it later? The code compile right, so Was I
> wrong thinking that "i" _is_declared_ into the "for"?
> Then, talking with the partners at work someone pointed out that in VS you
> can do it, but in C++Builder it wouldn't compile.
> The question is
> What is the standard? And how are declared that kinds of variables?
>
> Thank you in advance.
>
> Regards.
>
>


Carl Daniel [VC++ MVP]

2005-11-24, 7:01 pm

Gustavo Fernández wrote:
> Hi everyone!
>
> Revising some code I've found this:
>
> for(int i=0; i<m_aArray.GetSize(); i++)
> {
> ...
> if(bCondition)
> break;
> }
>
> if( i==m_aArray.GetSize())
> {
> // The condition was not satisfied so do whatever
> ...
> }
>
> That caused an ASSERT(HowCanItBe) into my brain. If "i" is declared
> into the "for", how is possible to use it later? The code compile
> right, so Was I wrong thinking that "i" _is_declared_ into the "for"?
> Then, talking with the partners at work someone pointed out that in
> VS you can do it, but in C++Builder it wouldn't compile.
> The question is
> What is the standard? And how are declared that kinds of variables?


The code shouldn't compile according to the standard. Whether it compiles
with VC++ depends on which version you're using and which command-line
options are present.

VC6 only implemented the pre-standard rules which allowed the above to
compile.

VC7 implemented a hybrid set of rules that would allow the above to compile,
but would also allow code like the following to compile:

for (int i ...)
;

int i = ...

in this code, i is re-declared following a declaration in a for loop. This
was done by "soft injecting" the 'i' declared in the for loop into the
surrounding scope. The 'soft injection' allowed the variable to be found by
variable lookup, but also allowed it to be re-defined.

VC7.1 introduced the /Zc:forScope command-line option. If this option is
present, VC7.1 implemented standard conforming for-loop scoping, while if
this option was not present, VC7.1 implemented the VC7 scoping rules.

VC8 changed the defaults, so standard-conformant for-loop scoping is now the
default. /Zc:-forScope gets the VC7 behavior.

-cd


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com