Test for self referential templated Class

Manfred_Nowak svv1999 at hotmail.com
Mon Jun 30 03:24:48 PDT 2008


Robert Fraser wrote:

> Make it derive from a (non-templated) interface and test for that.

Yes, this seems to be a part of the solution. With this hack every 
instance can check whether the given type T belongs to any instance of 
C's family of instances. But it seems still not possible to identify an 
instance on a specified non trivial depth of recursion:

import std.metastrings;
interface Hack{};
class C(T):Hack{
  pragma( msg, Format!(is( T: Hack)));
  pragma( msg, Format!(is( T== int)));
  pragma( msg, Format!(is( T== C!( int))));
  //pragma( msg, Format!(is( T== C!( C!( int))))); //recursion
  //pragma( msg, Format!(is( T== C!( C!( C!( int)))))); //recursion
}
//class C(T:C!( C!(int))){ }
void main(){
  auto c1= new C!(int);
  pragma( msg, "c1 done");
  auto c2= new C!( C!(int));
  pragma( msg, "c2 done");
  auto c3= new C!( C!( C!(int)));
  pragma( msg, "c3 done");
}

-manfred



More information about the Digitalmars-d mailing list