www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - need help to get member function const address

reply Calvin P <changlon gmail.com> writes:
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
=========
Mar 18 2020
next sibling parent Alex <sascha.orlov gmail.com> writes:
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.
Mar 18 2020
prev sibling next sibling parent reply Boris Carvajal <boris2.9 gmail.com> writes:
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
You asked the same question two days ago. Check the reply. https://forum.dlang.org/post/r4ohd3$2e94$1 digitalmars.com You can assemble a delegate with an struct pointer or class reference and a function member pointer.
Mar 19 2020
parent Calvin P <changlon gmail.com> writes:
On Thursday, 19 March 2020 at 06:34:40 UTC, Alex wrote:
 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.
I am not try to get the context, I just need the function address which is const and should able to get at compile time. On Thursday, 19 March 2020 at 09:13:42 UTC, Boris Carvajal wrote:
 On Thursday, 19 March 2020 at 04:30:32 UTC, Calvin P wrote:

 You can assemble a delegate with an struct pointer or class 
 reference and a function member pointer.
Sorry for duplicate thread. The last time I submit my question on web, it keep get blocked. I thought it was not submitted successfully, so I submit from draft again.
Mar 19 2020
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 19 March 2020 at 04:30:32 UTC, Calvin P wrote:
 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 A1 = (&__traits(getMember, A,"d")).funcptr; // Error: no 
 property funcptr for type void function()
Check the error message there... you already have a function pointer, no need to use the .funcptr metod. It is a bit weird though because it actually EXCLUDES the hidden argument for `this`. I prefer doing wrapper functions usually.
Mar 19 2020
parent Calvin P <changlon gmail.com> writes:
On Thursday, 19 March 2020 at 13:23:41 UTC, Adam D. Ruppe wrote:
 Check the error message there... you already have a function 
 pointer, no need to use the .funcptr metod.

 It is a bit weird though because it actually EXCLUDES the 
 hidden argument for `this`. I prefer doing wrapper functions 
 usually.
Thanks for the tips, I can get it into enum but not be able to use it as const at compile time. I come up with this workaround: static void* getCallee() pure nogc nothrow { enum callee_ptr = &(__traits(getMember, App, name)); return callee_ptr; } __gshared const AppHelper APP_HELPER = {&getCallee, ..};
Mar 19 2020