【问题标题】:struct template using struct template使用结构模板的结构模板
【发布时间】:2015-01-05 15:47:57
【问题描述】:

我在另一个模板化结构中实现模板化结构时遇到问题。

基本上这是可行的:

template<typename T>
struct ListElement {
    T Value;
    struct ListElement<T>* Next;
};

template<typename M>
struct List {
    struct ListElement<M>* ContainedElements;
};

但我真正想要的是这样的实现:

template<typename T>
struct ListElement {
    T Value;
    struct ListElement<T>* Next;
};

template<typename M>
typedef struct ListElement<M>* List;

问题是您不能将 typedef 与模板一起使用。写 struct List&lt;Anytype&gt; 代替 struct ListElementPointer* 的任何变通方法?

PS:顺便说一句,这是一个学校项目。不允许使用标准向量或列表类,我不想为我使用的每种类型键入新的定义......也不允许使用类:-/

【问题讨论】:

  • “您不能将 typedef 与模板一起使用”是什么意思。您遇到什么错误或问题?
  • @StephaneRolland 它给出了语法错误,因为它是不允许的。在其他话题中讨论过[link]

标签: c++ templates struct linked-list


【解决方案1】:

如果您能够使用 C++11,则可以使用以下别名声明:

template<typename M>
using List = ListElement<M>*;

【讨论】:

  • 谢谢。需要补充的是,它是在 C++11 中作为“别名声明”添加的。
猜你喜欢
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 2011-01-25
  • 1970-01-01
  • 2014-12-22
  • 1970-01-01
相关资源
最近更新 更多