Now I am aware of how the following works:
template<std::uint8_t num>
struct factorial
{
static_assert(num <= 20, "Integer overflow!!");
enum { value = num * factorial<num - 1>::value };
};
template <>
struct factorial<0>
{
enum { value = 1 };
};
It computes the factorial of a number using recursion at compile time!
You would call this function like so: std::cout << factorial<20>::value;
Beautiful, but an enum can only hold integral types (up to std::uintmax_t). That means we cannot compute the factorial of a number greater than 20.
Now I have written a nice little bigint class, which removes the boundaries.
Consider the following:
template<std::uintmax_t num>
struct factorial
{
static bigint value()
{
bigint result("1");
for (bigint i("2"); i <= num; ++i)
result *= i;
return result;
}
};
This would need me to call it like so: std::cout << factorial<99>::value();. Obviously... since it's a function.
My question is: is there a way to modify the code/function so it would be called like the first example, but still make use of the bigint class, which I can't stick in an enum.
Aucun commentaire:
Enregistrer un commentaire