跟踪专用模板对象的枚举成员值
Tracing a specialized template object's enum member value
我有一个结构模板,其枚举值递归定义如下:
template <unsigned x, unsigned y>
struct Special
{
enum { value = x * Special<x-1, y-1> :: value; }
};
template <unsigned n>
struct Special<n, 0>
{
enum { value = 1; }
};
我想知道 value 的逐渐增长对于 Special 的实例,例如:
Special <8, 3> spl;
因此,我在通用结构定义中插入了以下构造函数:
Special() { cout << "Value, x and y are: " << value << "," << x << "," << y;}
但是,我只将最终值打印为 336、8、3。如何获得 y 从 3 逐渐减少到 0 的全貌?
But, I only got the final values printed as 336, 8, 3. How do I get the full picture of gradual reduction in y from 3 to 0?
您没有得到预期的输出,因为 classes 本身不是从自身递归派生的。当您构造 Special<8,3>
时,它不是从 Special<7,2>
派生的。因此,当您构造 Special<8,3>
.
时,不会调用 Special<7,2>
的构造函数
解决方案一:将class改为继承自下一个递归class
#include <iostream>
template <unsigned x, unsigned y> struct Special;
template <unsigned x, unsigned y>
struct Special : Special<x-1, y-1>
{
Special()
{
std::cout << "Value, x and y are: " << value << "," << x << "," << y << std::endl;
}
enum { value = x * Special<x-1, y-1> :: value };
};
template <unsigned n>
struct Special<n, 0>
{
Special()
{
std::cout << "Value, x and y are: " << value << "," << n << "," << 0 << std::endl;
}
enum { value = 1 };
};
int main()
{
Special<8,3> s;
}
解决方案 2:使用递归函数而不是枚举来获取值
获得所需输出的另一种方法是通过递归函数调用而不是 class 中的 enum
并让代码在函数中生成输出。
#include <iostream>
template <unsigned x, unsigned y>
struct Special
{
Special() { getValue(); }
static int getValue()
{
int value = x*Special<x-1, y-1>::getValue();
std::cout << "Value, x and y are: " << value << "," << x << "," << y << std::endl;
return value;
}
};
template <unsigned n>
struct Special<n, 0>
{
static int getValue()
{
int value = 1;
std::cout << "Value, x and y are: " << value << "," << n << "," << 0 << std::endl;
return value;
}
};
int main()
{
Special<8,3> s;
}
两种情况下的输出:
Value, x and y are: 1,5,0
Value, x and y are: 6,6,1
Value, x and y are: 42,7,2
Value, x and y are: 336,8,3
我有一个结构模板,其枚举值递归定义如下:
template <unsigned x, unsigned y>
struct Special
{
enum { value = x * Special<x-1, y-1> :: value; }
};
template <unsigned n>
struct Special<n, 0>
{
enum { value = 1; }
};
我想知道 value 的逐渐增长对于 Special 的实例,例如:
Special <8, 3> spl;
因此,我在通用结构定义中插入了以下构造函数:
Special() { cout << "Value, x and y are: " << value << "," << x << "," << y;}
但是,我只将最终值打印为 336、8、3。如何获得 y 从 3 逐渐减少到 0 的全貌?
But, I only got the final values printed as 336, 8, 3. How do I get the full picture of gradual reduction in y from 3 to 0?
您没有得到预期的输出,因为 classes 本身不是从自身递归派生的。当您构造 Special<8,3>
时,它不是从 Special<7,2>
派生的。因此,当您构造 Special<8,3>
.
Special<7,2>
的构造函数
解决方案一:将class改为继承自下一个递归class
#include <iostream>
template <unsigned x, unsigned y> struct Special;
template <unsigned x, unsigned y>
struct Special : Special<x-1, y-1>
{
Special()
{
std::cout << "Value, x and y are: " << value << "," << x << "," << y << std::endl;
}
enum { value = x * Special<x-1, y-1> :: value };
};
template <unsigned n>
struct Special<n, 0>
{
Special()
{
std::cout << "Value, x and y are: " << value << "," << n << "," << 0 << std::endl;
}
enum { value = 1 };
};
int main()
{
Special<8,3> s;
}
解决方案 2:使用递归函数而不是枚举来获取值
获得所需输出的另一种方法是通过递归函数调用而不是 class 中的 enum
并让代码在函数中生成输出。
#include <iostream>
template <unsigned x, unsigned y>
struct Special
{
Special() { getValue(); }
static int getValue()
{
int value = x*Special<x-1, y-1>::getValue();
std::cout << "Value, x and y are: " << value << "," << x << "," << y << std::endl;
return value;
}
};
template <unsigned n>
struct Special<n, 0>
{
static int getValue()
{
int value = 1;
std::cout << "Value, x and y are: " << value << "," << n << "," << 0 << std::endl;
return value;
}
};
int main()
{
Special<8,3> s;
}
两种情况下的输出:
Value, x and y are: 1,5,0
Value, x and y are: 6,6,1
Value, x and y are: 42,7,2
Value, x and y are: 336,8,3