Why does this compile correctly?

B

Bob Altman

I upgraded a VS 2003 project to VS 2005, and now it gets a compiler error.
What I don't understand is why the code compiled without problems in VS
2003. The problem code, contained in a .cpp file and compiled as C++ (/TP)
is:

for ( int kk=1;kk<3;kk++ )
if ( abs_x_offset <= x_limit[kk] ) break;

// VS 2005 complains that kk isn't defined
float abs_y_limit = ( ( y_limit[kk] - y_limit[kk-1] )
/ ( x_limit[kk] - x_limit[kk-1] ) )
* ( abs_x_offset - x_limit[kk-1] ) + y_limit[kk-1];

I think that the intent of this code is to perform a linear interpolation by
first finding an index (kk) and then performing a calculation using that
value. But kk isn't defined outside of the "for" statement. (There are no
other instances of "kk" anywhere in the code.)

Is VS 2003 somehow making kk visible outside of the "for" statement?

TIA - Bob
 
D

Doug Harrison [MVP]

I upgraded a VS 2003 project to VS 2005, and now it gets a compiler error.
What I don't understand is why the code compiled without problems in VS
2003. The problem code, contained in a .cpp file and compiled as C++ (/TP)
is:

for ( int kk=1;kk<3;kk++ )
if ( abs_x_offset <= x_limit[kk] ) break;

// VS 2005 complains that kk isn't defined
float abs_y_limit = ( ( y_limit[kk] - y_limit[kk-1] )
/ ( x_limit[kk] - x_limit[kk-1] ) )
* ( abs_x_offset - x_limit[kk-1] ) + y_limit[kk-1];

I think that the intent of this code is to perform a linear interpolation by
first finding an index (kk) and then performing a calculation using that
value. But kk isn't defined outside of the "for" statement. (There are no
other instances of "kk" anywhere in the code.)

Is VS 2003 somehow making kk visible outside of the "for" statement?

Yep. There is an option to defeat the VC2003 pre-standard behavior, I think
it was /Zc:forScope. More recent versions default to standard behavior,
which confines the loop variable to the loop. The solution of course is to
declare kk outside the loop header if you need to use it later on below the
loop, e.g.


int kk = 1;
for ( ; kk < 3; ++kk) ...
 
D

David Wilkinson

Bob said:
I upgraded a VS 2003 project to VS 2005, and now it gets a compiler error.
What I don't understand is why the code compiled without problems in VS
2003. The problem code, contained in a .cpp file and compiled as C++ (/TP)
is:

for ( int kk=1;kk<3;kk++ )
if ( abs_x_offset <= x_limit[kk] ) break;

// VS 2005 complains that kk isn't defined
float abs_y_limit = ( ( y_limit[kk] - y_limit[kk-1] )
/ ( x_limit[kk] - x_limit[kk-1] ) )
* ( abs_x_offset - x_limit[kk-1] ) + y_limit[kk-1];

I think that the intent of this code is to perform a linear interpolation by
first finding an index (kk) and then performing a calculation using that
value. But kk isn't defined outside of the "for" statement. (There are no
other instances of "kk" anywhere in the code.)

Is VS 2003 somehow making kk visible outside of the "for" statement?

Bob:

In VC6 the for loop behavior was flat out wrong -- the scope of the variable
extended beyond the loop.

In VS2003 I think there was an option for the correct behavior, but the default
was some strange hybrid which would compile most VC6 code but paid some lip
service to the standard.

In VS2005 the default behavior is the C++ Standard, though You can get the old
VC6 behavior as a compiler option.
 
B

Ben Voigt [C++ MVP]

Bob:
In VC6 the for loop behavior was flat out wrong -- the scope of the
variable extended beyond the loop.

I think this conforms to the standard that existed when VC6 was released.
Only in C++03 did the scoping rules change.
 
B

Bo Persson

Ben said:
I think this conforms to the standard that existed when VC6 was
released. Only in C++03 did the scoping rules change.

Not quite (but close).

VC6 implemented the rules of the draft standard available at the start
of the project. The language rules were then modified before the
release of the C++98 standard.

VC6 was released slightly earlier the same year, but with MS well
aware of the changes to the then (formally) proposed standard.
Obviously the change came in too late for the compiler project, and
the next compiler release was then delayed several years longer than
expected.

Shit happens!


Bo Persson
 
B

Bo Persson

Bo said:
Not quite (but close).

VC6 implemented the rules of the draft standard available at the
start of the project. The language rules were then modified before
the release of the C++98 standard.

VC6 was released slightly earlier the same year, but with MS well
aware of the changes to the then (formally) proposed standard.
Obviously the change came in too late for the compiler project, and
the next compiler release was then delayed several years longer than
expected.

Shit happens!


Bo Persson

And, BTW, I wonder if the release version of VC10 will implement the
2008 drafts of rvalue references, or the 2009 draft. They are quite
different!


"Ten is the new six" can easily get a new meaning! :)


Bo Persson
 
B

Ben Voigt [C++ MVP]

Bo Persson said:
Not quite (but close).

VC6 implemented the rules of the draft standard available at the start of
the project. The language rules were then modified before the release of
the C++98 standard.

VC6 was released slightly earlier the same year, but with MS well aware of
the changes to the then (formally) proposed standard. Obviously the change
came in too late for the compiler project, and the next compiler release
was then delayed several years longer than expected.

Thanks for providing the corrected dates and standard versions (including
proposals) involved.
 
H

Howard Swope

Standard is a pretty grey word to begin with. Whose standard is it? Is it a
publicly accesible standards body? Are there competeing standards? Who
sponsors the standards body? etc.

Standards evolve, and as soon as you embrace one feature, you will end up
with a bunch of people who love it and a bunch of people who don't. I think
I like the tighter scoping that has become the standard, but how often do I
want to access the current position of iteration after breaking out of it
(maybe 10% of the time). So probably scoping it tighter makes sense.
 
D

Doug Harrison [MVP]

Standard is a pretty grey word to begin with. Whose standard is it? Is it a
publicly accesible standards body? Are there competeing standards? Who
sponsors the standards body? etc.

The only real question when talking about the C++ Standard is the version.
Standards evolve, and as soon as you embrace one feature, you will end up
with a bunch of people who love it and a bunch of people who don't. I think
I like the tighter scoping that has become the standard, but how often do I
want to access the current position of iteration after breaking out of it
(maybe 10% of the time). So probably scoping it tighter makes sense.

The original for-scope rule was a true blunder. It made no sense to
introduce an alternative syntax that added no new functionality to the
language but was instead purely obfuscating. In D&E, Stroustrup explains
that the goal was to make declaration and initialization one statement, but
he regretted not limiting the scope to the loop.
 
D

Doug Harrison [MVP]


That's got absolutely nothing to do with what you posted earlier, and my
comment addressed your claim that the C++ Standard is some sort of nebulous
concept, which it is not. The page you linked to contains the things that
will help determine what the next version of the standard will look like.
However, my main point was that a true expert is a seeker of knowledge.

He's also not some sort of sophist. :)
 
H

Howard Swope

Point taken. I did get a bit off topic.

Doug Harrison said:
That's got absolutely nothing to do with what you posted earlier, and my
comment addressed your claim that the C++ Standard is some sort of
nebulous
concept, which it is not. The page you linked to contains the things that
will help determine what the next version of the standard will look like.


He's also not some sort of sophist. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top