Thread overview
template - aliasing a member function
Aug 08, 2012
Ellery Newcomer
Aug 08, 2012
Kenji Hara
Aug 09, 2012
Ellery Newcomer
Aug 09, 2012
Philippe Sigaud
Aug 08, 2012
Ellery Newcomer
August 08, 2012
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..
August 08, 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;

August 08, 2012
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

August 09, 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)

August 09, 2012
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?)