On 03/24/12 15:39, Mantis wrote:
> 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).

Something like

> auto getOverload(alias func, A...)(A a) {
>     typeof(foo(a)) function(A) getOverload = &func;
> }

would work.

The problem with that solution is however that .mangleof will return
the wrong name.

This:

>     foreach (f; __traits(getOverloads, __traits(parent, main), "foo")) {
>        static if (is(ParameterTypeTuple!f==TypeTuple!(int))) {
>           writeln(f.mangleof);
>           writeln(ParameterTypeTuple!(f).stringof);
>           //writeln(typeof(f).stringof);
>           //f(42);
>           //etc
>        }
>     }

will do the right thing when you need the original name of foo(int).

artur

Reply via email to