Topic: for...(T : t) // fold expression improvement


Author: z.p.gaal@gmail.com
Date: Fri, 18 Nov 2016 00:14:11 -0800 (PST)
Raw View
------=_Part_3700_1177573913.1479456851285
Content-Type: text/plain; charset=UTF-8

During metaprogramming lots of dummy functions have to be implemented to process tuples and/or variadic templates. I don't know too much about proposal submission and I don't have so deep knowledge of the language either.
But it'd be great to have expressions something like

template<typename... T>
void foo(T... a) {
for...( Ti ai : a ) {
std::cout << a
}
}

It'd make the code more readable and remove lots of index_sequence based hacks.

Is there any similar proposal already. What do you think of this idea?

thanks Gzp

--
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/7c87350a-7ac2-489b-b107-ea25a7bd6c74%40isocpp.org.

------=_Part_3700_1177573913.1479456851285--

.


Author: Simon Brand <tartanllama@gmail.com>
Date: Fri, 18 Nov 2016 08:34:04 +0000
Raw View
If I understand your code correctly, you could implement it with the
current fold expression proposal like this:

template <typename... Ts>
void foo(Ts... ts) {
     ((std::cout << ts), ...);
}


On 18/11/16 08:14, z.p.gaal@gmail.com wrote:
> During metaprogramming lots of dummy functions have to be implemented to process tuples and/or variadic templates. I don't know too much about proposal submission and I don't have so deep knowledge of the language either.
> But it'd be great to have expressions something like
>
> template<typename... T>
> void foo(T... a) {
> for...( Ti ai : a ) {
> std::cout << a
> }
> }
>
> It'd make the code more readable and remove lots of index_sequence based hacks.
>
> Is there any similar proposal already. What do you think of this idea?
>
> thanks Gzp
>

--
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/110fecc1-a731-f6ad-fa8d-cfe2cb36faaa%40gmail.com.

.


Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Fri, 18 Nov 2016 10:36:29 +0200
Raw View
On 18 November 2016 at 10:34, Simon Brand <tartanllama@gmail.com> wrote:
> If I understand your code correctly, you could implement it with the current
> fold expression proposal like this:
>
> template <typename... Ts>
> void foo(Ts... ts) {
>     ((std::cout << ts), ...);
> }

Alternatively, fold over a lambda:

#include <iostream>

template<typename... T>
void foo(T... a) {
    ([&]() {
        std::cout << a << std::endl;
    }(),...);
}

int main()
{
    foo(1, 2, 3);
}

Live demo: http://melpon.org/wandbox/permlink/gYT0lEILKrxgSTzm

>
>
> On 18/11/16 08:14, z.p.gaal@gmail.com wrote:
>>
>> During metaprogramming lots of dummy functions have to be implemented to
>> process tuples and/or variadic templates. I don't know too much about
>> proposal submission and I don't have so deep knowledge of the language
>> either.
>> But it'd be great to have expressions something like
>>
>> template<typename... T>
>> void foo(T... a) {
>> for...( Ti ai : a ) {
>> std::cout << a
>> }
>> }
>>
>> It'd make the code more readable and remove lots of index_sequence based
>> hacks.
>>
>> Is there any similar proposal already. What do you think of this idea?
>>
>> thanks Gzp
>>
>
> --
> 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/110fecc1-a731-f6ad-fa8d-cfe2cb36faaa%40gmail.com.

--
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/CAFk2RUY6eJ2VpP2wVUcZBM5yJBbrwrJ3TdWBZRoH0Eho%2BYacfA%40mail.gmail.com.

.


Author: z.p.gaal@gmail.com
Date: Fri, 18 Nov 2016 00:41:41 -0800 (PST)
Raw View
------=_Part_5830_1942886715.1479458501493
Content-Type: text/plain; charset=UTF-8

hm, i have not thought of lamba. Thats quite readable. Thanks a lot.

--
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/609e0194-3fc8-4442-8fc5-ba17abeb04a4%40isocpp.org.

------=_Part_5830_1942886715.1479458501493--

