Duthomhas wrote: |
---|
There are long and technical reasons why what you are asking is no where near as simple as you think it is. You are far, far removed from the first person to ever want to do something like that. |
To my understanding, the proposed loop looks like
do loop-body-1; while (init; condition; increment) loop-body-2;
And is supposed to be roughly equivalent to
1 2 3 4 5 6 7 8 9 10
|
{
init;
while (true)
{
{ loop-body-1; }
if (! condition) break;
{ loop-body-2; }
increment;
}
}
|
We could debate whether we need to add or remove braces and what the consequences are, but as you know, this is simple to implement.
It seems this new loop is conceptually the same as an infinite loop with at least one conditional break anywhere inside it. Contrast this with
while (condition) loop-body;, which is equivalent to an infinite loop with a conditional break at the top:
while (true) { if (! condition) break; loop-body; }
And again with
do loop-body; while (condition);, which is equivalent to an infinite loop with a conditional break at the end:
while (true) { loop-body; if (! condition) break(); }
So the proposal does, in a way, fill a gap in the loop variants.
That being said, I would vote strongly against it for a few reasons. Primarily because
init runs before it is written in the code. That implies a surprising exception to the rules for object lifetime.
This is a big deal because C++ programmers manage system resources by controlling object lifetime (that is
Resource Acquisition Is Initialization), so exceptions to the lifetime rules impact the viability of C++ for resource management.
Another reason is that this offers marginal benefit. I think it has no performance effects, wouldn't really speed up development (since we can just make a while loop), and could be a source of confusion and mistakes. I'd have to say "no" to this one.
BTW, In early C, all local variables were required to be declared at the start of the function (why?). Because of that restriction, this inconvenience:
do { int success = frob(foo); /*...*/ } while(success); // doesn't compile
did not exist. Note it doesn't compile even with modern C++, because the second mention of
success is not in scope. I suspect that this inconvenience might have motivated your idea.