I wrote following metaprogramming template:
template <unsigned int N, unsigned int P>
struct cutom_imagined
{
static unsigned int function(unsigned int r)
{
return (P + N + r) * cutom_imagined<N - 1>::function(r);
}
};
P is actually like a constant. How should I write last recursion for above example? I suppose it should look similar to this one:
template <>
struct cutom_imagined<0, /* What should be here? */ >
{
static unsigned int function(unsigned int) { return 1; }
};
But don't know how to write it...
Make P
part of the template and the specialization. First off, the recursive call is:
return (P + N + r) * cutom_imagined<N - 1, P>::function(r);
Second, the specialziation is now partial:
template <unsigned int P>
struct cutom_imagined<0, P>
{
static unsigned int function(unsigned int) { return 1; }
};
If your compiler supports constexpr
you can also do it this like:
constexpr unsigned int func(unsigned int r, unsigned int p, unsigned int n)
{
return (n == 0) ? 1 : ((p + n + r) * func(r, p, n - 1));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.