[Issue 3835] [CTFE] Fragile CTFE

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 1 15:09:56 PST 2010


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


Serg Kovrov <kovrov+puremagic at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kovrov+puremagic at gmail.com


--- Comment #3 from Serg Kovrov <kovrov+puremagic at gmail.com> 2010-03-01 15:09:52 PST ---
Looks like foreach do not work in CTFE. Here is example with two equivalent
functions, one uses 'for', other 'foreach' loops:


 uint[256] crc32_table_for()
 {
     uint[256] table;
     for (uint i = 0; i < 256; ++i) {
         uint crc = i;
         for (int j = 0; j < 8; j++)
             crc = crc & 1 ? (crc >> 1) ^ 0xEDB88320 : crc >> 1;
         table[i] = crc;
     }
     return table;
 }

 uint[256] crc32_table_foreach()
 {
     uint[256] table;
     foreach (n, ref i; table) {
         uint crc = n;
         foreach (j; 0 .. 8)
             crc = crc & 1 ? (crc >> 1) ^ 0xEDB88320 : crc >> 1;
         i = crc;
     }
     return table;
 }

 immutable crc32_table1 = crc32_table_for();
 immutable crc32_table2 = crc32_table_foreach();

 import std.stdio;
 void main()
 {
     writefln("%s", crc32_table1);
     writefln("%s", crc32_table2);
     assert (crc32_table1 == crc32_table2);
 }

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