Bug 865 – in overloaded-function, class A should matches (Object) better than (void*)

Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-01-21T01:10:00Z
Last change time
2014-02-15T13:12:43Z
Assigned to
bugzilla
Creator
someanon

Comments

Comment #0 by someanon — 2007-01-21T01:10:26Z
From digitalmars.D: -- class A, matches (Object) better than (void*), so just do the Right thing to choose overloaded-function(Object obj). ==================================== $ cat ts.d import std.string; class A{} struct B{ char[] toString() {return "B";} } int c; char[] toStringFunc(Object obj) {return obj.toString();} char[] toStringFunc(int i) {return format(i);} char[] toStringFunc(void* obj) {return "null";} class S(T) { T obj; char[] toString() { return toStringFunc(obj); } } int main(char[][] args) { S!(A) sa; S!(B*) sb; S!(int) sc; printf("%.*s", sa.toString()); printf("%.*s", sb.toString()); printf("%.*s", sc.toString()); return 0; } $ dmd.exe ts.d ts.d(19): function ts.toStringFunc called with argument types: (A) matches both: ts.toStringFunc(Object) and: ts.toStringFunc(void*) ts.d(25): template instance ts.S!(A) error instantiating ==================================== Come on! class A matches both (Object) and (void*)?!
Comment #1 by bugzilla — 2007-01-21T02:48:09Z
D has 3 kinds of overload matching: 1) exact match 2) match with implicit conversions 3) no match There is no dividing up (2) into better or worse matches (like C++). So, the example is behaving as defined.
Comment #2 by someanon — 2007-01-21T12:41:42Z
But it certainly makes more sense to match super-class Object. Can this be considered a feature enhancement?
Comment #3 by htvennik — 2008-04-24T10:17:48Z
I think this is very bad indeed... The best way to go would be to not allow implicit conversions from a class object to void *. That would not break the rules as mentioned by Walter in reply #1.
Comment #4 by gide — 2008-11-08T13:15:13Z
The example given compiles on DMD 1.033. C:> cat ts.d import std.string; import std.stdio; class A{} struct B{ char[] toString() {return "B";} } int c; char[] toStringFunc(Object obj) {return obj.toString();} char[] toStringFunc(int i) {return format(i);} char[] toStringFunc(void* obj) {return "null";} class S(T) { T obj; char[] toString() { return toStringFunc(obj); } } int main(char[][] args) { S!(A) sa; S!(B*) sb; S!(int) sc; writefln("%.*s", sa.toString()); writefln("%.*s", sb.toString()); writefln("%.*s", sc.toString()); return 0; }