Scope rules

G

Guest

Hi!
Is the following code really correct according to the C++-standard?

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

i = 0;

return 0;
}

I thougt i would be interpreted as local variable part of the for loop
but Visual Studio .NET 2003 seams to accept the code without
complaining.

/H
 
E

E. Robert Tisdale

Is the following code really correct according to the C++-standard?
> cat main.cpp
int main(int argc, char* argv[]) {
for(unsigned int i = 0; i < 10; ++i) ;

i = 0;

return 0;
}
> g++ -Wall -ansi -pedantic -o main main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:4: error: \
name lookup of ‘i’ changed for new ISO ‘for’ scoping
main.cpp:2: error: using obsolete binding at ‘i’
 
R

Rolf Magnus

Hi!
Is the following code really correct according to the C++-standard?

No, it isn't.
int main()
{
for(unsigned int i = 0; i < 10; ++i)
;

i = 0;

return 0;
}

I thougt i would be interpreted as local variable part of the for loop
but Visual Studio .NET 2003 seams to accept the code without
complaining.

AFAIK, this is a legacy. There is some compiler switch to make the compiler
correctly produce an error message for this code.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top