Variadic template arguments unpacking

TommiT tommitissari at hotmail.com
Sat Jul 6 11:17:33 PDT 2013


On Saturday, 6 July 2013 at 15:23:40 UTC, Artur Skawina wrote:
> ...

Can you tell me why this isn't working though?

import std.stdio, std.typetuple;

template _TypeMap(alias MAP, size_t N, TS...) {
   static if (N<TS.length)
       alias _TypeMap = _TypeMap!(MAP,
                                  N+1,
                                  TS[0..N],
                                  typeof(MAP(TS[N].init)),
                                  TS[N+1..$]);
   else
       alias _TypeMap = TS;
}
template TypeMap(alias MAP, TS...) {
  alias TypeMap = _TypeMap!(MAP, 0, TS);
}

struct _ForEach(alias MAP, TS...)
{
   TypeMap!(MAP, TS) tuple;

   this(TS values)
   {
       foreach (i, v; values)
           tuple[i] = MAP(v);
   }
}

auto ForEach(alias MAP, TS...)(TS ts)
{
   return _ForEach!(MAP, TS)(ts);
}


int[2] getArray(int n)
{
     int[2] data = n;
     return data;
}

void foo(R...)(R ranges)
{
     foreach (range; ranges)
         foreach (value; range)
             write(value, " ");
}

void main()
{
     alias pack = TypeTuple!(1, 2, 3);

     // prints random garbage
     foo(ForEach!(tmp => tmp[])
                 (ForEach!(a => getArray(a))(pack).tuple).tuple);
}


More information about the Digitalmars-d-learn mailing list