iterate over variadic

Lamex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 9 16:09:24 PDT 2017


On Sunday, 9 July 2017 at 22:21:59 UTC, FoxyBrown wrote:
> How can we iterate over a variadic and have it's index. I'll do 
> different things depend on if it's an even or odd index, but 
> seems to be no way to get it.


import std.stdio;
import std.typecons, std.meta;

template indexedAllSatisfy(alias F, T...)
{
     bool doTest()
     {
         bool result = true;
         import std.range: iota;
         foreach(i; aliasSeqOf!(iota(0, T.length)))
             result &= F!(T[i], i, T.length);
         return result;
     }
     enum indexedAllSatisfy = doTest();
}

unittest
{
     template evenIntString(T, int index, int length)
     {
         static if (length & 1)
             enum evenIntString = false;
         else static if (index & 1)
             enum evenIntString = is(T == string);
         else
             enum evenIntString = is(T == int);
     }

     static assert(indexedAllSatisfy!(evenIntString, int, string, 
int, string));
     static assert(!indexedAllSatisfy!(evenIntString, int , 
string, char, Object));
}


More information about the Digitalmars-d-learn mailing list