[OT] ISO C++ 17 changes

Martin Nowak via Digitalmars-d digitalmars-d at puremagic.com
Sat Apr 8 00:28:58 PDT 2017


On Tuesday, 4 April 2017 at 13:45:47 UTC, Meta wrote:
> I mean what goes on inside fold. If you look at the C++ example 
> it's very concise and IMO beautiful:
>
> <typename ...Args> auto f(Args ...args) { return (0 + ... + 
> args); }

It's special syntax for a very limited (only infix operators) and 
rather obscure use-case. There are many different ways to do this 
already, recursive calls, static foreach, string mixin, and they 
work with any reduce function/op.

It seems the reason C++ needs this, is because parameter pack 
expansion is so restricted (and complex at the same time 
http://en.cppreference.com/w/cpp/language/parameter_pack).

Compare this with a fully generic fold in a few lines.

   fold(alias op, Args...)(Args args) if (Args.length > 1)
   {
       static if (Args.length > 2)
           return op(args[0], fold!op(args[1 .. $]));
       else
           return op(args[0], args[1]);
   }


More information about the Digitalmars-d mailing list