Address of overloaded functions

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Jul 3 09:24:20 PDT 2013


On Wed, Jul 03, 2013 at 06:07:07PM +0200, Artur Skawina wrote:
> On 07/03/13 17:43, H. S. Teoh wrote:
> > On Wed, Jul 03, 2013 at 05:41:25PM +0200, Artur Skawina wrote:
> >> On 07/03/13 17:27, H. S. Teoh wrote:
> >>> On Wed, Jul 03, 2013 at 05:15:48PM +0200, John Colvin wrote:
> >>>> On Wednesday, 3 July 2013 at 15:03:46 UTC, Artur Skawina wrote:
> >>>>> On 07/03/13 16:52, John Colvin wrote:
> >>>>>> Is there any way to take the address of any of an overloaded set
> >>>>>> of functions?
> >>>>>>
> >>>>>> import std.stdio;
> >>>>>>
> >>>>>> void foo(int a){ writeln("overload int"); }
> >>>>>> void foo(long b){ writeln("overload long"); }
> >>>>>>
> >>>>>> void main()
> >>>>>> {
> >>>>>>    auto b = &foo; //ambiguous => error
> >>>>>>    b(2); //valid for either overload
> >>>>>> }
> >>>>>
> >>>>>    void function(long) b = &foo;
> >>>>>
> >>>>> artur
> >>>>
> >>>> Thanks, that works
> >>>
> >>> This is interesting. How does C++ handle this? (Or does it?)
> >>
> >> The same - the context determines which overload is chosen, and 
> >> ambiguity is an error.
> > 
> > Oh, so it tells the difference by whether you write
> > 
> > 	void (*p)(int) = foo;
> > 
> > or
> > 
> > 	void (*p)(long) = foo;
> > 
> > ?
> 
> Yep. Things like
> 
>    void c(void (*fp)(long), long a) { fp(a); }
>    c(foo, 2);
> 
> work as expected too.
> 
> > I guess that makes sense.
> 
> The context dependence isn't ideal, but what's the alternative?...
[...]

Explicit syntax for specifying overloads? ;-) Not like that would happen
in D, though.


T

-- 
Error: Keyboard not attached. Press F1 to continue. -- Yoon Ha Lee, CONLANG


More information about the Digitalmars-d-learn mailing list