Qwe*_*tie 6 templates overloading d
为什么我不能重载此模板功能?
import std.stdio;
T[] find(T, E)(T[] haystack, E needle)
if (is(typeof(haystack[0] != needle)))
{
while(haystack.length > 0 && haystack[0] != needle) {
haystack = haystack[1 .. $];
}
return haystack;
}
// main.d(14): Error: function main.find conflicts with template main.find(T,E) if (is(typeof(haystack[0] != needle))) at main.d(5)
double[] find(double[] haystack, string needle) { return haystack; }
int main(string[] argv)
{
double[] a = [1,2.0,3];
writeln(find(a, 2.0));
writeln(find(a, "2"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这两个函数不能接受相同的参数类型.
由于错误,您无法使用非模板函数重载模板函数.这有希望在将来某个时候得到解决.
在此期间,您可以将其他函数编写为模板特化:
T find(T : double[], E : string)(T haystack, E needle)
{
return haystack;
}
Run Code Online (Sandbox Code Playgroud)