Template function specialization doesn't work

Steven Schveighoffer schveiguy at gmail.com
Tue Jul 7 20:04:36 UTC 2020


On 7/7/20 3:53 PM, IGotD- wrote:
> I have two template functions
> 
> void overloadedFunction(T)(ref T val)
> {
> ....
> }
> 
> 
> void overloadedFunction(T : T[])(ref T[] s)
> {
> ....
> }
> 
> Obviously the second should be used when the parameter is a slice of any 
> type, and the first should be used in other cases. However this doesn't 
> happen, the compiler always picks the first function regardless if the 
> parameter is a slice or not.
> 
> So
> 
> 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?
> 

That specialization is... odd. It's basically saying, is T an array of T.

I know I've seen this before, so I think it's valid. But maybe not?

Have you tried (T: U[], U)(ref T[] s) ?

-Steve


More information about the Digitalmars-d-learn mailing list