Template function specialization doesn't work

Ali Çehreli acehreli at yahoo.com
Tue Jul 7 21:27:35 UTC 2020


On 7/7/20 12:53 PM, IGotD- wrote:

> ubyte[3] ar = [ 1, 2, 3 ];
> ubyte[] arSlice = ar;
> 
> overloadedFunction(arSlice);
> 
> The first function will be used. Shouldn't the template argument (T : 
> T[]) make the compiler pick the second one?

There is also template constraints which may be useful:

import std.traits;

void overloadedFunction(T)(ref T val)
if (!isArray!T) {
   writeln("general");
}

void overloadedFunction(T)(ref T s)
if (isArray!T) {
   writeln("T[]");
}

Ali




More information about the Digitalmars-d-learn mailing list