template - aliasing a member function

Ellery Newcomer ellery-newcomer at utulsa.edu
Wed Aug 8 17:00:21 PDT 2012


On 08/08/2012 04:37 PM, Kenji Hara wrote:
>
> 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;
>

Awesome, thanks.

And to make this work with property functions (which is my real target 
case):

     pragma(msg, FunctionTypeOf!(Overloads[0]));  // prints @property int()
     pragma(msg, FunctionTypeOf!(Overloads[1]));  // prints @property 
void(int)



More information about the Digitalmars-d-learn mailing list