Compilable Recursive Data Structure ( was: Recursive data structure using template won't compile)

Rob T rob at ucora.com
Fri Nov 9 20:41:10 PST 2012


On Friday, 9 November 2012 at 20:57:42 UTC, Manfred Nowak wrote:
> Rob T wrote:
>
>> What is happening however, is that the templates are not doing
>> what would be expected if the type was introduced manually
>
> The expectations might be wrong.
>
> With Templates one is able to introduce recursive definitions 
> of types
> into the type system. As with recursive functions, the feedom, 
> thereby
> generated, can introduce relaxation as well as strain.
>
> -manfred

I agree that you can introduce recursive definitions into a 
template, and you have demonstrated that ability rather well, 
however the definitions in my template does not do this, and so 
far I have not seen a demonstration of how it could be doing so.

The type expansion is definitely finite, there's several posts 
following through with the expansion, and they all show that the 
compiler must stop expanding at the pointers. All of the pointers 
are of a known finite size (standard pointer type) and the type 
they represent for dereferencing is definitely knowable.

The problem appears to be with the compiler failing to evaluate 
all of the type definitions fully, and as a result it 
inappropriately fails with a "forwarded reference" error.

I've seen a somewhat related problem with functions that return 
"auto". If the body conatins something that needs to know the 
return value, the compiler may fail with a "forwarded reference" 
error. There's absolutely no valid reason why the compiler should 
be doing this, and it exposes a weakness in the way it does type 
evaluations (in at least that case). On the other hand, I've seen 
the compiler do very good work evaluating out-of-order type 
definitions that are all over the place.

If I'm to take the D specification (what little of it there is) 
at face value, out-of-order type definitions are always perfectly 
legal provided that they are legal definitions of course, so when 
the compiler fails on them, then it's a bug that needs to be 
fixed.

--rt



More information about the Digitalmars-d-learn mailing list