nos*_*bor 1 c++ recursion templates metaprogramming template-meta-programming
我写了以下元编程模板:
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);
}
};
Run Code Online (Sandbox Code Playgroud)
P实际上就像一个常数.我应该如何编写上述示例的最后一次递归?我想它看起来应该与这个相似:
template <>
struct cutom_imagined<0, /* What should be here? */ >
{
static unsigned int function(unsigned int) { return 1; }
};
Run Code Online (Sandbox Code Playgroud)
但不知道怎么写它......
制作P模板和专业化的一部分.首先,递归调用是:
return (P + N + r) * cutom_imagined<N - 1, P>::function(r);
Run Code Online (Sandbox Code Playgroud)
其次,特殊化现在是部分的:
template <unsigned int P>
struct cutom_imagined<0, P>
{
static unsigned int function(unsigned int) { return 1; }
};
Run Code Online (Sandbox Code Playgroud)