Compile-time function execution: Difference between revisions

Content deleted Content added
Ciemny00 (talk | contribs)
Examples: Fixed bug in Factorial function - incorrect result of Factorial(0)
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 10:
In earlier versions of [[C++]], [[template metaprogramming]] is often used to compute values at compile time, such as:
 
<sourcesyntaxhighlight lang="CPP">
template <int N>
struct Factorial {
Line 27:
int y = Factorial<4>::value; // == 24
}
</syntaxhighlight>
</source>
 
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.
 
<sourcesyntaxhighlight lang="CPP">
#include <cstdio>
 
Line 42:
return 0;
}
</syntaxhighlight>
</source>
 
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).
Line 48:
Here's an example of compile time function evaluation in C++14:
 
<sourcesyntaxhighlight lang="CPP">
// Iterative factorial at compile time.
constexpr int Factorial(int n) {
Line 61:
constexpr int f4 = Factorial(4); // f4 == 24
}
</syntaxhighlight>
</source>
 
Here's an example of compile time function evaluation in the [[D programming language]]:<ref>[http://d-programming-language.org/function.html#interpretation D 2.0 language specification: Functions]</ref>
 
<sourcesyntaxhighlight lang="D">
int factorial(int n) {
if (n == 0)
Line 75:
enum y = factorial(0); // == 1
enum x = factorial(4); // == 24
</syntaxhighlight>
</source>
 
This example specifies a valid D function called "factorial" which would typically be evaluated at run time. The use of <code>enum</code> 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.<ref>[http://d-programming-language.org/attribute.html#const D 2.0 language specification: Attributes]</ref>
Line 81:
CTFE can be used to populate data structures at compile-time in a simple way (D version 2):
 
<sourcesyntaxhighlight lang="D">int[] genFactorials(int n) {
auto result = new int[n];
result[0] = 1;
Line 96:
// [1, 1, 2, 6, 24, 120, 720, 5_040, 40_320, 362_880, 3_628_800,
// 39_916_800, 479_001_600]
</syntaxhighlight>
</source>
 
CTFE can be used to generate strings which are then parsed and compiled as D code in D.