Re: Stupid mwcc compilers ...

Shaw Terwilliger (sterwill@postman.abisource.com)
Wed, 9 Jun 1999 08:45:06 -0500


On Wed, Jun 09, 1999 at 08:37:45AM -0400, Thomas Fletcher wrote:
> Just a little note here to people who are cranking
> through the source making those all important
> feature additions and bug fixes.
>
> While this is perfectly legal C++:
>
> for (int i=0; i<10; i++) {
> printf("I is %d \n", i);
> }
> printf("I is now %d \n", i);

I was always under the impression that one shouldn't reference i
outside that loop block. I don't have a current ANSI/ISO C++
reference, but I do have the December 96 draft (from cygnus):

4 Names declared in the for-init-statement, and in the condition of if,
while, for, and switch statements are local to the if, while, for, or
switch statement (including the controlled statement), and shall not
be redeclared in a subsequent condition of that statement nor in the
outermost block (or, for the if statement, any of the outermost
blocks) of the controlled statement; see _stmt.select_.

But I'm really not sure how I should interpret that. It says "shall
not be redeclared in ... the outermost block", which I read as the
parent block of the conditional. But though it does say I'm not supposed
to use that name at that level, I'm not sure whether it means the existing
one is available there.

_stmt.select_ shows us:

3 A name introduced by a declaration in a condition (either introduced
by the type-specifier-seq or the declarator of the condition) is in
scope from its point of declaration until the end of the substatements
controlled by the condition. If the name is re-declared in the outer-
most block of a substatement controlled by the condition, the declara-
tion that re-declares the name is ill-formed. [Example:
if (int x = f()) {
int x; // ill-formed, redeclaration of 'x'
}
else {
int x; // ill-formed, redeclaration of 'x'
}

--end example]

The example only covers the limits of the outermost block of a
substatement but the rule says the variable is in scope from
its "point of declaration until the end of the substatements."

-- 
Shaw Terwilliger


This archive was generated by hypermail 1.03b2.