Compile-time function execution: Difference between revisions

Content deleted Content added
Remove {{DEFAULTSORT:Compile Time Function Execution}}
c++11 version is not as powerful. added missing hyphens, removed incorrect hyphens.
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 [[Runrun time (program lifecycle phase)|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.
 
==Example==
 
In earlier versions of [[C++]], [[template metaprogramming]] is often used to compute values at compile time, such as:
 
Line 26 ⟶ 25:
</source>
 
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://d-programming-language.org/function.html#interpretation D 2.0 language specification: Functions]</ref>
 
Line 62:
</source>
 
In [[C++11]], thea equivalentsimilar (less powerful) technique is known as [[C++11#constexpr – Generalized constant expressions|Generalizedgeneralized constant expressions]].
 
== References ==
<references/>