Typed variadic template syntax?

Timon Gehr timon.gehr at gmx.ch
Wed Jan 29 01:50:12 PST 2014


On 01/29/2014 08:01 AM, Andrei Alexandrescu wrote:
>
> int value(int xs[]...) {
>    int result = 0;
>    foreach (i; xs) {
>      result += 10 * result + i;
>    }
>    return result;
> }
>
> unittest
> {
>    static assert(value(1, 2) == 21);
> }
> ...

import std.range, std.algorithm;
int value(int xs[]...) {
     return reduce!((a,b)=>10*a+b)(0,xs.retro);
}
unittest{
     static assert(value()==0);
     static assert(value(1)==1);
     static assert(value(1,2)==21);
     static assert(value(1,2,3)==321);
}



More information about the Digitalmars-d mailing list