Compile time function execution
Encyclopedia
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.
, template metaprogramming
is often used to compute values at compile time, such as:
But with compile time function evaluation the code used to compute the factorial would be exactly the same as what one would write for run time evaluation (this example code is in the D programming language):
The use of
CTFE can be used to populate data structures at compile-time in a simple way (D version 2):
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...
, 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
Pure function
In computer programming, a function may be described as pure if both these statements about the function hold:# The function always evaluates the same result value given the same argument value...
).
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
Partial evaluation
In computing, partial evaluation is a technique for several different types of program optimization by specialization. The most straightforward application is to produce new programs which run faster than the originals while being guaranteed to behave in the same way...
), possibly producing more optimized code than if no arguments were known.
Example
In C++C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
, template metaprogramming
Template metaprogramming
Template metaprogramming is a metaprogramming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled. The output of these templates include compile-time constants, data structures, and...
is often used to compute values at compile time, such as:
But with compile time function evaluation the code used to compute the factorial would be exactly the same as what one would write for run time evaluation (this example code is in the D programming language):
The use of
const
tells the compiler that the initializer for the variables must be computed at compile time.CTFE can be used to populate data structures at compile-time in a simple way (D version 2):