http://d.puremagic.com/issues/show_bug.cgi?id=10712

           Summary: Compiletime foreach loop
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: [email protected]
        ReportedBy: [email protected]


--- Comment #0 from Temtaime <[email protected]> 2013-07-25 01:09:52 PDT ---
Sometimes it's neccessary to organize compiletime loop/unroll another loop for
better perfomance.

I'd make that simple template:

template IndexTuple(int e, int s = 0, T...) {
    static if(s == e)
        alias IndexTuple = T;
    else
        static if(s > e)
            alias IndexTuple = IndexTuple!(e, s - 1, T, s);
        else
            alias IndexTuple = IndexTuple!(e, s + 1, T, s);
}

Use:
foreach(idx; IndexTuple!10)
writeln(idx); // prints 0, 1, ..., 9

foreach(idx; IndexTuple!(10, 2))
writeln(idx); // prints 2, 3, ..., 9

foreach(idx; IndexTuple!-10)
writeln(idx); // prints 0, -1, ..., -9

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------

Reply via email to