Function Template Overloading

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 14 19:33:36 PDT 2017


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.


More information about the Digitalmars-d-learn mailing list