How to expand an expression along with a parameter tuple?

TommiT tommitissari at hotmail.com
Mon Jun 17 19:37:45 PDT 2013


On Monday, 17 June 2013 at 13:59:34 UTC, Artur Skawina wrote:
> [..]

Your setup has a pretty serious issue with correctness though. 
It's because all the types of _ForEach.tuple are the same as the 
first element of TS...

import std.stdio;

template NTup(size_t N, T...)
{
     static if (N > 1)
         alias NTup = NTup!(N-1, T, T[$-1]);
     else
         alias NTup = T;
}

struct _ForEach(alias MAP, TS...)
{
     NTup!(TS.length, typeof(MAP(TS[0].init))) tuple;

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

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

void foo(T...)(T values)
{
     foreach (v; values)
         writeln(v);
}

void bar(T...)(T values)
{
     foo(ForEach!(a => a + 1)(values).tuple);
}


void main()
{
     bar(10, 3_000_000_000u);
}
-----
Prints:
11
-1294967295

Change the call to bar(1, 3L); and it wouldn't even compile.


More information about the Digitalmars-d-learn mailing list