Why CTFE is context-sensitive?

Simen Kjærås simen.kjaras at gmail.com
Sun Jan 26 17:34:02 PST 2014


On 2014-01-26 09:59, Pierre Talbot wrote:> Hi,
 >
 > I was wondering why CTFE is context sensitive, why don't we check
 > every expressions and run the CTFE if it applies?

Mostly because it's not necessary, and takes more time than simply 
compiling it. For an optimization step, opportunistic CTFE would 
certainly be possible (it is, after all, simply constant folding on 
steroids).

The situations in which CTFE is attempted today are when it's explicitly 
required - either because the value is used at compile time, or because 
it's assigned to a manifest constant or static variable (which are 
defined to be required to be known at compile time).

This means the compiler does not need to spend inordinate amounts of 
time testing to see if the code compiles with CTFE, and that it can give 
sensible error messages when something cannot be CTFE'd. If instead 
opportunistic CTFE were the norm, you would need to disassemble your 
program to see if the compiler did in fact CTFE the code that you think 
it should.

--
   Simen


More information about the Digitalmars-d-learn mailing list