C++ C++;元编程求值
这是我目前的代码:C++ C++;元编程求值,c++,recursion,template-meta-programming,C++,Recursion,Template Meta Programming,这是我目前的代码: template<int n> struct N{ static const int k = (n >= 2) ? (1 + N<n-2>::k) : N<0>::k; }; template<> struct N<0>{ static const int k = 0; }; 模板 结构{ 静态常量int k=(n>=2)?(1+n::k):n::k; }; 样板 结构{ 静态常数int k
template<int n>
struct N{
static const int k = (n >= 2) ? (1 + N<n-2>::k) : N<0>::k;
};
template<>
struct N<0>{
static const int k = 0;
};
模板
结构{
静态常量int k=(n>=2)?(1+n::k):n::k;
};
样板
结构{
静态常数int k=0;
};
汇编如下:
int main(int, char *[])
{
cout << N<2>::k;
getchar();
}
intmain(int,char*[])
{
要使此示例起作用,请执行以下操作:
template<int n>
struct N{
static const int k = 1 + N<n-2>::k;
};
template<>
struct N<1>{
static const int k = 0;
};
template<>
struct N<0>{
static const int k = 0;
};
模板
结构{
静态常数int k=1+N::k;
};
样板
结构{
静态常数int k=0;
};
样板
结构{
静态常数int k=0;
};
问题在于N
。当N=1
时,这将导致无限的模板实例化深度。因此,这只能部分回答问题,但我认为您不能强制对模板进行惰性评估。要使此示例起作用,请执行以下操作:
template<int n>
struct N{
static const int k = 1 + N<n-2>::k;
};
template<>
struct N<1>{
static const int k = 0;
};
template<>
struct N<0>{
static const int k = 0;
};
模板
结构{
静态常数int k=1+N::k;
};
样板
结构{
静态常数int k=0;
};
样板
结构{
静态常数int k=0;
};
问题在于N
。当N=1
时,这将导致无限的模板实例化深度。因此,这只能部分回答这个问题,但我认为您不能强制对模板进行惰性评估。如果要使用模板专门化来实现这一点,我将始终使用这一点:
template<int n>
struct N{
static const int k = 1 + N<n-2>::k;
};
template<>
struct N<1>{
static const int k = 0;
};
template<>
struct N<0>{
static const int k = 0;
};
如果要使用模板专门化来实现这一点,我将始终使用:
template<int n>
struct N{
static const int k = 1 + N<n-2>::k;
};
template<>
struct N<1>{
static const int k = 0;
};
template<>
struct N<0>{
static const int k = 0;
};
这似乎是可行的,并且可以很容易地推广到需要用惰性计算结构替换三元运算符?
的情况:
#include <iostream>
template<int n, bool = (n >= 2)>
struct N;
template<>
struct N<0, false>{
static const int k = 0;
};
template<int n>
struct N<n, true> {
static const int k = 1 + N<n-2>::k;
};
template<int n>
struct N<n, false> {
static const int k = N<0>::k;
};
int main() {
std::cout << N<2>::k << '\n';
std::cout << N<1>::k << '\n';
}
#包括
模板=2)>
结构N;
样板
结构{
静态常数int k=0;
};
样板
结构{
静态常数int k=1+N::k;
};
样板
结构{
静态常数int k=N::k;
};
int main(){
std::cout这似乎有效,并且可以很容易地推广到需要用惰性计算结构替换三元运算符?
的情况:
#include <iostream>
template<int n, bool = (n >= 2)>
struct N;
template<>
struct N<0, false>{
static const int k = 0;
};
template<int n>
struct N<n, true> {
static const int k = 1 + N<n-2>::k;
};
template<int n>
struct N<n, false> {
static const int k = N<0>::k;
};
int main() {
std::cout << N<2>::k << '\n';
std::cout << N<1>::k << '\n';
}
#包括
模板=2)>
结构N;
样板
结构{
静态常数int k=0;
};
样板
结构{
静态常数int k=1+N::k;
};
样板
结构{
静态常数int k=N::k;
};
int main(){
std::cout编译错误是什么?错误是:错误C1202:递归类型或函数依赖关系上下文太复杂。我希望if语句只计算所需的分支。简单:添加基本大小写N,因为奇数不会达到N。编译错误是什么?错误是:错误C1202:递归类型或函数依赖关系上下文太复杂。我希望if语句只计算所需的分支。简单:添加基本情况N,因为奇数不会达到N。关于延迟计算:可以增强帮助吗?不确定。我不知道任何Boost延迟计算原语。可能是延迟计算(或者可能是我的误解))我的实现在这里:关于延迟求值:可以Boost帮助吗?老实说,我不知道有任何Boost延迟求值原语。可能是延迟求值(或者可能是我的误解),但对于函数的价值来说,代码的长度将是无法忍受的。)我的实现在这里::)+1即使作为模板,它也可以用相同的公式简化!@DavidRodríguez dribeas:这一点很好(特别是如果您的编译器不支持constepr
).:)+1即使将其作为模板,也可以使用相同的公式进行简化!@DavidRodríguez dribeas:这一点很好(特别是如果您的编译器不支持constexpr
)。