是否可以在专用模板类中访问非类型模板参数的值?
如果我有专门的模板类:
template <int major, int minor> struct A {
void f() { cout << major << endl; }
}
template <> struct A<4,0> {
void f() { cout << ??? << endl; }
}
我知道在上面的情况下,硬编码值 4 和 0 而不是使用变量很简单,但是我有一个更大的类,我正在专门研究它,我希望能够访问这些值。
是否可以在 A<4,0> 中访问major
和minor
值(4 和 0)?还是我必须在模板实例化时将它们分配为常量:
template <> struct A<4,0> {
static const int major = 4;
static const int minor = 0;
...
}