www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - template - aliasing a member function

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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..
Aug 08 2012
next sibling parent reply "Kenji Hara" <k.hara.pg gmail.com> writes:
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;
Aug 08 2012
parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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)
Aug 08 2012
prev sibling next sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 08/08/2012 04:21 PM, Ellery Newcomer wrote:
 mangleof looks promising..

.. or maybe not. wtf? template Z(string s) { pragma(msg, "fn.mangleof 2: " ~ s); } struct S(alias fn, string prop) { pragma(msg, "fn.mangleof 1: " ~ fn.mangleof); alias Z!(fn.mangleof) F; } class Foo{ property int i(){ return 1; } } void main() { alias S!(Foo.i,"") SA1; } fn.mangleof 1: _D3erg3Foo1iMFNdZi fn.mangleof 2: FNdZi
Aug 08 2012
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Thu, Aug 9, 2012 at 2:00 AM, Ellery Newcomer
<ellery-newcomer utulsa.edu> wrote:

 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)

Ellery, you might be interesting in reading sections 20.4-20.8 from a tutorial on templates you can get there: github.com/PhilippeSigaud/D-templates-tutorial Download the pdf. (is it me or github is down?)
Aug 08 2012