Map one tuple to another Tuple of different type

TheFlyingFiddle via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 21 08:04:13 PDT 2014


On Monday, 21 July 2014 at 01:42:58 UTC, Daniel Gibson wrote:
> Am 21.07.2014 03:34, schrieb Vlad Levenfeld:
>> To get a foreach to run at compile-time, you have to give it 
>> something
>> whose value is known to the compiler (so, T and 
>> typeof(argTuple) would
>> suffice, and 0..T.length really should as well).
>
> Yup

I use this when i want a compile time foreach(from a constant 
number). It's slightly longer but has worked great for me thus 
far.

template staticIota(size_t s, size_t e, size_t step = 1)
{
     import std.typetuple : TypeTuple;
     static if(s < e)
	alias staticIota = TypeTuple!(s, staticIota!(s + step, e));
     else
	alias staticIota = TypeTuple!();
}


usage:

unittest
{
     foreach(i; staticIota!(0, 10))
     {
         pragma(msg, i);
     }
}

//Outputs 1 to 10 at compile-time.




More information about the Digitalmars-d-learn mailing list