Template overloading

Bill Baxter dnewsgroup at billbaxter.com
Wed Dec 5 18:45:00 PST 2007


Bruno Medeiros wrote:
> Bill Baxter wrote:
>> Does anyone have a really good understanding of the dummy=void hack 
>> for making two templates with the same name but different arguments?  
>> Every time I try to use that I end up I just end up randomly adding in 
>> dummy=voids here and there till it works.  Is there some rationale for 
>> where it should go (end of parameter list? beginning?)?  And what 
>> about when you have 3 or more templates you need to differentiate?
>>
> 
> What do you mean "3 or more templates you need to differentiate"? Do you 
> remember this: 
> http://d.puremagic.com/issues/show_bug.cgi?format=multiple&id=337 ?
> 
>> Also, am I right in thinking that if you pass any of the "real" 
>> template arguments explicitly then suddenly the dummy args must be 
>> passed too?
>>
> 
> Yes, the dummy hack only works implicitly for IFTI.
> 
>> Any advice on best practices in this area?
>>
>> My most recent run-in with this was trying to write a matrix multiply 
>> template that could take arguments of either Mat!(T),Mat!(T), or 
>> Mat!(T),Vec!(T).
>>
>> --bb
> 
> What exactly are you trying to accomplish? This works:
> 
> void mult2(T)(Mat!(T) a, Mat!(T) b) {
>     pragma(msg, "Mat, Mat");
> }
> 
> void mult2(T)(Mat!(T) a, Vec!(T) b) {
>     pragma(msg, "Mat, Vec");
> }
> 
> 
> void main() {
>     mult2(new Mat!(int), new Mat!(int));
>     mult2(new Mat!(int), new Vec!(int));
> }
> 
> But that seems too simple a solution, was that you were trying to do?

Ah, ok.  Actually in the real code this was taken from there are integer 
parameters that give the dimensions of the Mat and Vec.  But DMD seems 
to have some issues with inference on integer parameters wrapped in a 
type, so I'm guessing that was probably why the straightforward solution 
didn't work.

Actually the two should be:

void mult2(T, int M, int N, int P)(Mat!(T,M,N) a, Mat!(T,N,P) b) {
     pragma(msg, "Mat, Mat");
}

void mult2(T,int M, int N)(Mat!(T,M,N) a, Vec!(T,N) b) {
     pragma(msg, "Mat, Vec");
}

--bb


More information about the Digitalmars-d-learn mailing list