digitalmars.D.learn - Function Template Overloading
- Q. Schroll (12/12) Mar 14 2017 void test(T)(T* arg);
- ketmar (4/8) Mar 14 2017 'cause `ref T` is more generic than `T*`. think of it as "greedy matchin...
- Q. Schroll (4/14) Mar 14 2017 Wouldn't it be better vice versa, the more specific pattern to be
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? Making the first one take auto ref T* lets the compiler choose the first. Making the second one non-ref lets the compiler give me an ambiguity error. Template Functions are not mentioned in the spec, at least not on https://dlang.org/spec/function.html#function-overloading, but it suggests that ref should not make the decision if it can be bound to.
Mar 14 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.
Mar 14 2017
On Wednesday, 15 March 2017 at 02:33:36 UTC, ketmar wrote:Q. Schroll wrote: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?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.
Mar 14 2017