Multiple Specialization?

Bill Baxter wbaxter at gmail.com
Fri Dec 22 14:10:44 PST 2006


Chris Nicholson-Sauls wrote:
> Xinok wrote:
> 
>> 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 agree with Chris that grouping the types seems like a better idea. 
Repeating T is asking for trouble.
     (T : {a, b})
or somesuch.  Or maybe multiple colons
     (T :a:b)

>> 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.
> 
> 
> Or one template with a long static if at the beginning, which can be 
> annoying for, say, template functions.  Its an interesting idea at the 
> least -- but I'm not sure what it might take in the compiler.

Yeh, static if(is(...)) is the way I'd do this now.  That also allows 
you to accept "any type implicitly convertable to T" which covers a lot 
of real-world cases.  For the most part you don't care if it's one of 16 
specific types, you care if it can be assigned to an 'int' or if an int 
can be assigned to it.

Personally, I have yet to use specialization, because of it's annoying 
"feature" of disabling IFTI.  (At least that's the way it seemed the 
last time I tried it -- correct me if I'm wrong there)  I would love for 
it to become usable.  But right now static if is more flexible and 
powerful.  I agree that it would be nice if the same power were 
available via the specialization syntax.

--bb



More information about the Digitalmars-d mailing list