Topic: Concerns about N3587 "Loop Exit Strategy".


Author: Mikhail Semenov <mikhailsemenov1957@gmail.com>
Date: Sat, 20 Apr 2013 03:09:42 -0700 (PDT)
Raw View
------=_Part_82_17871250.1366452582630
Content-Type: text/plain; charset=ISO-8859-1

The proposal does not provide clear syntax. The following "ugly" example is
given in N3587:

*something_t last; // Extra construction here*.
*for (auto& i : c)
{
   if (some_condition(i))
   {
        last = i; // Extra copy here
        goto EARLY; *
*   }
   do_something(i);
}
do_stuff();
goto DONE;
EARLY:
do_something_else(last);
DONE:*

It states that new syntax is needed. But it can be easily re-written using
one of the following three better looking forms:
(1)
bool ok = true;
for (auto& i : c)
{
     if (some_condition(i))
     {
          ok = false;
          do_something_else(i); // on early exit
          break;
     }
     do_something(i);
}
if (ok)
{
    do_stuff();
}

(2)
for (auto& i : c)
{
     if (some_condition(i))
     {
            do_something_else(i); // on early exit
            goto EARLY;
     }
     do_something(i);
}
do_stuff();
EARLY:;

The early-exit code does not have to be written outside the loop.
The paper goes ahead with the following proposal of new syntax:
if for (auto& i : c)
{
     if (some_condition(i)) break;
     do_something(i);
}
{
     do_stuff(); // normal termination
}
else
{
     do_something_else(i); // on early exit; the loop parameter *i* is
still available
}

The proposed syntax does not look better than the two pieces of code
written above.
Besides, the syntax may be confusing if the loop and code around it takes
more space: the normal
termination code is not easily recognized after the loop.









--

---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.



------=_Part_82_17871250.1366452582630
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>The proposal does not provide clear syntax. The following "ugly" examp=
le is given in&nbsp;N3587:</div><div>&nbsp;</div><div><font color=3D"#9900f=
f"><font face=3D"courier new,monospace"><strong><font color=3D"#9900ff">som=
ething_t last; // Extra construction here</font></strong>.<br></font></font=
><strong><font color=3D"#9900ff" face=3D"courier new,monospace">for (auto&a=
mp; i : c)<br>{<br>&nbsp;&nbsp;&nbsp;if (some_condition(i))<br>&nbsp;&nbsp;=
 {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; last =3D i; // Extra copy =
here<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; goto EARLY; </font></str=
ong></div><div><strong><font color=3D"#9900ff" face=3D"courier new,monospac=
e">&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;do_something(i);<br>}<br>do_stuff();=
<br>goto DONE;<br>EARLY:<br>do_something_else(last);<br>DONE:</font></stron=
g></div><div>&nbsp;</div><div>It states that new syntax is needed. But it c=
an be easily re-written using one of the following&nbsp;three better lookin=
g&nbsp;forms:</div><div>(1)</div><div><font color=3D"#9900ff" face=3D"couri=
er new,monospace">bool ok =3D true;<br>for (auto&amp; i : c)<br>{<br>&nbsp;=
&nbsp;&nbsp; &nbsp;if (some_condition(i)) <br>&nbsp;&nbsp;&nbsp;&nbsp; {<br=
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ok =3D false;<br>&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do_something_else=
(i); // on early exit<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;do_something(i);<br>}</font></div><div><font color=3D"#9900ff" fa=
ce=3D"courier new,monospace">if (ok)<br>{<br>&nbsp;&nbsp;&nbsp; do_stuff();=
<br>}</font></div><div>&nbsp;</div><div>(2)</div><div></div><font color=3D"=
#9900ff"><div><font face=3D"courier new,monospace">for (auto&amp; i : c)<br=
>{<br>&nbsp;&nbsp;&nbsp;&nbsp; if (some_condition(i)) <br>&nbsp;&nbsp;&nbsp=
;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp; do_something_else(i); // on early exit<br>&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;goto E=
ARLY;<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp; do_somethin=
g(i);<br>}<br>do_stuff();<br>EARLY:;</font></div><div>&nbsp;</div><div><fon=
t color=3D"#000000">The early-exit code does not have to be written outside=
 the loop.</font></div><div><font color=3D"#000000">The paper goes ahead wi=
th the following proposal of new syntax:</font></div><div><font color=3D"#0=
00000"></font><font face=3D"courier new,monospace"><font color=3D"#cc0000">=
if</font> for (auto&amp; i : c)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp; if (some_c=
ondition(i)) break;<br>&nbsp;&nbsp;&nbsp; &nbsp;do_something(i);<br>}<br>{<=
br>&nbsp;&nbsp;&nbsp;&nbsp; do_stuff(); // normal termination<br>}<br>else<=
br>{<br>&nbsp;&nbsp;&nbsp;&nbsp; do_something_else(i); // on early exit; th=
e loop parameter&nbsp;<strong><em>i</em></strong> is still available<br>}</=
font></div><div>&nbsp;</div><div><font color=3D"#000000">The proposed synta=
x does not look better than the two pieces of code written above. </font></=
div><div><font color=3D"#000000">Besides, the syntax may be confusing if th=
e loop and code around it takes more space: the normal</font></div><div><fo=
nt color=3D"#000000">termination code is not easily recognized after the lo=
op.</font></div><div><font color=3D"#000000"></font>&nbsp;</div><div><font =
color=3D"#000000"></font>&nbsp;</div><div><font color=3D"#000000"></font>&n=
bsp;</div><div><br>&nbsp;</div></font><div>&nbsp;</div><div>&nbsp;</div><di=
v>&nbsp;</div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_82_17871250.1366452582630--

.