Non-consistent implicit function template specializations

Mike Parker aldacron at gmail.com
Tue Aug 17 10:14:07 UTC 2021


On Tuesday, 17 August 2021 at 09:59:53 UTC, Rekel wrote:
> When using implicit function templates, identical 
> specialization yield different results.
> Example:
>
> ```d
> template TFoo(T)        { void foo(){writeln("1");} } // #1
> template TFoo(T : T[])  { void foo(){writeln("2");} } // #2
>
> void foo(T)(){
> 	writeln("1");
> }
>
> void foo(T : T[])(){
> 	writeln("1");
> }
>
> void main(string[] args) { // Works
> 	TFoo!(int).foo();            // "1"
> 	TFoo!(double[]).foo();       // "2"
> 	foo!(int)();            // "1"
> 	foo!(double[])();       // "1" !
> }
> ```
>
> I'm fairly certain the last call _should_ yield "2", yet it 
> does not.

The error is in your code. Both of your `foo` templates are 
implemented to print `"1"`. Change the second one to print "2" 
and you will see the desired output.

At any rate, to specialize on arrays you should generally be 
using something like `T : U[], U`. Ditto for pointers: `T : U*, 
U`.


More information about the Digitalmars-d-learn mailing list