template - aliasing a member function

Kenji Hara k.hara.pg at gmail.com
Wed Aug 8 16:37:07 PDT 2012


On Wednesday, 8 August 2012 at 23:21:32 UTC, Ellery Newcomer 
wrote:
> say I have
>
> template T(alias fn) {
> }
>
> class Foo {
>   int i();
>   void i(int);
> }
>
> alias T!(Foo.i) Biz;
>
> Is there a way to get a handle to both of the overloads of 
> Foo.i inside T?
>
> Actually, all I really need for that is to get 'Foo.i' out of 
> fn.
>
> mangleof looks promising..

import std.typetuple : TypeTuple;
template Id(alias a) { alias a Id; }

template T(alias fn) {
     alias Id!(__traits(parent, fn)) Parent;
     // use Id template so we cannot alias __traits result directly
     static assert(is(Parent == Foo));

     enum FnName = __traits(identifier, fn);

     alias TypeTuple!(__traits(getOverloads, Parent, FnName)) 
Overloads;
     // use TypeTuple template so we cannot alias __traits result 
directly
     pragma(msg, typeof(Overloads[0]));  // prints int()
     pragma(msg, typeof(Overloads[1]));  // prints void(int)
}

class Foo {
     int i();
     void i(int);
}
alias T!(Foo.i) Biz;



More information about the Digitalmars-d-learn mailing list