Concept

Compile-time function execution

In computing, compile-time function execution (or compile time function evaluation, or general constant expressions) 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 (i.e. it is a pure function). 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. The Lisp macro system is an early example of the use of compile-time evaluation of user-defined functions in the same language. The Metacode extension to C++ (Vandevoorde 2003) was an early experimental system to allow compile-time function evaluation (CTFE) and code injection as an improved syntax for C++ template metaprogramming. In earlier versions of C++, template metaprogramming is often used to compute values at compile time, such as: template struct Factorial { enum { value = N * Factorial::value }; }; template struct Factorial { enum { value = 1 }; }; // Factorial::value == 24 // Factorial::value == 1 void Foo() { int x = Factorial::value; // == 1 int y = Factorial::value; // == 24 } Using compile-time function evaluation, code used to compute the factorial would be similar to what one would write for run-time evaluation e.g. using C++11 constexpr. #include constexpr int Factorial(int n) { return n ? (n * Factorial(n - 1)) : 1; } constexpr int f10 = Factorial(10); int main() { printf("%d\n", f10); return 0; } In C++11, this technique is known as generalized constant expressions (constexpr). C++14 relaxes the constraints on constexpr – allowing local declarations and use of conditionals and loops (the general restriction that all data required for the execution be available at compile-time remains).

About this result
This page is automatically generated and may contain information that is not correct, complete, up-to-date, or relevant to your search query. The same applies to every other page on this website. Please make sure to verify the information with EPFL's official sources.

Graph Chatbot

Chat with Graph Search

Ask any question about EPFL courses, lectures, exercises, research, news, etc. or try the example questions below.

DISCLAIMER: The Graph Chatbot is not programmed to provide explicit or categorical answers to your questions. Rather, it transforms your questions into API requests that are distributed across the various IT services officially administered by EPFL. Its purpose is solely to collect and recommend relevant references to content that you can explore to help you answer your questions.