On Thu, 25 Aug 2011 12:21:19 -0400, Timon Gehr <timon.g...@gmx.ch> wrote:



I usually replace code like this:

x++;
if(x < 100)
{
   // use x
}

with this:

if(++x < 100) {
     // use x
}

so the logical mapping to while would map this:

x++;
while(x < 100)
{
     // use x.
}

to this:

while(++x < 100) {
     // use x.
}


But it looks like every time through the loop x gets incremented.
That's not what I'd want. At the very least, it's confusing
semantics one way or the other.

If you use the construct in the *init* section of a for loop, it reads
the same as the if statement, since that part is only executed once.

The while loop just doesn't fit the model. Let's ditch the while loop.

Not relevant, you are not declaring a variable in your example. The benefit of using the auto x = y is that you can move the declaration of the variable, which is used only during the loop, into the loop statement.

I have written loops like this:

bool found = false;
while(!found)
{
  // things that might set found to true
}

There is also this construct, which is what you are arguing for:

bool continueloop;
while(continueloop = someComplexFunction())
{
  // use continueloop as rvalue in places
}

So there are legitimate cases for while(auto x = y) being a shortcut for both behaviors, hence the confusion.

I agree your case yields more fruit, since I think the above is actually invalid (can't use = as a boolean expression), but it doesn't stop me from being confused about whether the initialization happens once or every time. It just reads like it happens once to me, even if it happens every loop. That confusion isn't there for if statements or for loops.

-Steve

Reply via email to