Template with template?

Chris wendlec at tcd.ie
Thu Mar 20 09:28:45 PDT 2014


How can I instantiate Person with Trait, i.e. a template with a 
template?

struct Trait(T0, T1) {
   T0 name;
   T1 value;
   T1[T0] map;

   this(T0 name, T1 value) {
     this.name = name;
     this.value = value;
     map[name] = value;
   }
}

class Person(T) {
   T traits[];

   void addTrait(T trait) {
     traits ~= trait;
   }
}


void main()
{
   auto trait1 = Trait!(string, string)("Name", "John");
   auto trait2 = Trait!(string, int)("Age", 42);
   writefln(%s", trait1.map);
   writefln(%s", trait2.map);
   // above code compiles and works
}


More information about the Digitalmars-d-learn mailing list