Compile time function execution

Compile time function execution

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.

Example

In C++, template metaprogramming is often used to compute values at compile time, such as:

template <int N> struct Factorial {
    enum {
        value = N * Factorial<N - 1>::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[1]):

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.[2]

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]

References

  1. ^ D 1.0 language specification: Functions
  2. ^ D 1.0 language specification: Attributes

Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Just-in-time compilation — In computing, just in time compilation (JIT), also known as dynamic translation, is a method to improve the runtime performance of computer programs. Historically, computer programs had two modes of runtime operation, either interpreted or static …   Wikipedia

  • Run-time system — In computer science, the runtime system is software that provides services for a running program but is itself not considered to be part of the operating system.Fact|date=April 2008 Examples include:* the code that is generated by the compiler to …   Wikipedia

  • D (programming language) — For other programming languages named D, see D (disambiguation)#Computing. D programming language Paradigm(s) multi paradigm: imperative, object oriented, functional, meta Appeared in 1999 (1999) Designed by …   Wikipedia

  • Chlorotrifluoroethylene — Chlorotrifluoroethylene[1] …   Wikipedia

  • Type system — Type systems Type safety Inferred vs. Manifest Dynamic vs. Static Strong vs. Weak Nominal vs. Structural Dependent typing Duck typing Latent typing Linear typing Uniqueness typing …   Wikipedia

  • 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… …   Wikipedia

  • Burroughs MCP — Master Control Program redirects here. For the fictitious computer program villain, see Master Control Program (Tron). MCP Company / developer Burroughs / Unisys Programmed in ESPOL, NEWP OS family Not Applicable Working state Current …   Wikipedia

  • C++0x — is the planned new standard for the C++ programming language. It is intended to replace the existing C++ standard, ISO/IEC 14882, which was published in 1998 and updated in 2003. These predecessors are informally known as C++98 and C++03. The new …   Wikipedia

  • C++11 — C++11, also formerly known as C++0x,[1] is the name of the most recent iteration of the C++ programming language, replacing C++TR1, approved by the ISO as of 12 August 2011.[2] The name is derived from the tradition of naming language versions by …   Wikipedia

  • Common Lisp — Paradigm(s) Multi paradigm: procedural, functional, object oriented, meta, reflective, generic Appeared in 1984, 1994 for ANSI Common Lisp Developer ANSI X3J13 committee Typing discipline …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”