.


Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Fri, 18 Nov 2016 10:46:26 +0200
Raw View
On 18 November 2016 at 10:41,  <z.p.gaal@gmail.com> wrote:
> hm, i have not thought of lamba. Thats quite readable. Thanks a lot.

Caveat emptor, it doesn't work with gcc or msvc yet.

--
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/CAFk2RUYKzPDDML9%2BM6q6njGC2qhdYDvmYgnpg2r5D5Wb15tt_w%40mail.gmail.com.

.


Author: z.p.gaal@gmail.com
Date: Fri, 18 Nov 2016 05:09:55 -0800 (PST)
Raw View
------=_Part_5801_1481313698.1479474595617
Content-Type: text/plain; charset=UTF-8

using the array initializer trick it shall work.

int dummy[] = {([&]{ std::cout<<aArgs;
}(),0)...};

(At least from msvc, not tested the others)

--
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/eb78d85f-058c-4e87-94a4-691d8ac41a71%40isocpp.org.

------=_Part_5801_1481313698.1479474595617--

.


Author: =?UTF-8?Q?Klaim_=2D_Jo=C3=ABl_Lamotte?= <mjklaim@gmail.com>
Date: Fri, 18 Nov 2016 15:18:11 +0100
Raw View
--001a1143a046e0ad96054193f9d4
Content-Type: text/plain; charset=UTF-8

The only issue is that it is a trick and not an explicit instruction that
normal humans
can understand when they read the code. :)

On 18 November 2016 at 14:09, <z.p.gaal@gmail.com> wrote:

> using the array initializer trick it shall work.
>
> int dummy[] = {([&]{ std::cout<<aArgs;
> }(),0)...};
>
> (At least from msvc, not tested the others)
>
> --
> 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/eb78d85f-058c-4e87-
> 94a4-691d8ac41a71%40isocpp.org.
>

--
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/CAOU91OOjjyKjwS8VRLSAoZ3-iS8B57Yi8Jyz%3DfHqcXFepFLqDw%40mail.gmail.com.

--001a1143a046e0ad96054193f9d4
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">The only issue is that it is a trick and not an explicit i=
nstruction that normal humans<div>can understand when they read the code. :=
)</div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On 1=
8 November 2016 at 14:09,  <span dir=3D"ltr">&lt;<a href=3D"mailto:z.p.gaal=
@gmail.com" target=3D"_blank">z.p.gaal@gmail.com</a>&gt;</span> wrote:<br><=
blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px=
 #ccc solid;padding-left:1ex">using the array initializer trick it shall wo=
rk.<br>
<br>
int dummy[] =3D {([&amp;]{ std::cout&lt;&lt;aArgs;<br>
}(),0)...};<br>
<br>
(At least from msvc, not tested the others)<br>
<span class=3D""><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 <a href=3D"mailto:std-proposals%2Bunsubscribe@isocpp.org">std-propo=
sals+unsubscribe@<wbr>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>
</span>To view this discussion on the web visit <a href=3D"https://groups.g=
oogle.com/a/isocpp.org/d/msgid/std-proposals/eb78d85f-058c-4e87-94a4-691d8a=
c41a71%40isocpp.org" rel=3D"noreferrer" target=3D"_blank">https://groups.go=
ogle.com/a/<wbr>isocpp.org/d/msgid/std-<wbr>proposals/eb78d85f-058c-4e87-<w=
br>94a4-691d8ac41a71%40isocpp.org</a><wbr>.<br>
</blockquote></div><br></div>

<p></p>

-- <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 <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/CAOU91OOjjyKjwS8VRLSAoZ3-iS8B57Yi8Jyz=
%3DfHqcXFepFLqDw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAOU91OOjjyKjwS=
8VRLSAoZ3-iS8B57Yi8Jyz%3DfHqcXFepFLqDw%40mail.gmail.com</a>.<br />

--001a1143a046e0ad96054193f9d4--

.


Author: Matthew Woehlke <mwoehlke.floss@gmail.com>
Date: Fri, 18 Nov 2016 11:07:20 -0500
Raw View
On 2016-11-18 03:36, Ville Voutilainen wrote:
> Alternatively, fold over a lambda:
>
> template<typename... T>
> void foo(T... a) {
>     ([&]() {
>         std::cout << a << std::endl;
>     }(),...);
> }

