[Issue 10712] New: Compiletime foreach loop

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jul 25 01:09:52 PDT 2013


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: nobody at puremagic.com
        ReportedBy: temtaime at gmail.com


--- Comment #0 from Temtaime <temtaime at gmail.com> 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: -------


More information about the Digitalmars-d-bugs mailing list