Recursive data structure using template won't compile
Nick Sabalausky
SeeWebsiteToContactMe at semitwist.com
Thu Nov 8 12:35:46 PST 2012
On Thu, 08 Nov 2012 19:19:52 +0400
Dmitry Olshansky <dmitry.olsh at gmail.com> wrote:
> 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.
>
That's incorrect behavior. The size of R is NOT needed to determine
the size of "d_list!R":
Note that "d_list!R" has exactly 2 data members, and BOTH of them are
pointers. Therefore, the size of "d_list!R" is trivially determined
*regardless* of its template parameter or "nested" type: Two pointers
== 8 bytes on 32-bit, 16 bytes on 64-bit, *regardless* of what the
pointers point to.
Also note that "node" is not actually a *data* member of d_list, and
does not contribute to the size or internal structure of d_list.
> 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.
>
No, I tried that, and it still fails. This is definitely a compiler bug.
More information about the Digitalmars-d-learn
mailing list