Compile-time function execution: Difference between revisions

Content deleted Content added
No edit summary
Line 27:
 
Using compile time function evaluation, code used to compute the factorial would be exactly the same as what one would write for run time evaluation.
Here's an example of CTFE in the [[D programming language]]<ref>[http://www.digitalmars.com/d/1-programming-language.0org/function.html#interpretation D 12.0 language specification: Functions]</ref>:
 
<source lang="D">
Line 41:
</source>
 
This example specifies a valid D function called "factorial" which would typically be evaluated at run time. The use of <code>const</code> tells the compiler that the initializer for the variables must be computed at compile time. Note that the arguments to the function must be able to be resolved at compile time as well.<ref>[http://www.digitalmars.com/d/1-programming-language.0org/attribute.html#const D 12.0 language specification: Attributes]</ref>
 
CTFE can be used to populate data structures at compile-time in a simple way (D version 2):