Variable Scope

P

PSN

The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of 'i' error ...

main()
{
for (int i=0; i<10; i++)
cout << i << endl;

for (int i=10; i>0; i--)
cout << i << endl;
}

Thanks,
Prakash
 
V

Victor Bazarov

PSN said:
The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of 'i' error ...

main()
{
for (int i=0; i<10; i++)
cout << i << endl;

for (int i=10; i>0; i--)
cout << i << endl;
}

No. That's the rule for variables declared in the 'for' statement.
Before it was standardized that way the language went back and forth
a couple of times briefly, and some compilers (VC++) implemented it
the "wrong" way, and held onto that. VC++ v6 is pre-standard compiler
and perhaps you've got used to the incorrect 'for' variables rule...
It's time to _un-learn_ bad habits. :)

V
 
O

Ondra Holub

PSN napsal:
The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of 'i' error ...

main()
{
for (int i=0; i<10; i++)
cout << i << endl;

for (int i=10; i>0; i--)
cout << i << endl;
}

Thanks,
Prakash

AFAIK it was changed (I do not how many years ago) in standard, so that
'i' is valid only within loop. In some howto for Mozilla (how to write
portable code) was mentioned, that some compilers will not compile your
example. But I think it is correct acording to standard (it is not
redefinition).
 
O

Ondra Holub

PSN napsal:
The following code compiles fine with GCC. Isnt this supposed to be a
redefinition of 'i' error ...

main()
{
for (int i=0; i<10; i++)
cout << i << endl;

for (int i=10; i>0; i--)
cout << i << endl;
}

Thanks,
Prakash

AFAIK it was changed (I do not how many years ago) in standard, so that
'i' is valid only within loop. In some howto for Mozilla (how to write
portable code) was mentioned, that some compilers will not compile your
example. But I think it is correct acording to standard (it is not
redefinition).
 
M

mlimber

The MSVC++ compiler ends up with a redefinition error.

VC6 is not conformant on this point (MSDN has a note on this). The
standard allows it, as do all C++ compilers released in the last decade
or so.

Cheers! --M
 
P

*PaN!*

Victor Bazarov said:
No. That's the rule for variables declared in the 'for' statement.
Before it was standardized that way the language went back and forth
a couple of times briefly, and some compilers (VC++) implemented it
the "wrong" way, and held onto that. VC++ v6 is pre-standard compiler
and perhaps you've got used to the incorrect 'for' variables rule...
It's time to _un-learn_ bad habits. :)

btw, most of the people I know use a workaround for VC++ 6, that is
re-defining for as << if(0);else for >> (it can be easily set as a
project-level compiler switch).
 
V

Victor Bazarov

*PaN!* said:
btw, most of the people I know use a workaround for VC++ 6, that is
re-defining for as << if(0);else for >> (it can be easily set as a
project-level compiler switch).

It's an ugly hack, and is against the Standard as well, BTW. Of
course keeping using VC++ v6 nowadays is a punishable offence (or
it should be :))

V
 
P

Pete Becker

*PaN!* said:
btw, most of the people I know use a workaround for VC++ 6, that is
re-defining for as << if(0);else for >> (it can be easily set as a
project-level compiler switch).

This introduces undefined behavior if you're not extremely careful. In
every situation I've seen where the same name was used for the loop
variable in multiple for loops, the simplest workaround was to hoist the
declaration of the variable into the enclosing block:

int main()
{
for (int i = 0; i < 10; ++i)
;
for (int i = 0; i < 10; ++i)
;
return 0;
}

becomes

int main()
{
int i;
for (i = 0; i < 10; ++i)
;
for (i = 0; i < 10; ++i)
;
return 0;
}

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
J

Jerry Coffin

[ ... ]
VC6 is not conformant on this point (MSDN has a note on this). The
standard allows it, as do all C++ compilers released in the last decade
or so.

It's mostly a technicality, but VC++ 6 _can_ conform in this regard --
if you ask it for its best conformance with the standard (/Za) it gives
variables declared in for loops the correct scope.

As I said, this is mostly a technicality though: when you do this, it
also enforces a couple of other rules that prevent it from compiling
nearly _any_ of its own headers, so the ability is generally useless
even though it's present.
 
P

*PaN!*

This introduces undefined behavior if you're not extremely careful.

I never trusted that solution very much (and it's really ugly), but can't
see how an undefined behaviour can arise (except if someone else is already
redefining for).
In every situation I've seen where the same name was used for the loop
variable in multiple for loops, the simplest workaround was to hoist the
declaration of the variable into the enclosing block:

Indeed, my favourite solution is modifying the code.
int main()
{
for (int i = 0; i < 10; ++i)
;
for (int i = 0; i < 10; ++i)
;
return 0;
}

becomes

int main()
{
int i;
for (i = 0; i < 10; ++i)
;
for (i = 0; i < 10; ++i)
;
return 0;
}

My favourite solution is actually

int main()
{
{
for (int i = 0; i < 10; ++i)
;
}
{
for (int i = 0; i < 10; ++i)
;
}
}

since while refactoring the code it might happen

a) that the two loops have different types for the i variable (e.g. int,
unsigned int)

b) that the two loops are in a longer/complex function

so I found that a simpler/faster solution is to enclose the for-loops in a
block.
 
R

Ron Natalie

No. That's the rule for variables declared in the 'for' statement.
Before it was standardized that way the language went back and forth
a couple of times briefly, and some compilers (VC++) implemented it
the "wrong" way, and held onto that. VC++ v6 is pre-standard compiler
and perhaps you've got used to the incorrect 'for' variables rule...
It's time to _un-learn_ bad habits. :)

We fixed it with a macro in the VC6. Even the contemporaneous
Microsoft doc's claimed it a "Microsoft Extension" before the
standard was published. The problem in VC6 is you can't turn
it off (i.e., get the standard behavior) without disabliing a
lot of other "extensions" that the development environment
relied on.

The later versions of Visual C++ (honestly, VC6 is two major
relases and nearly ten years old now) allow you to fix the
for loops with an option (which I believe is the 'standard'
way by default) distinct from anything else.
 
D

Duane Hebert

The later versions of Visual C++ (honestly, VC6 is two major
relases and nearly ten years old now) allow you to fix the
for loops with an option (which I believe is the 'standard'
way by default) distinct from anything else.

In VC7.1, at least, it's not the standard way by default
but at least you can set it.
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top