Map one tuple to another Tuple of different type
Daniel Gibson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jul 21 09:36:04 PDT 2014
Am 21.07.2014 17:04, schrieb TheFlyingFiddle:
> 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!();
> }
>
Yeah, I had a similar workaround:
template TupleIndicesImpl(alias len, I...)
{
static if(len == I.length) // also handles len == 0
alias TupleIndicesImpl = I;
else static if(I.length == 0)
alias TupleIndicesImpl = TupleIndicesImpl!(len, 0);
else // I contains 0 ... I.length - 1, so add I.length
alias TupleIndicesImpl = TupleIndicesImpl!(len, I, I.length);
}
template TupleIndices(alias len)
{
alias TupleIndices = TupleIndicesImpl!(len);
}
foreach(i; TupleIndices!(myTuple.length) { ... }
At least for iterating over a tuple Vlad's way suggestion
("foreach(i, U; TupleType)") is nicer and more concise.
However, having something like staticIota in the stdlib would probably
make sense.
Cheers,
Daniel
More information about the Digitalmars-d-learn
mailing list