Compile time function execution
Encyclopedia
Compile time function execution (or compile-time function evaluation, CTFE) is the ability of a compiler
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:


template struct Factorial {
enum {
value = N * Factorial::value
};
};

template <> struct Factorial<0> {
enum { value = 1 };
};

// Factorial<4>::value

24
// Factorial<0>::value

1
void foo {
int x = Factorial<0>::value; //

1
int y = Factorial<4>::value; //

24
}


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):


int factorial(int n) {
if (n

0)
return 1;
return n * factorial(n - 1);
}

// computed at compile time
const int y = factorial(0); //

1
const int x = factorial(4); // 24


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):

int[] genFactorials(int n) {
auto result = new int[n];
result[0] = 1;
foreach (i; 1 .. n)
result[i] = result[i - 1] * i;
return result;
}

enum factorials = genFactorials(13);

void main {}

// 'factorials' contains at compile-time:
// [1, 1, 2, 6, 24, 120, 720, 5_040, 40_320, 362_880, 3_628_800,
// 39_916_800, 479_001_600]
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK