need help to get member function const address

Steven Schveighoffer schveiguy at gmail.com
Mon Mar 16 18:43:47 UTC 2020


On 3/14/20 5:06 AM, Calvin P wrote:
> I use this code to get member function address on runtime:
> 
> =========
> struct A {
>         this(){};
> }
> auto ctor = (&__traits(getMember, A.init,"__ctor")).funcptr;
> =========
> 
> 
> my question is, how to get it in compile time like static function address:
> 
> =========
> struct A {
>       void d(){};
>       static void fn(){};

Note: no semicolons needed here

> }
> 
> enum FN = &A.fn;  // static method address is ok
> enum A0 = &(A.d).funcptr; // Error: need this for d of type void()

enum A0 = &A.d;

Note that you can't call it at all, but you can get the function 
pointer, and put it into a delegate later by assigning .funcptr.

void main()
{
     A a;
     void delegate() dg;
     dg.ptr = &a;
     dg.funcptr = A0;
     dg(); // calls a.d()
}

-Steve


More information about the Digitalmars-d-learn mailing list