我有这样的情况:
template<unsigned int N>
class Base
{
public:
Base(){}
int myint[N];
};
template<unsigned int M>
class BaseWrapper : Base<M>
{
public:
BaseWrapper(){}
};
template<typename T>
class User
{
public:
User(){}
//int myint[T::N]; //How to statically allocate using M or N from above?
};
int main(void)
{
User<BaseWrapper<10> > myuser;
// Do something with User::myint here.
}
我希望能够使用模板参数的非类型参数User
在 User
中静态分配数据类(class)。我知道我可以使用模板模板参数来创建 BaseWrapper<M>
里面User
但这不是我的首选方法。有什么简单的方法可以做到这一点吗?
谢谢!
最佳答案
将 static const unsigned int Size = N;
添加到您的类中。
示例:
template<unsigned int N>
class Base
{
public:
Base(){}
int myint[N];
static const unsigned int Size = N;
};
然后,N
可以在您的 User
类中作为 T::Size
进行访问。
关于c++ - 在没有模板参数的情况下访问嵌套模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8083215/