Thread overview | |||||
---|---|---|---|---|---|
|
February 20, 2005 Signature selection help. | ||||
---|---|---|---|---|
| ||||
In the code below, DMD keeps telling me ...
test.d(9): function test.foo overloads int(real x,uint cnt) and int(dchar
x,uint cnt) both match argument list for foo
So why can't it tell which function I'm trying to use, and what can I do to let it know which one I want. BTW I want to call the 'dchar' version when a character literal is supplied.
<code>
int foo(real x, uint cnt){ return 1;}
int foo(dchar x, uint cnt){ return 2;}
void main()
{
int x;
x = foo(cast(dchar)'a', 6); //#1 - fails
// x = foo('a', 6); // #2 - fails
//x = foo(cast(real)'a', 6); // #3 - compiles but uses wrong function.
}
</code>
--
Derek
Melbourne, Australia
|
February 20, 2005 Re: Signature selection help. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Attachments: | Derek wrote: | In the code below, DMD keeps telling me ... | | test.d(9): function test.foo overloads int(real x,uint cnt) and | int(dchar x,uint cnt) both match argument list for foo | | So why can't it tell which function I'm trying to use, and what can I | fo to let it know which one I want. BTW I want to call the 'dchar' | version when a character literal is supplied. | | <code> | int foo(real x, uint cnt){ return 1;} | int foo(dchar x, uint cnt){ return 2;} | | void main() | { | int x; | | x = foo(cast(dchar)'a', 6); //#1 - fails x = foo(cast(dchar)'a', 6u); // x=2 | // x = foo('a', 6); // #2 - fails x = foo('a', 6u); // expected failure // replace 'a'(char) with '\U00000061'(dchar) and it'll ork | //x = foo(cast(real)'a', 6); // #3 - compiles but uses wrong | function. x = foo(cast(real)'a', 6u); // x=1 | } | </code> http://www.digitalmars.com/d/function.html # 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. Thomas |
February 20, 2005 Re: Signature selection help. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Thomas Kühne | On Sun, 20 Feb 2005 10:13:32 +0100, Thomas Kühne wrote: Thanks Thomas, this has helped. > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Derek wrote: >| In the code below, DMD keeps telling me ... >| >| test.d(9): function test.foo overloads int(real x,uint cnt) and >| int(dchar x,uint cnt) both match argument list for foo >| >| So why can't it tell which function I'm trying to use, Okay, I know the matching rule... # 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. It was the implicit conversion that threw me, and the actual text of the error message. If the error message had have read more like ... # test.d(9): function test.foo overloads int(real x,uint cnt) and # int(dchar x,uint cnt) both match argument list for foo(dchar,int) then I might have realized that it was the *second* parameter it was barfing over. I kept on thinking it was the first parameter. >| and what can I do to let it know which one I want. I need to specify the exact storage type for literals in function calls, in order to avoid such things. -- Derek Melbourne, Australia |
Copyright © 1999-2021 by the D Language Foundation