Referencing an overloaded function

Mantis mail.mantis.88 at gmail.com
Sat Mar 24 07:39:39 PDT 2012


24.03.2012 14:13, John написал:
> Is there any way to refer to a specific function overload?
>
> For example:
>
> import std.stdio;
> import std.traits;
>
> void foo() {}
> void foo(int x) {}
>
> void main() {
>     writeln(foo.mangleof);
>     writeln(ParameterTypeTuple!(foo).stringof);
> }
>
> Both of these statements always refer to the first matching function.  
> If I change the order of declarations for foo, the results change.
>
> Is there a way to reference the second overload in this example?
>
> I can use a function pointer with ParameterTypeTuple to get the 
> correct result, but something like mangleof won't work, since that 
> will return the mangle of the local function pointer instead of the 
> original function.
>

import std.stdio;
import std.traits;

void foo() { writeln( "void" ); }
void foo( int x ) { writeln( x ); }

template getOverload( alias func, A... ) {
     void function(A) getOverload = &func;
}

void main() {
     writeln( getOverload!(foo, int).mangleof );
     writeln( ParameterTypeTuple!(getOverload!(foo, int)).stringof );
     getOverload!(foo, int)(42);
     getOverload!(foo)();
}

Will not hold for return types other than void, there may be some 
generic workaround (of course, you can pass return type as tempate 
parameter).


More information about the Digitalmars-d-learn mailing list