CTFE uses an interpreter that is embedded in the compiler - just as you would expect from an interpreted language such as Python. When you compile something like this:
bool not(bool arg) { return !arg; } void main() { enum compileTime = not(true);
The compiler goes through the usual stages of tokenization / lexing / parsing, etc. When the computeTime enum (or any other construct that requires a compilation time value) is encountered, it will try to evaluate what is on the right side of the expression. In the case of a constant, it does what you expect and saves the constant. If he encounters a function call, he will start the CTFE interpreter. In this example, he knows what arguments are, and what the operators in this function do, he goes step by step and interprets each of them. If it cannot interpret the statement at compile time, it will throw a compile time error. In this simple example, it simply negates the parameter, but CTFE is able to interpret the structures, classes, loops, and much more described here - http://dlang.org/function#interpretation .
Robert
source share