Compilable Recursive Data Structure ( was: Recursive data structure using template won't compile)
Rob T
rob at ucora.com
Fri Nov 9 20:21:31 PST 2012
On Friday, 9 November 2012 at 21:32:01 UTC, Manfred Nowak wrote:
> Timon Gehr wrote:
>
>> The example definitely exposes a bug in DMD.
>> The D front end I am developing can already handle it.
>
> May I guess, that your front end is also able to handle this
> code:
>
> struct Elem( size_t myNumber) {
> Elem!( myNumber +1)* next;
> }
> void main(){
> Elem!0 list;
> list.next= new Elem!1;
> }
>
> -manfred
With the unmodified template as posted, I get a very specific
error about "recusive expansion", but I can see why using that
form of template - it will expand into multiple types forever
Elem!(0), Elem!(1), Elem!(2) ... Elem!(infinity).
Now take note that with the modified code below, it works fine,
and exactly as I would expect:
struct Elem(T) {
Elem* next;
}
My attempt to mess up the compiler did not succeed, the code
below still compiles as expected.
struct Elem(T) {
Elem!(T)* next;
}
If it works above, then it should also work with the code I
introduced in the OP because it's the exact same scenario, only
much more simplified. The type expansion should stop at the
pointers, and the error message indicates that it does (there's
no recursive expansion message), but it fails to evaluate all the
types correctly (forwarded reference message).
I think this example clears the matter up nicely, and the problem
I'm experiencing is definitely a compiler bug. I'd like to see
this problem resolved, so I'll file a bug report.
BTW, thanks for all the attention to this matter, I've learned a
lot more about D templates, such as the recusivness of templates
demonstrated by manfred, and the dialogue in here helped me to
find a reasonable work-a-round for my particular application.
--rt
More information about the Digitalmars-d-learn
mailing list