digitalmars.D - Implicit casting issue
- zwang <nehzgnaw gmail.com> Feb 01 2005
- Manfred Nowak <svv1999 hotmail.com> Feb 01 2005
- zwang <nehzgnaw gmail.com> Feb 01 2005
The following C++ code correctly compiles and prints "C1":
#include <cstdio>
class C0{};
class C1 : public C0{};
class C2 : public C1{};
class V{
public:
void v(C0& i){printf("C0");}
void v(C1& i){printf("C1");}
};
int main(){
V i;
C2 j;
i.v(j);
return 0;
}
Here's the equivalent D version:
class C0{}
class C1 : C0{}
class C2 : C1{}
class V{
void v(C0 i){printf("C0");}
void v(C1 i){printf("C1");}
}
int main(){
V i = new V;
C2 j = new C2;
i.v(j);
return 0;
}
... which does *not* compile. DMD complains:
"function test.V.v overloads void(C0 i) and void(C1 i) both match
argument list for v"
Why doesn't DMD implicitly cast C2 to C1 just like C++ compilers do?
Is this intended or a bug?
Feb 01 2005
zwang wrote: [...]... which does *not* compile. DMD complains: "function test.V.v overloads void(C0 i) and void(C1 i) both match argument list for v" Why doesn't DMD implicitly cast C2 to C1 just like C++ compilers do? Is this intended or a bug?
Congratulions for not reading the docs: http://www.digitalmars.com/d/function.html#overloading | In D, function overloading is simple. It matches exactly, it | matches with implicit conversions, or it does not match. If there | is more than one match, it is an error. C2 can be implicitely casted to C1 and to C0. So there is more than one match and therefore an error. -manfred
Feb 01 2005
Thanks! I guess I'm still writing code in a c++ mindset. Manfred Nowak wrote:Congratulions for not reading the docs: http://www.digitalmars.com/d/function.html#overloading | In D, function overloading is simple. It matches exactly, it | matches with implicit conversions, or it does not match. If there | is more than one match, it is an error. C2 can be implicitely casted to C1 and to C0. So there is more than one match and therefore an error. -manfred
Feb 01 2005








zwang <nehzgnaw gmail.com>