Template with template?

Meta jared771 at gmail.com
Thu Mar 20 13:19:50 PDT 2014


On Thursday, 20 March 2014 at 19:38:25 UTC, Chris wrote:
> I thought the array T[] traits could hold any _type_ the 
> template Trait is instantiated into. That's where I got it 
> wrong.

This is the best explanation for this restriction that I can 
think of:

struct Person(T)
{
     static if (is(T == int))
     {
         int age;
         @property int age() { return age; }
         @property int age(int val) { return age = val; }

         @property bool isMiddleAge() { return age >= 30 && age <= 
50); }
     }
     else static if (is(T == string))
     {
         string name;
         //Etc...
     }
}

Depending on what type T is, Person could have an age and 
associated methods, or a name and associated methods, but never 
both. If T is neither int nor string, it will completely empty. 
Therefore, Person!int MUST be a completely different type from 
Person!string or Person!float. It just doesn't make any sense 
otherwise.


More information about the Digitalmars-d-learn mailing list