[Issue 8614] New: Cannot change the iteration index during CTFE

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Sep 3 10:22:15 PDT 2012


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

           Summary: Cannot change the iteration index during CTFE
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: philippe.sigaud at gmail.com


--- Comment #0 from Philippe Sigaud <philippe.sigaud at gmail.com> 2012-09-03 19:22:36 CEST ---
During runtime execution of a foreach loop, you can change the index. This
doesn't work at compile-time and gives a different result when foo is called
through CTFE or at runtime:


import std.stdio;
int[] foo(int[] data) 
{
     foreach (i, ref x; data) {
         x++;
         i++;
     }
     return data;
}

void main() 
{
    // Compile-time
    enum result1 = foo([10, 20, 30, 40]);
    assert(result1 == [11, 21, 31, 41]);

    // Runtime
    auto result2 = foo([10, 20, 30, 40]);
    assert(result2 == [11, 20, 31, 40]);
}

-- 
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