>> This kind of think comes up frequently, and it's usually because
>> someone's neglected to consider the compiler-generated copy
[quoted text clipped - 7 lines]
> {
> }
Nor should it according to standard C++ grammar.
> Neither will this:
>
> while (bool b)
> {
> }
Nor should it according to standard C++ grammar.
> But this will compile:
>
> while (bool b = true)
> {
> }
It should compile according to the standard. Basically, if you have a
declaration, you need an '=' for it to be legal in a for or while statement.
> I don't know what all this means, but it doesn't look like a bug to me.
It does to me. From the C++ standard:
2 When the condition of a while statement is a declaration, the scope of
the variable that is declared extends from its point of declaration
(3.3.1) to the end of the while statement. A while statement of the form
while (T t = x) statement
is equivalent to
label:
{ //start of condition scope
T t = x;
if (t) {
statement
goto label;
}
} //end of condition scope
The object created in a condition is destroyed and created with each
iteration of the loop. [Example:
struct A {
int val;
A(int i) : val(i) { }
~A() { }
operator bool() { return val != 0; }
};
int i = 1;
while (A a = i) {
//...
i = 0;
}
In the while-loop, the constructor and destructor are each called twice,
once for the condition that succeeds and once for the condition that
fails. ]
Tom