Meta tags:
Headings (most frequently used words):
compile, time, function, execution, contents, examples, references, external, links, lisp, rust, zig,
Text of the page (most frequently used words):
the (52), time (40), #compile (38), #function (30), factorial (28), int (22), result (20), main (15), this (14), constexpr (14), and (13), analysis (12), evaluation (12), edit (12), code (11), example (10), value (10), for (10), return (10), void (10), unsigned (10), loop (9), const (9), execution (8), constant (8), assertionfailed (8), wikipedia (7), with (7), compiler (7), zig (7), std (7), used (7), consteval (7), using (6), use (6), data (6), elimination (6), language (6), rust (6), i32 (6), usize (6), would (6), that (6), enum (6), uint (6), msg (6), page (5), expressions (5), functions (5), comptime (5), arguments (5), testassert (5), languages (4), contents (4), search (4), was (4), optimization (4), expansion (4), examples (4), links (4), general (4), metaprogramming (4), list (4), var (4), can (4), struct (4), ctfe (4), create (4), pub (4), which (4), run (4), must (4), here (4), checkedmessage (4), compilation (4), immediate (4), known (4), template (4), hide (4), move (4), sidebar (4), add (3), toggle (3), view (3), available (3), may (3), non (3), 2026 (3), articles (3), from (3), flow (3), checking (3), other (3), global (3), call (3), variable (3), reference (3), cppreference (3), com (3), pdf (3), system (3), type (3), evaluated (3), computed (3), able (3), println (3), are (3), error (3), hello (3), world (3), fails (3), sendcalmmessage (3), test (3), noexcept (3), were (3), article (3), lisp (3), some (3), tools (3), table (2), contact (2), about (2), privacy (2), policy (2), terms (2), july (2), categories (2), short (2), description (2), wikidata (2), optimizations (2), retrieved (2), org (2), partial (2), jump (2), expression (2), dead (2), instruction (2), numbering (2), uses (2), induction (2), automatic (2), local (2), external (2), parameters (2), team (2), april (2), specification (2), format (2), 2010 (2), programming (2), computing (2), vandevoorde (2), 2003 (2), references (2), len (2), buffer (2), items (2), generic (2), structures (2), specifies (2), valid (2), called (2), typically (2), tells (2), initializer (2), variables (2), note (2), resolved (2), well (2), factorials (2), genfactorials (2), version (2), message (2), arg (2), string_view (2), import (2), not (2), cond (2), without (2), while (2), iterative (2), more (2), f10 (2), compute (2), what (2), early (2), execute (2), any (2), appearance (2), upload (2), file (2), changes (2), history (2), read (2), log (2), account (2), donate (2), menu (2), topic, mobile, cookie, statement, statistics, developers, conduct, legal, safety, contacts, disclaimers, text, under, additional, apply, site, you, agree, registered, trademark, profit, organization, wikimedia, foundation, inc, creative, commons, attribution, sharealike, license, rendered, parsoid, last, edited, utc, hidden, different, construction, https, index, php, title, time_function_execution, oldid, 1363509369, range, shape, pointer, escape, dependence, control, array, access, alias, static, profile, guided, threading, inline, templates, bounds, interprocedural, tail, deforestation, functional, rematerialization, register, allocation, selection, scheduling, generation, sparse, conditional, propagation, based, ssa, reaching, definitions, define, chain, upwards, exposed, live, recognition, store, folding, common, subexpression, strength, reduction, software, pipelining, unswitching, unrolling, splitting, nest, interchange, inversion, invariant, motion, fusion, vectorization, parallelization, peephole, basic, block, rosettacode, various, docs, lang, attributes, gabriel, dos, reis, bjarne, stroustrup, march, sac, 25th, acm, symposium, applied, daveed, edison, design, group, 2015, reflective, undefined, structure, instantiated, passing, also, suppors, already, baked, into, binary, done, generate, strings, then, parsed, compiled, 39_916_800, 479_001_600, 120, 720, 5_040, 40_320, 362_880, 3_628_800, contains, foreach, new, populate, simple, way, ends_with, char, constructors, argument, typical, display, because, invoked, usable, words, stops, after, failed, assertion, bool, since, marked, guaranteed, invoke, being, forced, another, manifestly, context, hence, usage, offers, wide, instance, introduced, keyword, made, accessible, flexible, relaxed, restrictions, technique, allowing, declarations, conditionals, loops, restriction, all, required, remains, relaxes, constraints, generalized, similar, one, write, foo, earlier, versions, often, values, such, following, historically, named, integral, constants, macros, before, addition, preprocessor, enums, metacode, extension, experimental, allow, injection, improved, syntax, user, defined, same, macro, only, still, perform, level, possibly, producing, optimized, than, ability, normally, possible, does, make, attempt, modify, state, pure, machine, feature, compilers, free, encyclopedia, item, projects, printable, download, print, export, switch, legacy, parser, get, shortened, url, cite, information, permanent, link, related, actions, english, talk, 한국어, فارسی, subsection, top, personal, special, pages, recent, community, portal, learn, help, contribute, random, current, events, navigation, content,
Text of the page (random words):
compile time function execution wikipedia jump to content main menu main menu move to sidebar hide navigation main page contents current events random article about wikipedia contact us contribute help learn to edit community portal recent changes upload file special pages search search appearance donate create account log in personal tools donate create account log in contents move to sidebar hide top 1 examples toggle examples subsection 1 1 lisp 1 2 c 1 3 d 1 4 rust 1 5 zig 2 references 3 external links toggle the table of contents compile time function execution 2 languages فارسی 한국어 edit links article talk english read edit view history tools tools move to sidebar hide actions read edit view history general what links here related changes upload file permanent link page information cite this page get shortened url switch to legacy parser print export download as pdf printable version in other projects wikidata item appearance move to sidebar hide from wikipedia the free encyclopedia feature of some compilers 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 examples edit lisp edit the lisp macro system is an early example of the use of compile time evaluation of user defined functions in the same language c edit main article consteval the metacode extension to c vandevoorde 2003 1 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 the following in c and c enums were historically used to create compile time named integral constants without preprocessor macros before the addition of constexpr in c 11 template unsigned int n struct factorial enum value n factorial n 1 value template struct factorial 0 enum value 1 void foo int x factorial 0 value 1 int y factorial 4 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 constexpr import std constexpr unsigned int factorial unsigned int n noexcept return n n factorial n 1 1 constexpr unsigned int f10 factorial 10 int main std println f10 return 0 in c 11 this technique is known as generalized constant expressions constexpr 2 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 iterative factorial at compile time constexpr unsigned int factorial unsigned int n noexcept unsigned int result 1 while n 1 result n return result int main constexpr int f4 factorial 4 f4 24 in c 20 immediate functions were introduced with the consteval keyword and compile time function execution was made more accessible and flexible with relaxed constexpr restrictions iterative factorial at compile time consteval unsigned int factorial unsigned int n noexcept unsigned int result 1 while n 1 result n return result int main int f4 factorial 4 f4 24 since function factorial is marked consteval it is guaranteed to invoke at compile time without being forced in another manifestly constant evaluated context hence the usage of immediate functions offers wide uses in metaprogramming and compile time checking for instance this is used in std format 3 void assertionfailed consteval void testassert bool cond if cond assertionfailed consteval void test int x 10 testassert x 10 ok x testassert x 11 ok x testassert x 12 fails here int main test in this example the compilation fails because the immediate function invoked function which is not usable in constant expressions in other words the compilation stops after failed assertion the typical compilation error message would display in function int main in constexpr expansion of test in constexpr expansion of testassert x 12 error call to non constexpr function assertionfailed assertionfailed this is an example of using immediate functions as constructors for compile time argument checking import std using std string_view void assertionfailed struct checkedmessage string_view msg consteval checkedmessage const char msg msg msg if msg ends_with assertionfailed void sendcalmmessage const checkedmessage arg std println arg msg int main sendcalmmessage hello world sendcalmmessage hello world the compilation fails here with the message in function int main in constexpr expansion of checkedmessage hello world error call to non constexpr function void assertionfailed assertionfailed d edit this is an example of compile time function evaluation in the d programming language 4 uint factorial uint n if n 0 return 1 return n factorial n 1 computed at compile time enum y factorial 0 1 enum x factorial 4 24 this example specifies a valid d function called factorial which would typically be evaluated at run time the use of enum tells the compiler that the initializer for the variables must be computed at compile time note that the arguments to the function must be able to be resolved at compile time as well 5 ctfe can be used to populate data structures at compile time in a simple way d version 2 uint genfactorials uint n uint result new uint 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 ctfe can be used to generate strings which are then parsed and compiled as d code in d rust edit this is an example of compile time function evaluation in rust this is done using const fn 6 const fn add a i32 b i32 i32 a b const result i32 add 10 5 fn main the value 15 is already baked into the binary here println the result is result zig edit this is an example of compile time function evaluation in zig 7 pub fn factorial n usize usize var result 1 for 1 n 1 i result i return result pub fn main void const x comptime factorial 0 0 const y comptime factorial 4 24 this example specifies a valid zig function called factorial which would typically be evaluated at run time the use of comptime tells the compiler that the initializer for the variables must be computed at compile time note that the arguments to the function must be able to be resolved at compile time as well zig also suppors compile time parameters 8 pub fn factorial comptime n usize usize var result usize 1 for 1 n 1 i result i return result pub fn main void const x factorial 0 0 const y factorial 4 24 ctfe can be used to create generic data structures at compile time fn list comptime t type type return struct items t len usize the generic list data structure can be instantiated by passing in a type var buffer 10 i32 undefined var list list i32 items buffer len 0 references edit daveed vandevoorde edison design group april 18 2003 reflective metaprogramming in c pdf retrieved july 19 2015 gabriel dos reis and bjarne stroustrup march 2010 general constant expressions for system programming languages sac 2010 the 25th acm symposium on applied computing pdf cppreference com 10 may 2026 std format cppreference com cppreference com d 2 0 language specification functions d 2 0 language specification attributes the rust team 16 april 2026 constant evaluation docs rust lang org the rust team zig 0 11 0 language reference compile time expressions zig 0 11 0 language reference compile time parameters external links edit rosettacode examples of compile time function evaluation in various languages v t e compiler optimizations basic block peephole optimization local value numbering loop automatic parallelization automatic vectorization induction variable loop fusion loop invariant code motion loop inversion loop interchange loop nest optimization loop splitting loop unrolling loop unswitching software pipelining strength reduction data flow analysis available expression common subexpression elimination constant folding dead store elimination induction variable recognition and elimination live variable analysis upwards exposed uses use define chain reaching definitions ssa based global value numbering sparse conditional constant propagation code generation instruction scheduling instruction selection register allocation rematerialization functional deforestation tail call elimination global interprocedural optimization other bounds checking elimination compile time function execution dead code elimination expression templates inline expansion jump threading partial evaluation profile guided optimization static analysis alias analysis array access analysis control flow analysis data flow analysis dependence analysis escape analysis pointer analysis shape analysis value range analysis retrieved from https en wikipedia org w index php title compile time_function_execution oldid 1363509369 categories compiler construction compiler optimizations hidden categories articles with short description short description is different from wikidata articles with example c code articles with example d code this page was last edited on 10 july 2026 at 17 14 utc page was rendered with parsoid text is available under the creative commons attribution sharealike 4 0 license additional terms may apply by using this site you agree to the terms of use and privacy policy wikipedia is a registered trademark of the wikimedia foundation inc a non profit organization privacy policy about wikipedia disclaimers contact wikipedia legal safety contacts code of conduct developers statistics cookie statement mobile view search search toggle the table of contents compile time function execution 2 languages add topic
|