FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Compile-time_function_execution

Compile-time function execution

Contents
    D
    Zig
References

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.

importstd; constexprunsignedintfactorial(unsignedintn)noexcept{ returnn?(n*factorial(n-1)):1; } constexprunsignedintf10=factorial(10); intmain(){ std::println("{}",f10); return0; }

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

1) {\n result *= n--;\n }\n return result;\n}\n\nint main() {\n constexpr int f4 = factorial(4); // f4 == 24\n}\n"}}'>// Iterative factorial at compile time. constexprunsignedintfactorial(unsignedintn)noexcept{ unsignedintresult=1; while(n>1){ result*=n--; } returnresult; } intmain(){ constexprintf4=factorial(4);// f4 == 24 }

In C++20, immediate functions were introduced with the voidassertionFailed(){} constevalvoidtestAssert(boolcond){ if(!cond){ assertionFailed(); } } constevalvoidtest(){ intx=10; testAssert(x==10);// ok x++; testAssert(x==11);// ok x--; testAssert(x==12);// fails here } intmain(){ 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:

Infunction'intmain()': in'constexpr'expansionof'CheckedMessage("Hello, world!")' error:calltonon-'constexpr'function'voidassertionFailed()' assertionFailed(); ~~~~~~~~~~~~~~~^~ [...]

D

edit

This is an example of compile-time function evaluation in the D programming language:

constfn.

i32 {\n a + b\n}\n\nconst RESULT: i32 = add(10, 5);\n\nfn main() {\n // The value 15 is already baked into the binary here\n println!(\"The result is: {}\", RESULT);\n}\n"}}'>constfnadd(a:i32,b:i32)->i32{ a+b } constRESULT:i32=add(10,5); fnmain(){ // 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.

fnList(comptimeT:type)type{ returnstruct{ items:[]T, len:usize, }; } // The generic List data structure can be instantiated by passing in a type: varbuffer:[10]i32=undefined; varlist=List(i32){ .items=&buffer, .len=0, };

References

edit
cppreference.com (10 May 2026). "std::format". cppreference.com. cppreference.com.
  • ↑ D 2.0 language specification: Functions
  • ↑ D 2.0 language specification: Attributes
  • ↑

  • View original on Wikipedia | Back | FazBrowse Home