Topic: Looping construct with in-the-middle test: do {
Author: alexandre.isoard@gmail.com
Date: Fri, 16 Dec 2016 07:10:35 -0800 (PST)
Raw View
------=_Part_540_1050884919.1481901035661
Content-Type: multipart/alternative;
boundary="----=_Part_541_990259205.1481901035662"
------=_Part_541_990259205.1481901035662
Content-Type: text/plain; charset=UTF-8
Other examples:
Classic for loop:
for (int i = 0; i < n; ++i) {
code();
}
Equivalent to:
do {
int i = 0;
} while (i < n) {
code();
++i;
}
Reverse for loop:
for (int i = n-1; i >= 0; --i) {
code();
}
Equivalent to:
do {
int i = n;
} while (i > 0) {
--i;
code();
}
Reverse for loop with unsigned (I don't know how to write a clean one with
an actual for loop):
do {
unsigned i = n;
} while (i > 0) {
--i;
code();
}
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/7b338bbd-65b7-4038-bbaa-a9bdf41a6fc6%40isocpp.org.
------=_Part_541_990259205.1481901035662
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Other examples:<div><br></div><div>Classic for loop:</div>=
<div><div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 25=
0); border-color: rgb(187, 187, 187); border-style: solid; border-width: 1p=
x; word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subpre=
ttyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">for</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> i </span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #066;" class=3D"style=
d-by-prettify">0</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> i =
</span><span style=3D"color: #660;" class=3D"styled-by-prettify"><</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> n</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">++</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">i</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =
code</span><span style=3D"color: #660;" class=3D"styled-by-prettify">();</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">}</span><font color=
=3D"#000000"></font></div></code></div><br>Equivalent to:</div><div><div cl=
ass=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); border-c=
olor: rgb(187, 187, 187); border-style: solid; border-width: 1px; word-wrap=
: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">do</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify"><br>=C2=A0 </span><span style=3D"color: #008;=
" class=3D"styled-by-prettify">int</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> i </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify"=
>0</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">}</span><font color=3D"=
#000000"><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span>=
<span style=3D"color: #008;" class=3D"styled-by-prettify">while</span><span=
style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify">i </span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify"><</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> n</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 cod=
e</span><span style=3D"color: #660;" class=3D"styled-by-prettify">();</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 </spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">++</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">i</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">}</span></font></div></code></div><br>Reverse f=
or loop:</div><div><div class=3D"prettyprint" style=3D"background-color: rg=
b(250, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; bo=
rder-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div c=
lass=3D"subprettyprint"><font color=3D"#660066"><span style=3D"color: #008;=
" class=3D"styled-by-prettify">for</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">(</span><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">int</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
i </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> n</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">-</span><span style=3D"=
color: #066;" class=3D"styled-by-prettify">1</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> i </span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">>=3D</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #066;" class=3D"styled-by-pret=
tify">0</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">--</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">i</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br>=C2=A0 code</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">();</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></=
span></font></div></code></div><br>Equivalent to:</div><div><div class=3D"p=
rettyprint" style=3D"background-color: rgb(250, 250, 250); border-color: rg=
b(187, 187, 187); border-style: solid; border-width: 1px; word-wrap: break-=
word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><font colo=
r=3D"#660066"><span style=3D"color: #008;" class=3D"styled-by-prettify">do<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 </span><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> i </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> n</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span style=3D"color: #008;" class=3D"styled-by-prettify">while</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">i </span><span style=3D"color: #660;"=
class=3D"styled-by-prettify">></span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #066;" class=3D"style=
d-by-prettify">0</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 </span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">--</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">i</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"><br>=C2=A0 code</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">();</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">}</span></font></div></code></div><br>Reverse for loop=
with unsigned (I don't know how to write a clean one with an actual fo=
r loop):</div><div><div class=3D"prettyprint" style=3D"background-color: rg=
b(250, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; bo=
rder-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div c=
lass=3D"subprettyprint"><div class=3D"subprettyprint">do {</div><div class=
=3D"subprettyprint">=C2=A0 unsigned i =3D n;</div><div class=3D"subprettypr=
int">} while (i > 0) {</div><div class=3D"subprettyprint">=C2=A0 --i;</d=
iv><div class=3D"subprettyprint">=C2=A0 code();</div><div class=3D"subprett=
yprint">}</div></div></code></div></div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/7b338bbd-65b7-4038-bbaa-a9bdf41a6fc6%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/7b338bbd-65b7-4038-bbaa-a9bdf41a6fc6=
%40isocpp.org</a>.<br />
------=_Part_541_990259205.1481901035662--
------=_Part_540_1050884919.1481901035661--
.
Author: Nicol Bolas <jmckesson@gmail.com>
Date: Fri, 16 Dec 2016 07:29:02 -0800 (PST)
Raw View
------=_Part_2065_277997953.1481902142115
Content-Type: multipart/alternative;
boundary="----=_Part_2066_1362600081.1481902142116"
------=_Part_2066_1362600081.1481902142116
Content-Type: text/plain; charset=UTF-8
On Friday, December 16, 2016 at 10:10:35 AM UTC-5, alexandr...@gmail.com
wrote:
>
> Other examples:
>
> Classic for loop:
> for (int i = 0; i < n; ++i) {
> code();
> }
>
> Equivalent to:
> do {
> int i = 0;
> } while (i < n) {
> code();
> ++i;
> }
>
>
No, that's not equivalent. You explicitly stated that the `do` part was
executed first, then once for every loop. That will therefore reinitialize
the `int i` every time through the loop.
So none of these are accurate conversions from `for` to this syntax. And
even if they were... why would you use them instead of just `for`?
Overall, this is a bad idea. You have two blocks with a while in the
middle; by normal C++ rules, that means variables declared in the first
block are outside the scope of the second. Yet you want to access them
inside of the second block. That's like declaring variables in an `if`
block and expecting them to be accessible from the `else` block.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/a0b886c7-e329-4d7e-8cf8-07aef573f56f%40isocpp.org.
------=_Part_2066_1362600081.1481902142116
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><br><br>On Friday, December 16, 2016 at 10:10:35 AM UTC-5,=
alexandr...@gmail.com wrote:<blockquote class=3D"gmail_quote" style=3D"mar=
gin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><=
div dir=3D"ltr">Other examples:<div><br></div><div>Classic for loop:</div><=
div><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,18=
7,187);border-style:solid;border-width:1px;word-wrap:break-word"><code><div=
><span style=3D"color:#008">for</span><span style=3D"color:#000"> </span><s=
pan style=3D"color:#660">(</span><span style=3D"color:#008">int</span><span=
style=3D"color:#000"> i </span><span style=3D"color:#660">=3D</span><span =
style=3D"color:#000"> </span><span style=3D"color:#066">0</span><span style=
=3D"color:#660">;</span><span style=3D"color:#000"> i </span><span style=3D=
"color:#660"><</span><span style=3D"color:#000"> n</span><span style=3D"=
color:#660">;</span><span style=3D"color:#000"> </span><span style=3D"color=
:#660">++</span><span style=3D"color:#000">i</span><span style=3D"color:#66=
0">)</span><span style=3D"color:#000"> </span><span style=3D"color:#660">{<=
/span><span style=3D"color:#000"><br>=C2=A0 code</span><span style=3D"color=
:#660">();</span><span style=3D"color:#000"><br></span><span style=3D"color=
:#660">}</span><font color=3D"#000000"></font></div></code></div><br>Equiva=
lent to:</div><div><div style=3D"background-color:rgb(250,250,250);border-c=
olor:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-w=
ord"><code><div><span style=3D"color:#008">do</span><span style=3D"color:#0=
00"> </span><span style=3D"color:#660">{</span><span style=3D"color:#000"><=
br>=C2=A0 </span><span style=3D"color:#008">int</span><span style=3D"color:=
#000"> i </span><span style=3D"color:#660">=3D</span><span style=3D"color:#=
000"> </span><span style=3D"color:#066">0</span><span style=3D"color:#660">=
;</span><span style=3D"color:#000"><br></span><span style=3D"color:#660">}<=
/span><font color=3D"#000000"><span style=3D"color:#000"> </span><span styl=
e=3D"color:#008">while</span><span style=3D"color:#000"> </span><span style=
=3D"color:#660">(</span><span style=3D"color:#000">i </span><span style=3D"=
color:#660"><</span><span style=3D"color:#000"> n</span><span style=3D"c=
olor:#660">)</span><span style=3D"color:#000"> </span><span style=3D"color:=
#660">{</span><span style=3D"color:#000"><br>=C2=A0 code</span><span style=
=3D"color:#660">();</span><span style=3D"color:#000"><br>=C2=A0 </span><spa=
n style=3D"color:#660">++</span><span style=3D"color:#000">i</span><span st=
yle=3D"color:#660">;</span><span style=3D"color:#000"><br></span><span styl=
e=3D"color:#660">}</span></font></div></code></div><br></div></div></blockq=
uote><div><br>No, that's not equivalent. You explicitly stated that the=
`do` part was executed first, then once for every loop. That will therefor=
e reinitialize the `int i` every time through the loop.<br><br>So none of t=
hese are accurate conversions from `for` to this syntax. And even if they w=
ere... why would you use them instead of just `for`?</div><br>Overall, this=
is a bad idea. You have two blocks with a while in the middle; by normal C=
++ rules, that means variables declared in the first block are outside the =
scope of the second. Yet you want to access them inside of the second block=
.. That's like declaring variables in an `if` block and expecting them t=
o be accessible from the `else` block.<br></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/a0b886c7-e329-4d7e-8cf8-07aef573f56f%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/a0b886c7-e329-4d7e-8cf8-07aef573f56f=
%40isocpp.org</a>.<br />
------=_Part_2066_1362600081.1481902142116--
------=_Part_2065_277997953.1481902142115--
.
Author: Alexandre Isoard <alexandre.isoard@gmail.com>
Date: Fri, 16 Dec 2016 07:52:05 -0800 (PST)
Raw View
------=_Part_533_1206937338.1481903525098
Content-Type: multipart/alternative;
boundary="----=_Part_534_574669351.1481903525098"
------=_Part_534_574669351.1481903525098
Content-Type: text/plain; charset=UTF-8
On Friday, December 16, 2016 at 3:29:02 PM UTC, Nicol Bolas wrote:
>
>
>
> On Friday, December 16, 2016 at 10:10:35 AM UTC-5, alexandr...@gmail.com
> wrote:
>>
>> Other examples:
>>
>> Classic for loop:
>> for (int i = 0; i < n; ++i) {
>> code();
>> }
>>
>> Equivalent to:
>> do {
>> int i = 0;
>> } while (i < n) {
>> code();
>> ++i;
>> }
>>
>>
> No, that's not equivalent. You explicitly stated that the `do` part was
> executed first, then once for every loop. That will therefore reinitialize
> the `int i` every time through the loop.
>
Oops. Indeed, I was thinking "static int" all along and that is just plain
wrong.
Also, the semantic would not match the classical do {} while(); semantic
which is against what I was aiming for...
That is:
do {
bool condition = test();
} while(condition);
Is currently (and probably forever) incorrect code as the condition
variable's life-time do not propagate to the test...
So none of these are accurate conversions from `for` to this syntax. And
> even if they were... why would you use them instead of just `for`?
>
Indeed, they are all wrong, sorry. The only one that would have been
"useful" would be the reverse for loop with unsigned integers.
But anyway, that is all incorrect, meaculpa.
> Overall, this is a bad idea. You have two blocks with a while in the
> middle; by normal C++ rules, that means variables declared in the first
> block are outside the scope of the second. Yet you want to access them
> inside of the second block.
>
That is indeed one of the (many?) nasty behavior.
> That's like declaring variables in an `if` block and expecting them to be
> accessible from the `else` block.
>
That is an exaggerated comparison. For `if` / `else` the semantic does not
require to execute the `if` part when the `else` part is executed, while in
this case it always happen.
But I agree that it would be the only construct that has this unnatural
bleeding. I expected this proposition to be widely rejected for this exact
reason.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/65ecd66b-aee9-422e-8fa9-ed0ea8e873a7%40isocpp.org.
------=_Part_534_574669351.1481903525098
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><br><br>On Friday, December 16, 2016 at 3:29:02 PM UTC, Ni=
col Bolas wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin=
-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"lt=
r"><br><br>On Friday, December 16, 2016 at 10:10:35 AM UTC-5, <a>alexandr..=
..@gmail.com</a> wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0;m=
argin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"l=
tr">Other examples:<div><br></div><div>Classic for loop:</div><div><div sty=
le=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);borde=
r-style:solid;border-width:1px;word-wrap:break-word"><code><div><span style=
=3D"color:#008">for</span><span style=3D"color:#000"> </span><span style=3D=
"color:#660">(</span><span style=3D"color:#008">int</span><span style=3D"co=
lor:#000"> i </span><span style=3D"color:#660">=3D</span><span style=3D"col=
or:#000"> </span><span style=3D"color:#066">0</span><span style=3D"color:#6=
60">;</span><span style=3D"color:#000"> i </span><span style=3D"color:#660"=
><</span><span style=3D"color:#000"> n</span><span style=3D"color:#660">=
;</span><span style=3D"color:#000"> </span><span style=3D"color:#660">++</s=
pan><span style=3D"color:#000">i</span><span style=3D"color:#660">)</span><=
span style=3D"color:#000"> </span><span style=3D"color:#660">{</span><span =
style=3D"color:#000"><br>=C2=A0 code</span><span style=3D"color:#660">();</=
span><span style=3D"color:#000"><br></span><span style=3D"color:#660">}</sp=
an><font color=3D"#000000"></font></div></code></div><br>Equivalent to:</di=
v><div><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187=
,187,187);border-style:solid;border-width:1px;word-wrap:break-word"><code><=
div><span style=3D"color:#008">do</span><span style=3D"color:#000"> </span>=
<span style=3D"color:#660">{</span><span style=3D"color:#000"><br>=C2=A0 </=
span><span style=3D"color:#008">int</span><span style=3D"color:#000"> i </s=
pan><span style=3D"color:#660">=3D</span><span style=3D"color:#000"> </span=
><span style=3D"color:#066">0</span><span style=3D"color:#660">;</span><spa=
n style=3D"color:#000"><br></span><span style=3D"color:#660">}</span><font =
color=3D"#000000"><span style=3D"color:#000"> </span><span style=3D"color:#=
008">while</span><span style=3D"color:#000"> </span><span style=3D"color:#6=
60">(</span><span style=3D"color:#000">i </span><span style=3D"color:#660">=
<</span><span style=3D"color:#000"> n</span><span style=3D"color:#660">)=
</span><span style=3D"color:#000"> </span><span style=3D"color:#660">{</spa=
n><span style=3D"color:#000"><br>=C2=A0 code</span><span style=3D"color:#66=
0">();</span><span style=3D"color:#000"><br>=C2=A0 </span><span style=3D"co=
lor:#660">++</span><span style=3D"color:#000">i</span><span style=3D"color:=
#660">;</span><span style=3D"color:#000"><br></span><span style=3D"color:#6=
60">}</span></font></div></code></div><br></div></div></blockquote><div><br=
>No, that's not equivalent. You explicitly stated that the `do` part wa=
s executed first, then once for every loop. That will therefore reinitializ=
e the `int i` every time through the loop.<br></div></div></blockquote><div=
>Oops. Indeed, I was thinking "static int" all along and that is =
just plain wrong.</div><div><br></div><div>Also, the semantic would not mat=
ch the classical do {} while(); semantic which is against what I was aiming=
for...</div><div><br></div><div>That is:</div><div class=3D"prettyprint" s=
tyle=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 187, 1=
87); border-style: solid; border-width: 1px; word-wrap: break-word;"><code =
class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #=
008;" class=3D"styled-by-prettify">do</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br>=C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">bool</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> condition </span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> t=
est</span><span style=3D"color: #660;" class=3D"styled-by-prettify">();</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">while</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">condition</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">);</span></div></code></div><div><br></div><div>Is =
currently (and probably forever) incorrect code as the condition variable&#=
39;s life-time do not propagate to the test...</div><div><br></div><blockqu=
ote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left=
: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div>So none of these=
are accurate conversions from `for` to this syntax. And even if they were.=
... why would you use them instead of just `for`?</div></div></blockquote><d=
iv>Indeed, they are all wrong, sorry. The only one that would have been &qu=
ot;useful" would be the reverse for loop with unsigned integers.</div>=
<div>But anyway, that is all incorrect, meaculpa.</div><div>=C2=A0</div><bl=
ockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border=
-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">Overall, this is=
a bad idea. You have two blocks with a while in the middle; by normal C++ =
rules, that means variables declared in the first block are outside the sco=
pe of the second. Yet you want to access them inside of the second block.</=
div></blockquote><div>That is indeed one of the (many?) nasty behavior.</di=
v><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0;mar=
gin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D=
"ltr">That's like declaring variables in an `if` block and expecting th=
em to be accessible from the `else` block.<br></div></blockquote><div>That =
is an exaggerated comparison. For `if` / `else` the semantic does not requi=
re to execute the `if` part when the `else` part is executed, while in this=
case it always happen.</div><div>But I agree that it would be the only con=
struct that has this unnatural bleeding. I expected this proposition to be =
widely rejected for this exact reason.</div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/65ecd66b-aee9-422e-8fa9-ed0ea8e873a7%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/65ecd66b-aee9-422e-8fa9-ed0ea8e873a7=
%40isocpp.org</a>.<br />
------=_Part_534_574669351.1481903525098--
------=_Part_533_1206937338.1481903525098--
.
Author: Matthew Woehlke <mwoehlke.floss@gmail.com>
Date: Fri, 16 Dec 2016 11:21:34 -0500
Raw View
On 2016-12-16 09:49, alexandre.isoard@gmail.com wrote:
> I propose an additional while loop structures that fit the syntax of the
> second form:
> do {
> bool condition = test();
> } while (condition) {
> action();
> }
> [...]
> An other use case:
> std::ostream &join(std::ostream &os, iterator it, iterator end, const std::
> string &sep) {
> if (it != end) {
> os << *it;
> while(it != end) {
> os << sep;
> os << *it;
> }
> }
> return os;
> }
>
> Could be written:
> std::ostream &join(std::ostream &os, iterator it, iterator end, const std::
> string &sep) {
> if (it != end)
> do {
> os << *it;
> } while(it != end) {
> os << sep;
> }
> return os;
> }
....or
if (it != end) {
for ever { // #define ever (;;)
os << *it;
if (it == end) break;
os << sep;
}
}
If we express your proposal as:
MY_DO { ... } MY_WHILE(expr) { ... }
....then we can replace `MY_DO` with `for(;;)` and `} MY_WHILE(expr) {`
with `if(!(expr)) break;`. That being the case, I fail to see any
significant value to the proposed feature.
--
Matthew
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5854148E.4070901%40gmail.com.
.