scope of for loop in VC6

N

Nick Howes

When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of variable
redeclaration. I thought that the i stayed within the scope of the for loop?
The same code on gcc doesn't give this error (not on the default settings at
least). Is VC being fussy, and can I turn this fussiness off?
 
P

Pete C.

Nick said:
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of
variable redeclaration. I thought that the i stayed within the scope
of the for loop? The same code on gcc doesn't give this error (not on
the default settings at least). Is VC being fussy, and can I turn
this fussiness off?

It's a VC6 bug. Download the free VC++ Toolkit 2003 from Microsoft's site,
and use that in place of the VC6 compiler. It comes with the full VC++ 2003
Pro edition optimizing compiler and standard library.

- Pete
 
J

John Harrison

Pete C. said:
It's a VC6 bug.

It's not really a bug, it's a relic of pre-standard C++.
Download the free VC++ Toolkit 2003 from Microsoft's site,
and use that in place of the VC6 compiler. It comes with the full VC++ 2003
Pro edition optimizing compiler and standard library.

If you don't want to switch compilers, an alternative is this macro. Place
it at the beginning of each file.

#define for if (0); else for
 
W

Walter Tross

Nick Howes 2004-06-15 :
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of variable
redeclaration. I thought that the i stayed within the scope of the for loop?
The same code on gcc doesn't give this error (not on the default settings at
least). Is VC being fussy, and can I turn this fussiness off?

You are a bit off-topic here, but anyhow...
I don't know about VC6, but in VC .NET it's an option in project
properties -> C/C++ -> language. I don't know it's precise naming in
English since I have it in Italian, but it should be something like
"impose for cycle scope conformity".
By default it's turned off, which in my opinion is plain wrong.
I wish I knew a way to have it turned on by default for every new project
I start...

Walter Tross
 
N

Nick Howes

Walter Tross said:
Nick Howes 2004-06-15 :


You are a bit off-topic here, but anyhow...
I don't know about VC6, but in VC .NET it's an option in project
properties -> C/C++ -> language. I don't know it's precise naming in
English since I have it in Italian, but it should be something like
"impose for cycle scope conformity".

Sorry about the OT-ness, should've looked up a visual studio newsgroup.
Thanks everyone for your advice anyhow, there's no such option in VC6 but i
will look up that VC++ Toolkit 2003 and have a look.

Cheers
Nick H
 
J

Julie

Walter said:
Nick Howes 2004-06-15 :


You are a bit off-topic here, but anyhow...
<snip>

au contraire --

This is very *much* on topic. Prior to the (pre)standard change to for-loop
scoping rules, variables declared in a for-loop were still in scope outside of
the loop.

This conformity issue, in part, is due to the very *long* time that it took for
the standard to actually be finalized/approved/ratified in the 90's.
 
J

Jerry Coffin

[ ... ]
Sorry about the OT-ness, should've looked up a visual studio newsgroup.
Thanks everyone for your advice anyhow, there's no such option in VC6 but i
will look up that VC++ Toolkit 2003 and have a look.

VC++ 6 has an option (/Za) that enforces the correct scope for
variables defined in the header of a for loop (etc.) The problem is
that when you turn this switch on, it enforces a number of other
rules, and its standard headers (among other things) won't compile
when those rules are enforced -- the result is that even though the
compiler itself has the capability, the remainder of the
implementation renders it unusable.
 
M

Mike Smith

Pete said:
It's a VC6 bug. Download the free VC++ Toolkit 2003 from Microsoft's site,
and use that in place of the VC6 compiler. It comes with the full VC++ 2003
Pro edition optimizing compiler and standard library.

I know we're getting OT here, but when you say "use that in place of the
VC6 compiler" do you mean that the v13 MS C++ compiler is compatible
with Visual Studio 6, i.e. interchangeable with the v12 compiler? I
wasn't aware of that; it would be very interesting to me if true.
 

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

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top