【问题标题】:alternate ways to typedef structs [duplicate]typedef结构的替代方法[重复]
【发布时间】:2021-10-04 20:28:07
【问题描述】:

有没有办法在 C++ 中对模板化结构进行类型定义?例如:

template <typename T>
struct mylist
{
  std::vector<T> contents;
  // ... other members
};

typedef struct mylist<T> * List<T>; // this is illegal

我的目标是隐藏我的List 是一个指针的事实,这样我就可以编写如下代码:

List<int> intList;
List<char> charList;

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    您可以使用alias template(C++11 起)。例如

    template <typename T>
    using List = mylist<T> *;
    

    然后

    List<int> intList;   // -> mylist<int> *
    List<char> charList; // -> mylist<char> *
    

    【讨论】:

    • fwiw,我建议正确命名它,例如ListPtr。指向列表的指针不是列表
    猜你喜欢
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多