|
-
December 2nd, 2003, 04:16 PM
#3
Re: What does the C++ standard say?
I know this isn't the VC++ forum, but...
MSVC++ doesn't adhere to the standard, but the 7.x versions of the MS compiler offer a switch that you can use to force compliance in this respect. From MSDN:
The C++ standard says that a variable declared in a for loop shall go out of scope after the for loop ends. For example:
Code:
for (int i = 0 ; i < 5 ; i++) {
// do something
}
// i is now out of scope under /Za or /Zc:forScope
By default, under /Ze, a variable declared in a for loop remains in scope until the for loop's enclosing scope ends.
/Zc:forScope enables standard behavior of variables declared in for loops without needing to specify /Za.
It is also possible to use the scoping differences of the for loop to redeclare variables under /Ze as follows:
Code:
// for_statement5.cpp
int main()
{
int i = 0; // hidden by var with same name declared in for loop
for ( int i = 0 ; i < 3; i++ )
{
}
for ( int i = 0 ; i < 3; i++ )
{
}
}
This more closely mimics the standard behavior of a variable declared in a for loop, which requires variables declared in a for loop to go out of scope after the loop is done. When a variable is declared in a for loop, the compiler internally promotes it to a local variable in the for loop's enclosing scope even if there is already a local variable with the same name.
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|