Compile-time function execution: Difference between revisions

Content deleted Content added
Added a second more meaningful example
different name and acronym added
Line 1:
'''Compile time function execution''' (or Compile-Time Function Evaluation, CTFE) is the ability of a [[compiler]], that would normally compile a function to machine code and execute it at run time, to execute the function at compile time. This is possible if the arguments to the function are known at compile time, and the function does not make any reference to or attempt to modify any global state (is a [[pure function]]).
 
Even if the value of only some of the arguments are known, the compiler may still be able to perform some level of compile time function execution ([[partial evaluation]]), possibly producing more optimized code than if no arguments were known.
Line 42:
The use of <code>const</code> tells the compiler that the initializer for the variables must be computed at compile time<ref>[http://www.digitalmars.com/d/1.0/attribute.html#const D 1.0 language specification: Attributes]</ref>.
 
Such Compile-Time Function Evaluation (CTFE) can be used to populate simple data structures at compile-time in a simple way (D 1 code, the <code>~</code> operator concatenates arrays and items to arrays):
 
<source lang="D">