Another interesting hack: expand a static array into parameter arguments

Andrej Mitrovic andrej.mitrovich at gmail.com
Wed Apr 2 12:46:36 PDT 2014


On Wednesday, 2 April 2014 at 19:40:41 UTC, Andrej Mitrovic wrote:
> snip

Cleaned up version:

-----
import std.stdio;
import std.traits;
import std.typetuple;

auto Delay(alias arg, size_t idx)() { return arg[idx]; }

template expand(alias array, size_t idx = 0)
     if (isStaticArray!(typeof(array)))
{
     alias Array = typeof(array);

     static if (idx + 1 < Array.length)
         alias expand = TypeTuple!(Delay!(array, idx),
                                   expand!(array, idx + 1));
     else
         alias expand = Delay!(array, idx);
}

void print(int a) { writefln("1: %s", a); }
void print(int a, int b) { writefln("2: %s %s", a, b); }
void print(int a, int b, int c) { writefln("3: %s %s %s", a, b, 
c); }

void main()
{
     int[1] arr1 = [1];
     int[2] arr2 = [1, 2];
     int[3] arr3 = [1, 2, 3];

     print(expand!arr1);
     print(expand!arr2);
     print(expand!arr3);
}
-----


More information about the Digitalmars-d mailing list