need help to get member function const address

Alex sascha.orlov at gmail.com
Thu Mar 19 06:34:40 UTC 2020


On Thursday, 19 March 2020 at 04:30:32 UTC, 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(){};
> }
>
> enum FN = &A.fn;  // static method address is ok
> enum A0 = &(A.d).funcptr; // Error: need this for d of type 
> void()
> enum A1 = (&__traits(getMember, A,"d")).funcptr; // Error: no 
> property funcptr for type void function()
> enum A2 = (&__traits(getMember, A.init,"d")).funcptr; //  
> Error: (&A().d).funcptr cannot be evaluated at compile time
> =========

A non-static member method can use the context of the struct 
where it is defined in. E.g. it could alter a member variable.
This context has to be constructed at run time (and there could 
be many instances of the context) and does not exist in compile 
time. Therefore the difference to the static method.


More information about the Digitalmars-d-learn mailing list