Recursive data structure using template won't compile
Dmitry Olshansky
dmitry.olsh at gmail.com
Thu Nov 8 07:19:52 PST 2012
11/8/2012 10:39 AM, Rob T пишет:
> I want to create a simple recursive data structure as follows:
>
> struct R
> {
> int value;
> d_list!R Rlist;
> }
Not possible - you don't know the size of R at this point. So determine
it compiler looks inside of d_list, and then encounters _node_.
It naturally sees T which is R and starts this loop anew.
To break this chain make node a separate templated struct outside of
d_list. Then peeking inside of d_list compiler shouldn't go crazy.
>
> // d-linked list with templated payload
> struct d_list( T )
> {
> struct node
> {
> T payload;
> node* pred;
> node* succ;
> }
> node* head;
> node* tail;
> }
>
> The compiler complains about node having "forward references".
>
> I was able to get something like this to work in C++, so I imagine I can
> also get it to work in D, but does anyone know how?
>
Naturally C++ doesn't have nested struct definitions.
> I also want to template the recursive structure itself to specify the
> value type, ie struct R(T){ T value; d_list!R Rlist; }, but I left that
> out to keep the example more simple.
>
> I've been stuck on this problem all day, so any help is appreciated!
See if it helps.
--
Dmitry Olshansky
More information about the Digitalmars-d-learn
mailing list