www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A bug with matching overloaded functions?

reply flyinghearts <flyinghearts qq.com> writes:
void check(string s)  {}
void check(wstring s) {}
void check(dstring s) {}

void main()
{
  check("test");
  //check("test"c);
}


D:\Desktop\d\zb.d(7): Error: function zb.check called with argument types:
	((string))
matches both:
	zb.check(string s)
and:
	zb.check(immutable(dchar)[] s)





The type of "test" is string, isn't it?
Dec 01 2010
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, December 01, 2010 08:42:23 flyinghearts wrote:
 void check(string s)  {}
 void check(wstring s) {}
 void check(dstring s) {}
 
 void main()
 {
   check("test");
   //check("test"c);
 }
 
 
 D:\Desktop\d\zb.d(7): Error: function zb.check called with argument types:
 	((string))
 matches both:
 	zb.check(string s)
 and:
 	zb.check(immutable(dchar)[] s)
 
 
 
 
 
 The type of "test" is string, isn't it?
Not exactly. A string literal implictly casts to all 3 string types. You can add a suffix to force it to be a wstring or dstring (I don't think that there's a suffix for a normal string though), or you can cast it to the exact one you want, or you can assign it to a variable first. If you use auto, I believe that it will default to string rather than wstring or dstring, but string literals implicitly cast to all 3, so the compiler considers your code to be ambiguous. - Jonathan M Davis
Dec 01 2010
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 01 Dec 2010 13:58:25 -0500, Jonathan M Davis <jmdavisProg gmx.com>  
wrote:

 On Wednesday, December 01, 2010 08:42:23 flyinghearts wrote:
 void check(string s)  {}
 void check(wstring s) {}
 void check(dstring s) {}

 void main()
 {
   check("test");
   //check("test"c);
 }


 D:\Desktop\d\zb.d(7): Error: function zb.check called with argument  
 types:
 	((string))
 matches both:
 	zb.check(string s)
 and:
 	zb.check(immutable(dchar)[] s)





 The type of "test" is string, isn't it?
Not exactly. A string literal implictly casts to all 3 string types. You can add a suffix to force it to be a wstring or dstring (I don't think that there's a suffix for a normal string though), or you can cast it to the exact one you want, or you can assign it to a variable first. If you use auto, I believe that it will default to string rather than wstring or dstring, but string literals implicitly cast to all 3, so the compiler considers your code to be ambiguous.
I think it's a bug. This compiles: void check(int i) {} void check(long i) {} void check(short i) {} void main() { check(1); } -Steve
Dec 01 2010