forcing compile time function execution

Artur Skawina art.08.09 at gmail.com
Tue Dec 27 12:03:27 PST 2011


On 12/27/11 20:31, Joshua Reusch wrote:
> Am 27.12.2011 18:59, Artur Skawina wrote:
>> Is there a way to *force* CTFE? Without using an extra wrapper like this:
>>
>> auto f_impl(alias a)() { / * ... ctfeable ... */ }
>> auto f(alias a)() { enum ctfed = f_impl!a; return ctfed; }
>>
>> A "@compile" (or "@ctfe" etc) function attribute would eliminate the need for the wrapper...
>>
>> artur
> 
> more general: is there any reason to not evaluate a pure function with const arguments always at compile-time ?

"Pure" does not necessarily == cheap. There will always be a limit to the amount of transformations a compiler can do; otherwise it could in many cases just run the program instead of compiling it. :)
So there has to be some kind of heuristic, to make sure builds don't take too long. And when the compiler gives up, it has to generate that "pure" code after all. This is not what you want if you know that something can be reasonably cheaply computed at compile time, but is not really needed in the resulting program and would slow it down significantly.
Hence "@ctfe", which would have to be the equivalent of the enum-assigning wrapper -- either the function evaluates to a constant, or the build fails.

artur


More information about the Digitalmars-d mailing list