Where is the conflict here?

Why can't I overload this template function?

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; } 

As far as I can tell, both functions cannot accept the same types of arguments.

+6
source share
1 answer

You cannot overload template functions with functions without templates due to an error . This will hopefully be fixed in the future.

In the meantime, you can write another function as a template specialization:

 T find(T : double[], E : string)(T haystack, E needle) { return haystack; } 
+9
source

Source: https://habr.com/ru/post/917730/


All Articles