Variadic template arguments unpacking
TommiT
tommitissari at hotmail.com
Sat Jul 6 11:34:14 PDT 2013
On Saturday, 6 July 2013 at 18:17:34 UTC, TommiT wrote:
> Can you tell me why this isn't working though?
I can get this so far that it prints: 1 1 2 0 <random> <random>
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]][0])),
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!((ref int[2] tmp) { return tmp[]; })
(ForEach!(a => getArray(a))(pack).tuple).tuple);
}
More information about the Digitalmars-d-learn
mailing list