Topic: [Thoughts/Idea] Labeled Loop Variable
Author: peetpaul69@gmail.com
Date: Thu, 20 Jul 2017 18:49:29 -0700 (PDT)
Raw View
------=_Part_1076_987460235.1500601769412
Content-Type: multipart/alternative;
boundary="----=_Part_1077_147517153.1500601769413"
------=_Part_1077_147517153.1500601769413
Content-Type: text/plain; charset="UTF-8"
I would like to get some feedback on these quick thoughts.
The aim is to make the for-loop more flexible (by supporting better loop
control-flow) and eventually turning it into an expression.
Also, please excuse my English (It's not my native language).
// Feature: Labeled Loop Variable//// Note: The code below is pseudo code and therefore cannot be compiled with// current compilers.// This isn't a proposal, these are just ideas (thoughts) :).
template <typename T>void Test(const std::vector<T>& data) {
//
// Syntax (Lines marked with + are additions/modifications):
// -----------------------------------------------------------------------
//
// + decl-specifier-mod:
// + constexpr
// + static
// + thread_local
// + cv-qualifier-seq opt
//
// + labeled-for:
// + decl-specifier-mod opt identifier :
//
// + labeled-for-default-expression:
// + -> init-declarator
//
// iteration-statement:
// + for labeled-for opt ( init-statement condition opt ; expression opt ) labeled-for-default-expression opt
// + for labeled-for opt ( for-range-declaration : for-range-initializer ) labeled-for-default-expression opt
//
// + labeled-break-expression:
// + : expr-or-braced-init-list opt
//
// + labeled-break-statement-expression:
// + identifier labeled-break-expression opt
// + expr-or-braced-init-list
//
// jump-statement:
// + break labeled-break-statement-expression opt ;
// continue ;
// return expr-or-braced-init-list opt ;
// goto identifier ;
// -----------------------------------------------------------------------
// Examples
// -----------------------------------------------------------------------
//
// The below loop will emit a variable definition (labeled loop variable)
// outside of the for-loop with an optional default initialization.
//
// The labeled loop variable cannot be accessed inside the for-loop and
// can only be indirectly modified by using the 'break' keyword with an
// expression as operand.
// This has the advantage that we can use the name of the
// labeled loop variable as label to break out of that loop.
//
// There are several ways one can use the 'break' keyword in this context.
//
// 1.)
//
// This is emitted before the loop:
// sugar for -> int calc_loop = 0;
for calc_loop: (auto& item : data) -> int{0} {
if (item()) {
continue;
}
break 1;
}
if (calc_loop == 0) {
// The loop did not break
} else {
// calc_loop = 1
}
// 2.)
//
// The labeled loop variable 'exit_code' which type is std::optional<int>
// is default initialized.
//
// This is emitted before the loop:
// sugar for -> std::optional<int> exit_code;
for exit_code: (auto& item : data) -> std::optional<int>{} {
if (item()) {
continue;
}
break 1;
}
if (exit_code) {
auto val = *exit_code;
// Deal with the error "returned" by the loop
}
// 3.)
//
// It's possible to break out of an outer labeled for-loop
// by using its label.
// One can also specify an additional expression to be assigned
// to the labeled loop variable associated with the label.
for loop: (auto& item : data) -> std::optional<int>{} {
for (auto nested : item) {
if (nested.cond1) {
// This is going to break out of the outer loop.
// Note that this isn't assigning an expression, this works
// because you can't access the labeled loop variable inside the
// loop. It can only be used as a label.
break loop;
}
if (nested.special_cond) {
break loop: nested.val;
}
if (nested.super) {
break loop: 13 + nested.val;
}
}
}
if (loop) {
auto error = *loop;
// Process the error...
}
// More thoughts:
//
// This whole for-loop-statement can be considered as an expression which
// returns the labeled loop variable, therefore the explicit loop variable
// can be omitted.
//
// The syntax can be simplified to something like this:
auto loop = for (auto& item : data) -> std::optional<int>{} {
for nested_loop: (auto nested : item) {
if (nested.cond1) {
break loop;
}
if (nested.special_cond) {
break loop: nested.val;
}
if (nested.super) {
break loop: 13 + nested.val;
}
if (nested.naeh) {
break nested_loop;
}
}
};
if (loop) {
// Do the processing...
}
}
Why these thoughts? Well, I've been thinking of getting myself to know clang better. So the idea is to implement some playful new C++ syntax in clang :P
If this idea seems like a "okay"-idea, I might think of implementing this in clang.
--
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/02a01c4a-3c45-4665-a531-1dd26d79e60d%40isocpp.org.
------=_Part_1077_147517153.1500601769413
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I would like to get some feedback on these quick thoughts.=
<br>The aim is to make the for-loop more flexible (by supporting better loo=
p control-flow) and eventually turning it into an expression.<div><br></div=
><div>Also, please excuse my English (It's not my native language).<br>=
<pre style=3D"color: rgb(51, 51, 51); line-height: 16.25px;"><br></pre><pre=
style=3D"color: rgb(51, 51, 51); line-height: 16.25px;"><span style=3D"col=
or: rgb(136, 136, 136);">// Feature: Labeled Loop Variable</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// Note: The code below is pseud=
o code and therefore cannot be compiled with</span>
<span style=3D"color: rgb(136, 136, 136);">// current compilers.</spa=
n>
<span style=3D"color: rgb(136, 136, 136);">// This isn't a propos=
al, these are just ideas (thoughts) :).</span>
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">template</span> &=
lt;<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">typename</span=
> T>
<span style=3D"color: rgb(51, 51, 153); font-weight: bold;">void</span> Tes=
t(<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">const</span> st=
d::vector<T>& data) {
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// Syntax (Lines marked with=
+ are additions/modifications):</span>
<span style=3D"color: rgb(136, 136, 136);">// -------------------------=
----------------------------------------------</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// + decl-specifier-mod:</sp=
an>
<span style=3D"color: rgb(136, 136, 136);">// + constexpr</span>
<span style=3D"color: rgb(136, 136, 136);">// + static</span>
<span style=3D"color: rgb(136, 136, 136);">// + thread_local</span=
>
<span style=3D"color: rgb(136, 136, 136);">// + cv-qualifier-seq o=
pt</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// + labeled-for:</span>
<span style=3D"color: rgb(136, 136, 136);">// + decl-specifier-mod=
opt identifier :</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// + labeled-for-default-exp=
ression:</span>
<span style=3D"color: rgb(136, 136, 136);">// + -> init-declara=
tor</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// iteration-statement:</s=
pan>
<span style=3D"color: rgb(136, 136, 136);">// + for labeled-for op=
t ( init-statement condition opt ; expression opt ) labeled-for-default-exp=
ression opt</span>
<span style=3D"color: rgb(136, 136, 136);">// + for labeled-for op=
t ( for-range-declaration : for-range-initializer ) labeled-for-default-exp=
ression opt</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// + labeled-break-expressio=
n:</span>
<span style=3D"color: rgb(136, 136, 136);">// + : expr-or-braced-i=
nit-list opt</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// + labeled-break-statement=
-expression:</span>
<span style=3D"color: rgb(136, 136, 136);">// + identifier labeled=
-break-expression opt</span>
<span style=3D"color: rgb(136, 136, 136);">// + expr-or-braced-ini=
t-list</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// jump-statement:</span>
<span style=3D"color: rgb(136, 136, 136);">// + break labeled-brea=
k-statement-expression opt ;</span>
<span style=3D"color: rgb(136, 136, 136);">// continue ;</span>
<span style=3D"color: rgb(136, 136, 136);">// return expr-or-bra=
ced-init-list opt ;</span>
<span style=3D"color: rgb(136, 136, 136);">// goto identifier ;<=
/span>
<span style=3D"color: rgb(136, 136, 136);">// -------------------------=
----------------------------------------------</span>
<span style=3D"color: rgb(136, 136, 136);">// Examples</span>
<span style=3D"color: rgb(136, 136, 136);">// -------------------------=
----------------------------------------------</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// The below loop will emit =
a variable definition (labeled loop variable)</span>
<span style=3D"color: rgb(136, 136, 136);">// outside of the for-loop w=
ith an optional default initialization.</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// The labeled loop variable=
cannot be accessed inside the for-loop and</span>
<span style=3D"color: rgb(136, 136, 136);">// can only be indirectly mo=
dified by using the 'break' keyword with an</span>
<span style=3D"color: rgb(136, 136, 136);">// expression as operand.</s=
pan>
<span style=3D"color: rgb(136, 136, 136);">// This has the advantage th=
at we can use the name of the</span>
<span style=3D"color: rgb(136, 136, 136);">// labeled loop variable as =
label to break out of that loop.</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// There are several ways on=
e can use the 'break' keyword in this context.</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// 1.)</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// This is emitted before th=
e loop:</span>
<span style=3D"color: rgb(136, 136, 136);">// sugar for -> int calc_=
loop =3D 0;</span>
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">for</span> ca=
lc_loop: (<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">auto</s=
pan>& item : data) -> <span style=3D"color: rgb(51, 51, 153); font-w=
eight: bold;">int</span>{<span style=3D"color: rgb(0, 0, 221); font-weight:=
bold;">0</span>} {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</span>=
(item()) {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">conti=
nue</span>;
}
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">break</sp=
an> <span style=3D"color: rgb(0, 0, 221); font-weight: bold;">1</span>;
}
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</span> (ca=
lc_loop =3D=3D <span style=3D"color: rgb(0, 0, 221); font-weight: bold;">0<=
/span>) {
<span style=3D"color: rgb(136, 136, 136);">// The loop did not brea=
k</span>
} <span style=3D"color: rgb(0, 136, 0); font-weight: bold;">else</span>=
{
<span style=3D"color: rgb(136, 136, 136);">// calc_loop =3D 1</span=
>
}
<span style=3D"color: rgb(136, 136, 136);">// 2.)</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// The labeled loop variable=
'exit_code' which type is std::optional<int></span>
<span style=3D"color: rgb(136, 136, 136);">// is default initialized.</=
span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// This is emitted before th=
e loop:</span>
<span style=3D"color: rgb(136, 136, 136);">// sugar for -> std::opti=
onal<int> exit_code;</span>
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">for</span> ex=
it_code: (<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">auto</s=
pan>& item : data) -> std::optional<<span style=3D"color: rgb(51,=
51, 153); font-weight: bold;">int</span>>{} {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</span>=
(item()) {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">conti=
nue</span>;
}
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">break</sp=
an> <span style=3D"color: rgb(0, 0, 221); font-weight: bold;">1</span>;
}
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</span> (ex=
it_code) {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">auto</spa=
n> val =3D *exit_code;
<span style=3D"color: rgb(136, 136, 136);">// Deal with the error &=
quot;returned" by the loop</span>
}
<span style=3D"color: rgb(136, 136, 136);">// 3.)</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// It's possible to brea=
k out of an outer labeled for-loop</span>
<span style=3D"color: rgb(136, 136, 136);">// by using its label.</span=
>
<span style=3D"color: rgb(136, 136, 136);">// One can also specify an a=
dditional expression to be assigned</span>
<span style=3D"color: rgb(136, 136, 136);">// to the labeled loop varia=
ble associated with the label.</span>
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">for</span> lo=
op: (<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">auto</span>&=
amp; item : data) -> std::optional<<span style=3D"color: rgb(51, 51, =
153); font-weight: bold;">int</span>>{} {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">for</span=
> (<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">auto</span> ne=
sted : item) {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</s=
pan> (nested.cond1) {
<span style=3D"color: rgb(136, 136, 136);">// This is going=
to break out of the outer loop.</span>
<span style=3D"color: rgb(136, 136, 136);">// Note that thi=
s isn't assigning an expression, this works</span>
<span style=3D"color: rgb(136, 136, 136);">// because you c=
an't access the labeled loop variable inside the</span>
<span style=3D"color: rgb(136, 136, 136);">// loop. It can =
only be used as a label.</span>
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">b=
reak</span> loop;
}
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</s=
pan> (nested.special_cond) {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">b=
reak</span> loop: nested.val;
}
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</s=
pan> (nested.super) {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">b=
reak</span> loop: <span style=3D"color: rgb(0, 0, 221); font-weight: bold;"=
>13</span> + nested.val;
}
}
}
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</span> (lo=
op) {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">auto</spa=
n> error =3D *loop;
<span style=3D"color: rgb(136, 136, 136);">// Process the error...<=
/span>
}
<span style=3D"color: rgb(136, 136, 136);">// More thoughts:</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// This whole for-loop-state=
ment can be considered as an expression which</span>
<span style=3D"color: rgb(136, 136, 136);">// returns the labeled loop =
variable, therefore the explicit loop variable</span>
<span style=3D"color: rgb(136, 136, 136);">// can be omitted.</span>
<span style=3D"color: rgb(136, 136, 136);">//</span>
<span style=3D"color: rgb(136, 136, 136);">// The syntax can be simplif=
ied to something like this:</span>
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">auto</span> l=
oop =3D <span style=3D"color: rgb(0, 136, 0); font-weight: bold;">for</span=
> (<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">auto</span>&am=
p; item : data) -> std::optional<<span style=3D"color: rgb(51, 51, 15=
3); font-weight: bold;">int</span>>{} {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">for</span=
> nested_loop: (<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">a=
uto</span> nested : item) {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</s=
pan> (nested.cond1) {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">b=
reak</span> loop;
}
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</s=
pan> (nested.special_cond) {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">b=
reak</span> loop: nested.val;
}
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</s=
pan> (nested.super) {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">b=
reak</span> loop: <span style=3D"color: rgb(0, 0, 221); font-weight: bold;"=
>13</span> + nested.val;
}
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</s=
pan> (nested.naeh) {
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">b=
reak</span> nested_loop;
}
}
};
<span style=3D"color: rgb(0, 136, 0); font-weight: bold;">if</span> (lo=
op) {
<span style=3D"color: rgb(136, 136, 136);">// Do the processing...<=
/span>
}
}</pre><pre style=3D"color: rgb(51, 51, 51); line-height: 16.25px;"><br></p=
re><pre style=3D"color: rgb(51, 51, 51); line-height: 16.25px;">Why these t=
houghts? Well, I've been thinking of getting myself to know clang bette=
r. So the idea is to implement some playful new C++ syntax in clang :P</pre=
><pre style=3D"color: rgb(51, 51, 51); line-height: 16.25px;">If this idea =
seems like a "okay"-idea, I might think of implementing this in c=
lang.</pre></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/02a01c4a-3c45-4665-a531-1dd26d79e60d%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/02a01c4a-3c45-4665-a531-1dd26d79e60d=
%40isocpp.org</a>.<br />
------=_Part_1077_147517153.1500601769413--
------=_Part_1076_987460235.1500601769412--
.
Author: Nicol Bolas <jmckesson@gmail.com>
Date: Thu, 20 Jul 2017 20:52:54 -0700 (PDT)
Raw View
------=_Part_1023_654840930.1500609174744
Content-Type: multipart/alternative;
boundary="----=_Part_1024_447795072.1500609174744"
------=_Part_1024_447795072.1500609174744
Content-Type: text/plain; charset="UTF-8"
The much simpler for loop exit strategies proposa <http://wg21.link/P0082>l
covers the more reasonable cases of this. As for breaking to particular
loops and such, no. That's way too much complexity. If your loops are that
complicated, maybe you should be using a different construct.
--
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/45b516b2-a501-45b5-97e1-9c3d9fec9fd0%40isocpp.org.
------=_Part_1024_447795072.1500609174744
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">The much simpler <a href=3D"http://wg21.link/P0082">for lo=
op exit strategies proposa</a>l covers the more reasonable cases of this. A=
s for breaking to particular loops and such, no. That's way too much co=
mplexity. If your loops are that complicated, maybe you should be using a d=
ifferent construct.<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/45b516b2-a501-45b5-97e1-9c3d9fec9fd0%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/45b516b2-a501-45b5-97e1-9c3d9fec9fd0=
%40isocpp.org</a>.<br />
------=_Part_1024_447795072.1500609174744--
------=_Part_1023_654840930.1500609174744--
.
Author: Giovanni Piero Deretta <gpderetta@gmail.com>
Date: Fri, 21 Jul 2017 08:00:37 -0700 (PDT)
Raw View
------=_Part_1501_1044634399.1500649238056
Content-Type: multipart/alternative;
boundary="----=_Part_1502_690198133.1500649238056"
------=_Part_1502_690198133.1500649238056
Content-Type: text/plain; charset="UTF-8"
On Friday, July 21, 2017 at 4:52:54 AM UTC+1, Nicol Bolas wrote:
>
> The much simpler for loop exit strategies proposa <http://wg21.link/P0082>l
> covers the more reasonable cases of this. As for breaking to particular
> loops and such, no. That's way too much complexity. If your loops are that
> complicated, maybe you should be using a different construct.
>
the op proposal allows returning values from a loop which is nice. Although
I can't shake the feeling that just wrapping the loop in a parameterless
lambda subsumes both proposals and also is lightweight enough not to
require additional sugar:
auto found = [&]
{
for(auto&& r : v)
for (auto&&x : r)
if ( predicate(x) )
return optional{x};
return {};
}
--
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/be63987b-9261-492c-8a20-cac9cb42d503%40isocpp.org.
------=_Part_1502_690198133.1500649238056
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Friday, July 21, 2017 at 4:52:54 AM UTC+1, Nicol Bolas =
wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8=
ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">The muc=
h simpler <a href=3D"http://wg21.link/P0082" target=3D"_blank" rel=3D"nofol=
low" onmousedown=3D"this.href=3D'http://www.google.com/url?q\x3dhttp%3A=
%2F%2Fwg21.link%2FP0082\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHdI_3wotqZR=
EuL6fuPQaRLkrFYGg';return true;" onclick=3D"this.href=3D'http://www=
..google.com/url?q\x3dhttp%3A%2F%2Fwg21.link%2FP0082\x26sa\x3dD\x26sntz\x3d1=
\x26usg\x3dAFQjCNHdI_3wotqZREuL6fuPQaRLkrFYGg';return true;">for loop e=
xit strategies proposa</a>l covers the more reasonable cases of this. As fo=
r breaking to particular loops and such, no. That's way too much comple=
xity. If your loops are that complicated, maybe you should be using a diffe=
rent construct.<br></div></blockquote><div><br>the op proposal allows retur=
ning values from a loop which is nice. Although I can't shake the feeli=
ng that just wrapping the loop in a parameterless lambda subsumes both prop=
osals and also is lightweight enough not to require additional sugar:<br><b=
r>auto found =3D [&]<br>{<br>=C2=A0=C2=A0 for(auto&& r : v)<br>=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 for (auto&&x : r)<=
br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0 if ( predicate(x) )<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return optional{x};<br>=C2=A0=
return {};<br>}<br><br></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/be63987b-9261-492c-8a20-cac9cb42d503%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/be63987b-9261-492c-8a20-cac9cb42d503=
%40isocpp.org</a>.<br />
------=_Part_1502_690198133.1500649238056--
------=_Part_1501_1044634399.1500649238056--
.
Author: Larry Evans <cppljevans@suddenlink.net>
Date: Sat, 22 Jul 2017 02:48:32 -0500
Raw View
On 07/21/2017 10:00 AM, Giovanni Piero Deretta wrote:
> On Friday, July 21, 2017 at 4:52:54 AM UTC+1, Nicol Bolas wrote:
>
> The much simpler for loop exit strategies proposa
> <http://wg21.link/P0082>l covers the more reasonable cases of this.
> As for breaking to particular loops and such, no. That's way too
> much complexity. If your loops are that complicated, maybe you
> should be using a different construct.
>
>
> the op proposal allows returning values from a loop which is nice.
> Although I can't shake the feeling that just wrapping the loop in a
> parameterless lambda subsumes both proposals and also is lightweight
> enough not to require additional sugar:
>
> auto found = [&]
> {
> for(auto&& r : v)
> for (auto&&x : r)
> if ( predicate(x) )
> return optional{x};
> return {};
> }
Giovanni, with this scheme, how would you exit a specific
loop. For example, the loop with loop variable x. The
above, IIUC, exits both the r and x loops.
I believe this was done OP's last example, IOW, the one
with:
auto loop = for (auto& item : data) -> std::optional<int>{} {
for nested_loop: (auto nested : item) {
if (nested.cond1) {
break loop;//break's outer loop.
}
if (nested.special_cond) {
break loop: nested.val;//breaks outer loop.
}
if (nested.super) {
break loop: 13 + nested.val;//breaks outer loop.
}
if (nested.naeh) {
break nested_loop;//breaks just inner loop.
}
}
};
if (loop) {
// Do the processing...
}
BTW, peetpaul69, this beaking out of several loops idea was
proposed in 1979 here:
http://dl.acm.org/citation.cfm?id=359057
however, in the arsac paper, instead of:
break labeled-break-statement-expression
the syntax was:
exit(p)
where p is integer >= 0 and the exit(p) statement means:
go out of the p-th enclosing loop and proceed
sequentially after it.
The examples in the reference included restructuring go-to
programs to make them structured.
Just thought this might provide another selling point for
your idea.
-regards,
Larry
--
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/okuvtk%242ra%241%40blaine.gmane.org.
.
Author: Larry Evans <cppljevans@suddenlink.net>
Date: Sat, 22 Jul 2017 03:02:35 -0500
Raw View
On 07/22/2017 02:48 AM, Larry Evans wrote:
[snip]
> BTW, peetpaul69, this beaking out of several loops idea was
> proposed in 1979 here:
>
> http://dl.acm.org/citation.cfm?id=359057
>
It was also proposed in this newsgroup in a post with headers:
From: amgstc@gmail.com
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Have you ever considered break( n ) ?
Date: Thu, 8 Sep 2016 04:54:14 -0700 (PDT)
Several people gave feedback to that post.
-regards,
Larry
--
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/okv0nv%2460v%241%40blaine.gmane.org.
.