About the lifetime/scope of the initializer in

for (int i = 0; i < n; ++i) .....

The lifetime _was_ originally the enclosing scope containing
the for-loop. But it changed. I think this happened
during ANSI standardisation.
Personally I think the change was a mistake.
The only way to be safe about this matter is to
declare variable outside the loop if they are to be
used elsewhere in the enclosing scope.
(When I say "safe", I mean portable.)
This is a real nuisance, because as observed, it makes
for-loops non-modular. You can't portably copy/paste a for-loop
within the same enclosing block.
This means you have to go back to C conventions of
putting the variable declaration at the beginning
of the block, effectively.

But this unsatisfactory situation is unavoidable,
unless you want to permit re-declaration of variables.
You can't have modular local lifetime and also
enclosing-block lifetime.
So ANSI voted, I think, and decided to go for
local for-loop scope, which meant that there
were then two kinds of compilers: pre-ANSI and ANSI.
Therefore if VC++ 6.0 lets the lifetime of a variable
escape outside the for-loop block, it is pre-ANSI!
I.e. it is several years behind the standards.

But then again, the official palmpilot system header files
have nested comments, which is an equally serious matter.
I wish people would take portability more seriously!!!!

Regards,
Alan Kennington.

Reply via email to