CTFE history

bearophile bearophileHUGS at lycos.com
Fri Nov 19 06:03:28 PST 2010

Michal Minich:

>see thread "Compile time function execution..." at http://www.digitalmars.com/d/archives/ digitalmars/D/index2007.html. In the previous discussion you can see this idea forming (I searched for the word "compile").<

Thank you, I have found and read some of those threads. It seems the idea of CTFE comes from 3-4 persons, and it has evolved progressively from finding the limits of template-based execution of regex, it was not invented in a single step by a single person. It's indeed an interesting story, worth writing a little article about. The "constexpr" has shown up in the threads only after CTFE was already in DMD, so maybe there is no direct influence of it.

After reading some of those threads I have a new question. Let's say I have a function bad() that I want to run at CT, it calls two functions, a slow CT function named aVerySlowCTFunction() and a function notCTExecutable() that can't be run at CT:

int aVerySlowCTFunction(int x) {
    int count;
    foreach(i; 0 .. x)
        foreach(j; 0 .. x)
            foreach(k; 0 .. x)
    return count;

class Foo {
    int bar(int y) { return y; }

int notCTExecutable(int y) {
    // not currently possible in CTFE
    return (new Foo()).bar(y);

int bad(int x) {
    auto y = aVerySlowCTFunction(x); // good
    return notCTExecutable(y); // bad

enum int z = bad(120);

void main() {}

Now the compiler runs aVerySlowCTFunction() and only later it finds that notCTExecutable() can't be run at compile-time and returns an error. This isn't a tidy design for CTFE (but it probably allows a simpler implementation of it). Is this going to be a problem in programs that use CTFE a lot? It looks even worse than Python dynamic typing, this seems dynamic compilation :-)


More information about the Digitalmars-d-learn mailing list