Content deleted Content added
Alon Alush (talk | contribs) m v2.05b - Fix errors for CW project (Reference before punctuation) |
No edit summary |
||
Line 1:
{{Short description|A compiler's ability to execute a function at compile time rather than runtime}}
In [[computing]], '''compile-time function execution''' (or '''compile
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.
Line 14:
In earlier versions of [[C++]], [[template metaprogramming]] is often used to compute values at compile time, such as:
<syntaxhighlight lang="
template <int N>
struct Factorial {
Line 35:
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.
<syntaxhighlight lang="
#include <cstdio>
Line 50:
In [[C++11]], this technique is known as [[C++11#constexpr – Generalized constant expressions|generalized constant expressions]] (<code>constexpr</code>).<ref>{{cite web|url=http://www.stroustrup.com/sac10-constexpr.pdf|author=Gabriel Dos Reis and Bjarne Stroustrup | title=General Constant Expressions for System Programming Languages. SAC-2010. The 25th ACM Symposium On Applied Computing. | date=March 2010}}</ref> [[C++14]] [[C++14#Relaxed constexpr restrictions|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).
Here's an example of compile
<syntaxhighlight lang="
// Iterative factorial at compile time.
constexpr int Factorial(int n) {
Line 70:
In [[C++20]], immediate functions were introduced, and compile-time function execution was made more accessible and flexible with relaxed <code>constexpr</code> restrictions.
<syntaxhighlight lang="
// Iterative factorial at compile time.
consteval int Factorial(int n) {
Line 88:
Here's an example of using immediate functions in compile-time function execution:
<syntaxhighlight lang="
void you_see_this_error_because_assertion_fails() {}
Line 111:
The typical compilation error message would display:
<syntaxhighlight lang="
In function 'int main()':
in 'constexpr' expansion of 'test()'
Line 122:
Here's another example of using immediate functions as constructors which enables compile-time argument checking:
<syntaxhighlight lang="
#include <string_view>
#include <iostream>
Line 149:
The compilation fails here with the message:
<syntaxhighlight lang="
In function 'int main()':
in 'constexpr' expansion of 'checked_message(((const char*)"Hello, world!"))'
Line 159:
===D===
Here's an example of compile
<syntaxhighlight lang="
int factorial(int n) {
if (n == 0)
Line 177:
CTFE can be used to populate data structures at compile-time in a simple way (D version 2):
<syntaxhighlight lang="
int[] genFactorials(int n) { auto result = new int[n];
result[0] = 1;
Line 197 ⟶ 198:
===Zig===
Here's an example of compile
<syntaxhighlight lang="
pub fn factorial(n: usize) usize {
var result = 1;
Line 218 ⟶ 219:
Zig also support Compile-Time Parameters.<ref>[https://ziglang.org/documentation/0.11.0/#Compile-Time-Parameters Zig 0.11.0 Language Reference: Compile-Time Parameters]</ref>
<syntaxhighlight lang="
pub fn factorial(comptime n: usize) usize {
var result: usize = 1;
Line 235 ⟶ 236:
CTFE can be used to create generic data structures at compile-time:
<syntaxhighlight lang="
fn List(comptime T: type) type {
return struct {
Line 256 ⟶ 257:
==External links==
* [http://rosettacode.org/wiki/Compile-time_calculation Rosettacode examples of compile-time function evaluation in various languages]
{{Compiler optimizations}}
[[Category:Compiler construction]]
[[Category:Articles with example C++ code]]
[[Category:Articles with example D code]]
[[Category:Compiler optimizations]]
|