Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!indetech!schmidt From: schmidt@indetech.com (Douglas C. Schmidt) Newsgroups: comp.lang.c++ Subject: Re: FORever Summary: Breaks old code? Actually only breaks new code Message-ID: <1990Oct10.195031.5018@indetech.com> Date: 10 Oct 90 19:50:31 GMT References: <6303@drutx.ATT.COM> Reply-To: schmidt@indetech.COM (Douglas C. Schmidt) Distribution: comp Organization: Independence Technologies, Inc. Fremont, CA Lines: 88 [ED note: This is posted on behalf of Jim Roskind] ---------------------------------------- In article <1990Sep27.150948.9109@sco.COM> ron@sco.COM (Ron Irvine) writes: ron> BEWARE there is a major problem with C++'s "for" statement. ron> ron> main() { ron> for (int i=10; i<13;) { ron> for (int i=0; i<3; ++i) ron> ; // stuff ron> ++i; ron> } ron> } ron> ron> ... ron> ron> This is a bad joke for a language that is designed for the 90's. ron> The scope of a variable in a "for-init-statement" MUST end ron> at the end of the "for" statement. Lets change the definition of ron> the language NOW before we inflict horrendous pain on C++ programmers. Response From: marc@dumbcat.sf.ca.us (Marco S Hyman) Marco> Marco> Do you really want to break all the existing code that does something like Marco> Marco> for (int i=10; i<13; ++i) { ^^^^^^^^ Marco> if (x[i] == magicValue) { Marco> break; Marco> } Marco> } Marco> if (i >= 13) { Marco> // we didn't find magicValue Marco> } Marco> Marco> Lots of C code uses this. I assume (which will probably get me into ^^^ Marco> trouble) that C++ programmers will want to do the same thing. Marco> Actually, since C code does *not* allow declarations in the middle of a "for" statement, this adjustment does *not* seem to break any "old C code". When C++ programmers "want to do the same thing", I think they would be well advised to clearly locate the declaration of the iteration variable at the scope in which they intend to use the variable (i.e., declare "i" before the "for" statement if the intent is to continue using it after the "for" statement). This approach is actually what C programmers have always been doing :-). I am aware that current semantic rules (re: E&S and cfront) support the odd behavior wherein variables declared in the for statement are "exported" to the enclosing scope. From my view, this would appear to be a "bug" in cfront that has grown to be gospel in E&S. I can only hope that enough programmers will take it upon themselves to avoid use of such arguable semantic decisions, with the belief that the decision *might* some day be changed. If enough programmers take this enlightened view, it may be possible to make the transition, but otherwise C++ will slowly be saddled with more than the heritage and compatibility with C, it must also forever support its initial decisions (accidents?). I personally don't think that any of the little quirks in C++ (this is only one) can, in and of themselves individually, damage the language. I tend to wonder when the summation of the many quirks might actually end up hindering the language and its use/implementation. I agree with Ron Irvine, and I would like to see the for statement limit the scope of declared variables to its own "for statement scope". I figure every little bit of improvement helps the language, but the questions of compatibility haunt me, as the trade-off between good language design and "existing" code are beyond me. -- Jim Roskind- Author of a YACCable C++ grammar Independent Consultant (407)729-4348 or (617)577-9813 x5570 jar@hq.ileaf.com or ...!uunet!leafusa!jar -- ____*_ Douglas C. Schmidt schmidt@indetech.com \ / / Independence Technologies {sun,sharkey,pacbell}!indetech!schmidt \/ / 42705 Lawrence Place FAX: 415 438-2034 \/ Fremont, CA 94538 Voice: 415 438-2023