Content deleted Content added
m linking |
OllivierRob (talk | contribs) |
||
Line 23:
What exactly "programming at compile-time" means can be illustrated with an example of a factorial function, which in non-template C++ can be written using recursion as follows:
<syntaxhighlight lang=cpp>
unsigned
return n == 0 ? 1 : n * factorial(n - 1);
}
Line 34:
By using template metaprogramming and template specialization to provide the ending condition for the recursion, the factorials used in the program—ignoring any factorial not used—can be calculated at compile time by this code:
<syntaxhighlight lang=cpp>
template <unsigned
struct factorial {
};
template <>
struct factorial<0> {
};
|