Content deleted Content added
No edit summary |
CyberShadow (talk | contribs) m added syntax highlighting |
||
Line 7:
This example code is in the [[D programming language]]:
<source lang="D">
</source>
===Example 2===
Line 18 ⟶ 20:
In [[C++]], [[template metaprogramming]] is often used to compute values at compile time, such as:
<source lang="CPP">
template <int N>
struct Factorial
Line 38 ⟶ 40:
int y = Factorial<0>::value; // == 1
}
</
With compile time function evaluation, the code used to generate the factorial would be exactly the same as what one would write for run time evaluation (example is in D):
<source lang="D">
int factorial(int n)
{
Line 55 ⟶ 57:
static const int y = factorial(0); // == 0! == 1
}
</
The use of static const tells the compiler that the initializer for the variables must be computed at compile time.
|