Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!necntc!ima!haddock!karl From: karl@haddock.ISC.COM (Karl Heuer) Newsgroups: comp.lang.c++ Subject: Re: operator precedence question Keywords: c++, precedence Message-ID: <5341@haddock.ISC.COM> Date: 20 Jul 88 15:31:09 GMT References: <347@gt-eedsp.UUCP> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Distribution: na Organization: Interactive Systems, Boston Lines: 24 In article <347@gt-eedsp.UUCP> baud@gt-eedsp.UUCP (Kurt Baudendistel) writes: > for ( int i=0, t=s; i < 10; i++ ) ... >[intending to perform "t=s" as an assignment, but instead finding it to be a >declaration] Basically, you've been hit by what could be considered a design flaw in the syntax of the "for" statement. In C, where the three components are expressions, there's no ambiguity, and one can do multiple initializations via the comma operator. In C++, the first component is a statement instead of an expression (note that all expressions are statements, but not vice versa); if, as in this case, it's a non-expression statement, then the comma operator is not legal. Neither is a semicolon (normally the statement-equivalent of the comma operator), because of the ambiguity this would create. The appendix to Bjarne's C++ book suggests that for ({int i=0; t=s;} i < 10; ++i) ... should work, but I haven't tested it. An even kludgier approach is to use for (int i=(t=s, 0); i < 10; ++i) ... which should be a legal use of the comma operator. Personally, I disrecommend using a declaration as the first component of a for statement, since it (improperly, in my opinion) extends to the end of the enclosing block instead of being restricted to the for statement itself. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint