Function Template Overloading

Q. Schroll via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 14 19:49:16 PDT 2017


On Wednesday, 15 March 2017 at 02:33:36 UTC, ketmar wrote:
> Q. Schroll wrote:
>
>> void test(T)(T* arg);
>> void test(T)(ref T arg);
>>
>> Let p be any pointer. Why is test(p) not an ambiguity error? 
>> Why is the second overload chosen?
>
> 'cause `ref T` is more generic than `T*`. think of it as 
> "greedy matching": compiler first tries to match `int*`, and if 
> that failed, it tries `int`, for example. and `int*` matches 
> the second template, so compiler choosing it.

Wouldn't it be better vice versa, the more specific pattern to be 
prioritized? And as it actually *can* match both, is it a 
compiler-bug not to be an ambiguity error?


More information about the Digitalmars-d-learn mailing list