-
December 2nd, 2003, 04:09 PM
#1
What does the C++ standard say?
What does the C++ standard say with regards to the following piece of code. I am attempting to compile someone else's code on two different compilers, and on one it fails. The code is something like this:
Code:
const int iMax = 50;
void SomeFunction()
{
for(int i=0; i<iMax; ++i)
doSomething(i);
for(int i=0; i<iMax; ++i)
doSomethingElse(i);
}
The reason one fails is it says that i is defined twice (in both for statements). So the question I have is does defining a variable within a for loop limit the scope of the variable to the for loop? Or does it effectively define the variable in the scope before the loop? Which compiler is more compliant to the standard in this particular area?
Thanks,
Kevin
-
December 2nd, 2003, 04:15 PM
#2
The standard says that "i" is only valid *inside* the scope of the for loop.
The problem is that some compilers treat:
Code:
for (int i = 0; i < 10; ++i)
as:
Code:
int i;
for (i = 0; i < 10; ++i)
Funny thing, the compile that does NOT issure the warning is correct, in this case. However, don't expect "i" to be valid outside either loop.
Viggy
-
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.
-
December 2nd, 2003, 04:31 PM
#4
Thanks guys! That is the answer I needed.
-
December 2nd, 2003, 07:23 PM
#5
Kevin,
if you need paragraph and verse:
The Standard, 6.5.3 / 3:
If the forinitstatement
is a declaration, the scope of the name(s) declared extends to the end of the forstatement.
[Example:
int i = 42;
int a[10];
for (int i = 0; i < 10; i++)
a[i ] = i;
int j = i; // j = 42
—end example]
-
December 3rd, 2003, 11:31 AM
#6
I didn't need paragraph and verse, but thanks though!
I just wanted to know what the standard required.
- Kevin
-
December 3rd, 2003, 11:54 AM
#7
Originally posted by KevinHall
I didn't need paragraph and verse, but thanks though!
I just wanted to know what the standard required.
- Kevin
Why dont you buy the Standard ?? It is about $280 (2003 version)
You will know alot ofdifferent things..
Fiona
-
December 3rd, 2003, 12:00 PM
#8
Originally posted by hometown
Why dont you buy the Standard ?? It is about $280 (2003 version)
You will know alot ofdifferent things..
Fiona
read the question!
R. Thomas
"Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
"Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18
-
December 3rd, 2003, 12:03 PM
#9
Originally posted by hometown
Why dont you buy the Standard ?? It is about $280 (2003 version)
I can't personally afford it. And right now, the company I work for is financially tight and there are more important things for me to ask to put on the company budget than a copy of the standard.
- Kevin
-
December 3rd, 2003, 12:34 PM
#10
Originally posted by hometown
Why dont you buy the Standard ?? It is about $280 (2003 version)
You will know alot ofdifferent things..
Fiona
I think that is for the printed copy. I think the electronic copy is much less.
-
December 3rd, 2003, 05:14 PM
#11
$18 for the PDF version from the ANSI store. Well worth it.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
December 3rd, 2003, 05:47 PM
#12
Interesting.... ANSI sells "INCITS/ISO/IEC 14882-1998" (1998 C++ standard) for $18 dollars in PDF format.
ANSI also sells "ISO/IEC 14882:2003" for $273.
Is there a new ISO standard for C++? I have not heard anything? If not, then what is this other document for? Are there any changes in this newer document?
But, you are correct Graham, $18 is worth it. Thanks!
-
December 3rd, 2003, 06:36 PM
#13
I guess the 2003 version is revised and some errata are fixed.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
December 3rd, 2003, 10:11 PM
#14
Originally posted by KevinHall
Interesting.... ANSI sells "INCITS/ISO/IEC 14882-1998" (1998 C++ standard) for $18 dollars in PDF format.
ANSI also sells "ISO/IEC 14882:2003" for $273.
Is there a new ISO standard for C++? I have not heard anything? If not, then what is this other document for? Are there any changes in this newer document?
But, you are correct Graham, $18 is worth it. Thanks!
You should tell us where you are getting your information from, since it seems to be very incomplete and misleading. It is probably not accurate, and if it is not accurate, then it is especially important to specify the source.
As far as I know, it is very misleading to mention ANSI in this context. The only possible purpsoe I have heard that ANSI can serve for this is that for Americans, they supposedly can sell the standard for less. However as far as I know, that is a rumor that is not true.
See Techstreet - Product Details: Programming Languages - C++; I think that is current and accurate.
-
December 3rd, 2003, 10:27 PM
#15
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
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
|