求问c++中enum的一个奇怪用法解决办法
求问c++中enum的一个奇怪用法
早上刚请教了元编程,模版特化等
就看了这个例子,但是其中的enum怎么解释啊?
经过测试
把程序改写成如下,也没问题
------解决思路----------------------
原理是一样的,只不过这里的枚举时嵌套的匿名的,只能通过其所在外部类去引用啊
------解决思路----------------------
enum 本来也就是定义常量的一种方法
template <int N>
struct Factorial
{
enum { value = N * Factorial<N - 1>::value };
};
template <>
struct Factorial<0>
{
enum { value = 1 };
};
// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
int x = Factorial<4>::value; // == 24
int y = Factorial<0>::value; // == 1
}
早上刚请教了元编程,模版特化等
就看了这个例子,但是其中的enum怎么解释啊?
经过测试
把程序改写成如下,也没问题
template <int N>
struct Factorial
{
static const int value = N * Factorial<N - 1>::value ;
};
template <>
struct Factorial<0>
{
static const int value = 1;
};
// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
int x = Factorial<4>::value; // == 24
int y = Factorial<0>::value; // == 1
}
------解决思路----------------------
原理是一样的,只不过这里的枚举时嵌套的匿名的,只能通过其所在外部类去引用啊
------解决思路----------------------
enum 本来也就是定义常量的一种方法