0

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 :-/

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
TradeItEasy
  • 129
  • 8
  • what do you mean by "you can not use typedef with templates". What error, or problem do you encounter ? – Stephane Rolland Jan 05 '15 at 15:52
  • @StephaneRolland it gives syntax errors as it is not allowed. It was discussed in other topics [[link](http://stackoverflow.com/questions/2448242/struct-with-template-variables-in-c)] – TradeItEasy Jan 05 '15 at 18:07

1 Answers1

1

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

template<typename M>
using List = ListElement<M>*;
R Sahu
  • 204,454
  • 14
  • 159
  • 270