Multiple Specialization?

Xinok xnknet at gmail.com
Fri Dec 22 11:17:45 PST 2006


I'm not sure what else you could call this...

My idea for multiple specialization is to be able to associate multiple types
with a single template. Take for example:

template temp(T){ }
template temp(T : int, T : long){ } // Specialized Template
It would use the specialized template if T == int OR T == short

I have two designs for this, the first you see above. Because the order of
template arguments is important, all types must come in succession:
template temp(T1, T2 : int, T2 : long, T3){ } // OK
template temp(T1, T2 : int, T3, T2 : long){ } // Error - T2 is not in succession
This is important, because the compiler can't be sure if T2 or T3 comes first:
(T1, T2, T3) or (T1, T3, T2)

My second design is to use braces:
template temp(T1, T2 : {int, long}, T3){ }

I also think you could use tuples somehow, but I have no idea how you could
write that. But take for example:
template tuple(T...){ alias T tuple; }
template temp(T : tuple!(int, long)){ }

I think tuples would work best because you can define aliases for them, where
as you can't define an alias for the two other designs:
alias tuple!(byte, short, int long) intset;
template temp(T1 : intset, T2 : intset){ }


What could this be used for? Suppose you wanted to make a specialized template
for all int types? Or float types? Or char types? You would have to duplicate
the template multiple times, especially if you have two or more types:
template temp(T1 : {byte, short, int, long}, T2 : {byte, short, int long}){ }
This would normally require you to define 16 specialized templates.



More information about the Digitalmars-d mailing list