[Issue 3835] New: [CTFE] Fragile CTFE

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Feb 18 13:06:34 PST 2010


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

           Summary: [CTFE] Fragile CTFE
           Product: D
           Version: 2.040
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-02-18 13:06:33 PST ---
This code (that uses CTFE to do something that can be useful, it initializes an
array with values compiled at compile time) compiles and works correctly, but I
think it's too much fragile: if you change the for loop a little, or other
details in genFactorials(), the program stops compiling:

int factorial(int n) {
    if (n == 0)
       return 1;
    return n * factorial(n - 1);
}

int[] genFactorials(int n) {
    int[] result;
    for (; n >= 0; --n)
        result = factorial(n) ~ result;
    return result;
}

const int N = 13;
static const auto factorials = cast(int[N])genFactorials(N - 1);
void main() {}


If you need them, I can show many variants of this program that don't
compile/work.
I think to be useful in real programs CTFE has to be less fragile than this.

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