Function overloading question

Daniel Keep daniel.keep.lists at gmail.com
Sun Aug 26 10:00:18 PDT 2007



Márcio Faustino wrote:
> Hi,
> 
> Shouldn't the compiler (DMD v2.003) issue at least a warning when I do
> this?
> 
> //---------------------------------------------------------
> import std.stdio;
> 
> void f(bool b, string which) {
>     writefln("bool == ", which);
> }
> 
> void f(char c, string which) {
>     writefln("char == ", which);
> }
> 
> void main() {
>     // Here:
>     void* fp = &f;
>     
>     // Which one will be called?
>     (cast(void function(bool, string)) fp)(0, "bool");
>     (cast(void function(char, string)) fp)(0, "char");
> }
> //---------------------------------------------------------
> 
> Thanks,

I can't think why.  I mean, you've put a specific kind of pointer into a
void* (that's perfectly fine).  Because f is overloaded, and you haven't
specified which overload you want, I believe it will take the first one,
lexically speaking.

You've then gone and brute-force cast the pointer to another type.  The
moment you involve cast, you're taking on responsibility for not doing
anything stupid.

Doing things like getting a pointer to an overloaded function is a bit
of a wart at the moment.  Hopefully, polysemous values will help.

	-- Daniel


More information about the Digitalmars-d-learn mailing list