You can, obviously, put a variable declaration in a for loop:
for (int i = 0; ...
and I've noticed that you can do the same thing in if and switch statements as well:
if ((int i = f()) != 0) ...
switch (int ch = stream.get()) ...
But when I try to do the same thing in a while loop:
while ((int ch = stream.get()) != -1) ...
The compiler (VC++ 9.0) does not like it at all.
Is this compliant behavior? Is there a reason for it?
EDIT: I found I can do this:
while (int ch = stream.get() != -1) ...
but because of precedence rules, that's interpreted as:
while (int ch = (stream.get() != -1)) ...
which is not what I want.