www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Function overloading question

reply =?UTF-8?B?TcOhcmNpbyBGYXVzdGlubw==?= <m.faustino no.spam.gmail.com> writes:
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,
Aug 26 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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
Aug 26 2007
parent reply =?UTF-8?B?TcOhcmNpbyBGYXVzdGlubw==?= <m.faustino no.spam.gmail.com> writes:
Daniel Keep wrote:
 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

Thanks for the answer. I wasn't actually coding anything like that, it's just that I was wondering how to take the address of an overloaded function.
Aug 26 2007
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Márcio Faustino wrote:
 Daniel Keep wrote:
 
 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

Thanks for the answer. I wasn't actually coding anything like that, it's just that I was wondering how to take the address of an overloaded function.

void foo(int) {} void foo(double) {} There are two ways: void function(int) fn1 = &foo; void function(double) fn2 = &foo; These will each get the proper overloads. Alternately: auto fn3 = cast(void function(int)) &foo; auto fn4 = cast(void function(double)) &foo; This will also work. -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Aug 26 2007
parent =?UTF-8?B?TcOhcmNpbyBGYXVzdGlubw==?= <m.faustino no.spam.gmail.com> writes:
Kirk McDonald wrote:
 void foo(int) {}
 void foo(double) {}
 
 There are two ways:
 
 void function(int) fn1 = &foo;
 void function(double) fn2 = &foo;
 
 These will each get the proper overloads. Alternately:
 
 auto fn3 = cast(void function(int)) &foo;
 auto fn4 = cast(void function(double)) &foo;
 
 This will also work.
 

Thanks!
Aug 27 2007