Wow... that's twisty :-). I didn't know you could do that with fold
expressions! (That... actually creates a *different lambda* for each
item in `a`!)

> Caveat emptor, it doesn't work with gcc or msvc yet.

This version should work:

  template<typename... T>
  void foo(T... a) {
      ([&](auto&& ai) {
          std::cout << ai << std::endl;
      }(a),...);
  }

....and is less surprising that it does ;-). Instead of folding on the
lambda itself, this version folds on invocation of the lambda (albeit a
templated lambda).

--
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/582F2738.7040209%40gmail.com.

.


Author: Richard Smith <richard@metafoo.co.uk>
Date: Fri, 18 Nov 2016 17:02:39 -0800
Raw View
--001a114dc4b6dec4f605419cfbce
Content-Type: text/plain; charset=UTF-8

On 18 November 2016 at 08:07, Matthew Woehlke <mwoehlke.floss@gmail.com>
wrote:

> On 2016-11-18 03:36, Ville Voutilainen wrote:
> > Alternatively, fold over a lambda:
> >
> > template<typename... T>
> > void foo(T... a) {
> >     ([&]() {
> >         std::cout << a << std::endl;
> >     }(),...);
> > }
>
> Wow... that's twisty :-). I didn't know you could do that with fold
> expressions! (That... actually creates a *different lambda* for each
> item in `a`!)
>
> > Caveat emptor, it doesn't work with gcc or msvc yet.
>
> This version should work:
>
>   template<typename... T>
>   void foo(T... a) {
>       ([&](auto&& ai) {
>           std::cout << ai << std::endl;
>       }(a),...);
>   }
>
> ...and is less surprising that it does ;-). Instead of folding on the
> lambda itself, this version folds on invocation of the lambda (albeit a
> templated lambda).
>

I would probably write that as:

  template<typename... T>
  void foo(T... a) {
      auto print = [](auto&& ai) {
          std::cout << ai << std::endl;
      };
      (print(a), ...);
  }

I find that a bit more readable, especially as it gives a name to the block
of code, and as a bonus it avoids unnecessarily creating N different
closure types. But I like Ville's approach too :)

As an aside, we should probably consider making the parentheses optional
when a fold-expression is the sole constituent of an expression-statement.
There's no parsing problem I can see with

  print(a), ...;

or

  std::cout << ... << a;

I'd also like to allow a lower-precedence-than-the-operator subexpression
on the left or right of a fold-expression, not just a cast-expression.
Allowing an equal-precedence operator could also be useful:

  (std::cout << ... << a.name() << "=" << a.value() << ";") << std::endl;

Also, some use cases need a "joiner" between pairs of elements, but at
neither the start or the end, which we could support with something like:

  (std::cout << ... << ", " << ... << a) << std::endl;

