Sorry Spellbound, but just because a compiler fails to implement the C++ standard correctly, does not alter the language. acraig was totally correct. Declaring a variable in the for statement causes the variable''s scope to be restricted to that for loop. This is clearly stated in section 6.5.3, paragraph 3 of the standard:
If the for-init-statement is a declaration, the scope of the name(s) declared extends to the end of the for-statement.
-Brian
for loops- quick question! =)
Too bad it doesn''t work that way in VC++. It would certainly make my life easier, as I always make the mistake to declare the same variable twice. =)
Yeah, actually, it was implemented correctly in Borland C++Builder 3, with the variable only being within scope of the for loop. But I still think it''s important to mention the fact that it will be outside the for loop''s scope in VC++, because even though a compiler implementing the language wrong (according to the standard) doesn''t alter the standard, we still have to deal with the way the compiler implements it. Just because the standard says something, doesn''t mean that''s the way it''s gonna work (even though it should).
(Please don''t flame me for this, I think I''m only being reasonable)
------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge
(Please don''t flame me for this, I think I''m only being reasonable)
------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge
Indeed. I just don''t want people thinking that the CORRECT way is such-and-such because of the compiler. Similarly, writing code like:
#include {iostream}
int main()
{
cout << "Hello world!" << endl;
return 0;
}
and having it compile under g++ is no reason to think that it''s legal C++. We must obviously be aware of the pitfalls of any implementation we use, and also be aware of what is right, according to the language, so we can easily move to other compilers and future versions of our compiler.
-Brian
#include {iostream}
int main()
{
cout << "Hello world!" << endl;
return 0;
}
and having it compile under g++ is no reason to think that it''s legal C++. We must obviously be aware of the pitfalls of any implementation we use, and also be aware of what is right, according to the language, so we can easily move to other compilers and future versions of our compiler.
-Brian
Cool. I totally agree with you on that. It is sort of a shame that Microsoft of all companies doesn''t take the standard correctly...
It''s not totally their fault. The for loop scope thing is especially tricky, because it USED to be the other way, and during the standards revision process, it was changed. Since all the compilers were constantly trying to adjust to meet the (ever changing) draft standard, many implemented the old scheme, and people used it. If the compiler didn''t accept it in some way, a lot of code would break. It''s really just a symptom of the VERY long and agonizing standards process that C++ went through. I''m not sure there is a single compiler, ANYWHERE, that implements the entire standard correctly right now. (Although certain commercial compilers come close -- Comeau''s for example.)
-Brian
-Brian
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement