如何在c++中编写元编程模板的最后递归
How to write last recursion for meta-programming template in c++
我编写了以下元编程模板:
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实际上就像一个常数。对于上面的例子,我应该如何写最后一个递归?我想应该和下面这个差不多:
template <>
struct cutom_imagined<0, /* What should be here? */ >
{
static unsigned int function(unsigned int) { return 1; }
};
但是不知道怎么写
使P
成为模板和专门化的一部分。首先,递归调用是:
return (P + N + r) * cutom_imagined<N - 1, P>::function(r);
第二,专门化现在是局部的:
template <unsigned int P>
struct cutom_imagined<0, P>
{
static unsigned int function(unsigned int) { return 1; }
};
如果你的编译器支持constexpr
,你也可以这样做:
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));
}
相关文章:
- 通过递归进行因子分解
- 递归函数计算序列中的平方和(并输出过程)
- 使用递归的数组的最小值.这是怎么回事
- 递归列出所有目录中的C++与Python与Ruby的性能
- 返回不停止函数,递归函数问题?(编程练习,动态规划,Levenshtein 回溯)
- 如何使用 Boost Hana 删除元编程递归
- 使用元编程递归初始化 std::array
- 递归模板参数包编程
- 使用递归编程布尔计划
- 双递归动态编程
- 使用递归编程的树
- 模板元编程递归上限
- 元编程和SFINAE/std::enable_if:无限模板递归
- C++元编程递归步长限制
- 递归解决方案的动态编程解决方案
- 模板元编程:原始递归
- 如何在c++中编写元编程模板的最后递归
- 在TBB中使用递归、基于任务的并行编程获得不同的输出
- 形成递归函数C++编程
- 模板元编程的尾递归性能