Thread overview | |||||
---|---|---|---|---|---|
|
February 01, 2005 Implicit casting issue | ||||
---|---|---|---|---|
| ||||
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? |
February 01, 2005 Re: Implicit casting issue | ||||
---|---|---|---|---|
| ||||
Posted in reply to zwang | 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 |
February 02, 2005 Re: Implicit casting issue | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manfred Nowak | 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
|
Copyright © 1999-2021 by the D Language Foundation