Forward referencing templates..

James Dean Palmer james at tiger3k.com
Thu Oct 26 09:55:59 PDT 2006


Bradley Smith wrote:
> James Dean Palmer wrote:
>> I am interested in expressing some data structure relationships like 
>> the half-edge data structure using templates.  For example, consider 
>> these classes with these type dependencies:
>>
>> class edge(TFace, TVector) {
>>  ...
>> }
>>
>> class face(TSurface, TEdge) {
>>  ...
>> }
>>
>> class surface(TFace) {
>>  ...
>> }
>>
>> class vector(TNumeric) {
>>  ...
>> }
>>
>> The idea being any one of these classes could be subclassed - for 
>> example to represent weighted edges, weighted faces, etc.. Or a 
>> different kind of basic numeric could be used. Now I would like to say 
>> this..
>>
>> alias vector!(double) vector3D;
>> alias edge!(face3D, vector3D) edge3D;
>> alias face!(surface3D, edge3D) face3D;
>> alias surface!(face3D) surface3D;
>>
>> But I get an error about a forward reference when trying to do it like 
>> this.  C++ has difficulty expressing the same kind of relationship 
>> because it doesn't forward reference templates.  I believe that Java 
>> can represent this kind of relationship though.
>>
>> Does anyone know if this can't be done in D? Or is there a better "D" 
>> way to do it?  Thanks!
> 
> 
> If you substitute surface3D into the alias for face3D, you get
> 
> alias face!(surface!(face3D), edge3D) face3D;
> 
> This shows that the alias for face3D is being defined by face3D. Isn't 
> that an infinitely recursive definition?

Sure, but remove the template aspect and consider the classes - we have 
no trouble representing classes with infinitely recursive relationships. 
  class Bob can point to an object of type Alice and Alice can point to 
an object of type Bob.  I'd like to be able to do the same thing with 
templatized classes.

> How would you construct a similar relationship in Java?

I gathered from this page that it seemed at least possible though I have 
not tested a construction:
http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeParameters.html



More information about the Digitalmars-d mailing list