简体   繁体   中英

struct template using struct template

I am facing a problem implementing a templated struct in another templated struct.

Basically this works:

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

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

But what I really want is an implementation like that:

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

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

The thing is that you can not use typedef with templates. Any workaroud to write struct List<Anytype> instead of struct ListElementPointer*?

PS: It is a school project by the way. Using standard vector or list classes is not allowed and I do not want to type new definitions for every type I use...classes are not allowed as well :-/

If you are able to use C++11, you can use the following alias declaration:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM