[OT] ISO C++ 17 changes
Timon Gehr via Digitalmars-d
digitalmars-d at puremagic.com
Thu Apr 6 13:48:25 PDT 2017
On 03.04.2017 23:43, Meta wrote:
> On Monday, 3 April 2017 at 21:33:20 UTC, Timon Gehr wrote:
>> On 03.04.2017 20:24, Meta wrote:
>>> On Monday, 3 April 2017 at 08:53:57 UTC, Andrea Fontana wrote:
>>>> https://isocpp.org/files/papers/p0636r0.html
>>>
>>> The fold expressions and inference of constructor template args look
>>> very nice. C++ is quickly catching up to D in a lot of areas, although
>>> usually still with worse syntax. I don't know how fold expressions could
>>> be emulated in D.
>>
>> String mixins.
>
> I try to avoid string mixins if possible as they're incredibly ugly. I
> did try hacking something together with templates and string mixins,
> though, but could not get it to work. I stopped here:
>
> template fold(string op, Args...)
> {
> static if (Args.length == 1)
> enum fold = Args[0];
> else
> enum fold = mixin("Args[0] " ~ op ~ " fold!(op, Args[1..$])");
> //variable _param_2 cannot be read at compile time
> }
>
> auto f(Args...)(Args args)
> {
> return fold!("+", args);
> }
>
> void main()
> {
> assert(f(1, 2, 3) == 6);
> }
>
> Any suggestions as to how to get something similar working?
I usually just use:
mixin(iota(args.length).map!(i=>text("args[",i,"]")).join("+"))
Obvious and fully customizable; IMHO, the aesthetics are actually better
than for the C++ example:
"0 + ... + args" ?
"0 + args" does not make sense, so why should "0 + ... + args" ?
Furthermore, the feature explicitly special-cases operators (why not
allow fold over arbitrary binary functions?) and adds a few more rules
to C++'s language definition.
More information about the Digitalmars-d
mailing list