On Monday, June 25, 2018 05:03:26 Mr.Bingo via Digitalmars-d-learn wrote: > The compiler should be easily able to figure out that foo(3) can > be precomputed(ctfe'ed) and do so. It can already do this, as you > say, by forcing enum on it. Why can't the compiler figure it out > directly?
The big problem with that is that determining whether the calculation can be done at compile time or not means solving the halting problem. In general, the only feasible way to do it would be to attempt it for every function call and then give up when something didn't work during CTFE, which would balloon compilation times and likely cause the compiler to run out of memory on a regular basis given how it currently manages memory and how CTFE tends to use a lot of memory. It was decided ages ago that the best approach to the problem was to use CTFE only when CTFE was actually required. If an expression is used in a context where its value must be known at compile time, then it's evaluated at compile time; otherwise, it isn't. The compiler never attempts CTFE as an optimization or because it thinks that it might be possible to evaluate the value at compile time. As things stand, it should be pretty trivial to be able to look at an expression and determine whether it's evaluated at compile time or not based on how it's used. If you're looking to have the compiler figure out when to do CTFE based on the fact that an expression could theoretically be evaluated at compile time, or because you want the compiler to optimize using CTFE, then you're going to be disappointed, because that's never how CTFE has worked, and I'd be _very_ surprised if it ever worked any differently. - Jonathan M Davis