c++ - 在没有模板参数的情况下访问嵌套模板参数

标签 c++ templates nested

我有这样的情况:

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.
}

我希望能够使用模板参数的非类型参数UserUser 中静态分配数据类(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/

相关文章:

c++ - 使用派生类型调用 boost::function<void(BaseType)>

c++ - std::set_new_handler 如何提供更多可用内存?

按嵌套类型数组中的第一个元素排序

python - 将嵌套列表转换为 pd Dataframe 的快速且 Pythonic 的方法

django - 比较 Django 模板中的变量

我可以在 C 中的格式字符串中使用嵌套格式字符吗?

c++ - 将两个 std::vector<cv::Point> vector 和安全公共(public)点与第三个 std::vector<cv::Point> 进行比较

c++ - 二维多维数组传递给内核,CUDA

c++ - 为什么这个 Blitz++ 代码不能编译?

c++ - "template"不需要关键字? [gcc/clang/Comeau 错误?]