(expanding to "(std::cout << a$0 << ", " << a$1 << ", " << a$2) <<
std::endl;"), although maybe that's a little too magical.

--
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/CAOfiQqkrTmvmh8_ei7sg%2BcQcA1G_Sev6rDGn4UjyVBObztRhxw%40mail.gmail.com.

--001a114dc4b6dec4f605419cfbce
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On 1=
8 November 2016 at 08:07, Matthew Woehlke <span dir=3D"ltr">&lt;<a href=3D"=
mailto:mwoehlke.floss@gmail.com" target=3D"_blank">mwoehlke.floss@gmail.com=
</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin=
:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"=
><span class=3D"gmail-">On 2016-11-18 03:36, Ville Voutilainen wrote:<br>
&gt; Alternatively, fold over a lambda:<br>
&gt;<br>
</span><span class=3D"gmail-">&gt; template&lt;typename... T&gt;<br>
&gt; void foo(T... a) {<br>
&gt;=C2=A0 =C2=A0 =C2=A0([&amp;]() {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0std::cout &lt;&lt; a &lt;&lt; std::en=
dl;<br>
&gt;=C2=A0 =C2=A0 =C2=A0}(),...);<br>
&gt; }<br>
<br>
</span>Wow... that&#39;s twisty :-). I didn&#39;t know you could do that wi=
th fold<br>
expressions! (That... actually creates a *different lambda* for each<br>
item in `a`!)<br>
<span class=3D"gmail-"><br>
&gt; Caveat emptor, it doesn&#39;t work with gcc or msvc yet.<br>
<br>
</span>This version should work:<br>
<span class=3D"gmail-"><br>
=C2=A0 template&lt;typename... T&gt;<br>
=C2=A0 void foo(T... a) {<br>
</span>=C2=A0 =C2=A0 =C2=A0 ([&amp;](auto&amp;&amp; ai) {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 std::cout &lt;&lt; ai &lt;&lt; std::endl=
;<br>
=C2=A0 =C2=A0 =C2=A0 }(a),...);<br>
=C2=A0 }<br>
<br>
....and is less surprising that it does ;-). Instead of folding on the<br>
lambda itself, this version folds on invocation of the lambda (albeit a<br>
templated lambda).<br></blockquote><div><br></div><div>I would probably wri=
te that as:</div><div><br></div><div><span class=3D"gmail-">=C2=A0 template=
&lt;typename... T&gt;<br>=C2=A0 void foo(T... a) {</span></div><div><span c=
lass=3D"gmail-">=C2=A0 =C2=A0 =C2=A0 auto print =3D=C2=A0</span>[](auto&amp=
;&amp; ai) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 std::cout &lt;&lt; ai &l=
t;&lt; std::endl;<br>=C2=A0 =C2=A0 =C2=A0 };</div><div>=C2=A0 =C2=A0 =C2=A0=
 (print(a), ...);</div><div>=C2=A0 }<br></div><div><br></div><div>I find th=
at a bit more readable, especially as it gives a name to the block of code,=
 and as a bonus it avoids unnecessarily creating N different closure types.=
 But I like Ville&#39;s approach too :)</div><div><br></div><div>As an asid=
e, we should probably consider making the parentheses optional when a fold-=
expression is the sole constituent of an expression-statement. There&#39;s =
no parsing problem I can see with</div><div><br></div><div>=C2=A0 print(a),=
 ...;</div><div><br></div><div>or</div><div><br></div><div>=C2=A0 std::cout=
 &lt;&lt; ... &lt;&lt; a;</div><div><br></div><div>I&#39;d also like to all=
ow a lower-precedence-than-the-operator subexpression on the left or right =
of a fold-expression, not just a cast-expression. Allowing an equal-precede=
nce operator could also be useful:</div><div><br></div><div>=C2=A0 (std::co=
ut &lt;&lt; ... &lt;&lt; <a href=3D"http://a.name">a.name</a>() &lt;&lt; &q=
uot;=3D&quot; &lt;&lt; a.value() &lt;&lt; &quot;;&quot;) &lt;&lt; std::endl=
;</div><div><br></div><div>Also, some use cases need a &quot;joiner&quot; b=
etween pairs of elements, but at neither the start or the end, which we cou=
ld support with something like:</div><div><br></div><div>=C2=A0 (std::cout =
&lt;&lt; ... &lt;&lt; &quot;, &quot; &lt;&lt; ... &lt;&lt; a) &lt;&lt; std:=
:endl;</div><div><br></div><div>(expanding to &quot;(std::cout &lt;&lt; a$0=
 &lt;&lt; &quot;, &quot; &lt;&lt; a$1 &lt;&lt; &quot;, &quot; &lt;&lt; a$2)=
 &lt;&lt; std::endl;&quot;), although maybe that&#39;s a little too magical=
..</div></div></div></div>

<p></p>

-- <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 <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/CAOfiQqkrTmvmh8_ei7sg%2BcQcA1G_Sev6rD=
Gn4UjyVBObztRhxw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAOfiQqkrTmvmh8=
_ei7sg%2BcQcA1G_Sev6rDGn4UjyVBObztRhxw%40mail.gmail.com</a>.<br />

--001a114dc4b6dec4f605419cfbce--

.