Re: need help to get member function const address
On Thursday, 19 March 2020 at 23:46:01 UTC, Boris Carvajal wrote: https://issues.dlang.org/show_bug.cgi?id=20687 https://github.com/dlang/dmd/pull/10946 Thanks very much for such a quick fix.
Re: need help to get member function const address
On Thursday, 19 March 2020 at 14:43:53 UTC, Steven Schveighoffer wrote: I think this is an invalid limitation, you should file an issue at https://issues.dlang.org essentially, with your sample A struct: const f1 = // OK const f2 = // Error, not constant expression Both these should be valid, is not a delegate, but a function pointer. -Steve https://issues.dlang.org/show_bug.cgi?id=20687 https://github.com/dlang/dmd/pull/10946
Re: need help to get member function const address
On 3/19/20 9:40 AM, Calvin P wrote: On Monday, 16 March 2020 at 18:43:47 UTC, Steven Schveighoffer wrote: enum A0 = 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 = dg.funcptr = A0; dg(); // calls a.d() } Thanks for the tips, Steve. I need to put them into a big const struct to use on runtime, so they can not be modify on runtime by mistake. I can not put the address into enum, because Error: need this to access d add cast(void*) still get same error: enum callee_ptr = cast(void*) &(__traits(getMember, A, "d")); // Error: need this to access d but I come up with a workaround: static void* getCallee() pure @nogc nothrow { enum callee_ptr = &(__traits(getMember, App, name)); return callee_ptr; } __gshared const AppHelper APP_HELPER = {, ..}; I'm not sure this is what you want. Essentially the address of the getCallee function is going to be put into AppHelper, so you are still fetching the function address at runtime. What might be best is simply to use a static ctor to build the struct you want (as long as it's not betterC): const AppHelper APP_HELPER; share static this() { APP_HELPER = ...; } It's kind of strange to me that you can't evaluate but you can evaluate at compile time. Even stranger that it works as an enum, but not as a const function pointer. I too cannot find a way to make it work. I think this is an invalid limitation, you should file an issue at https://issues.dlang.org essentially, with your sample A struct: const f1 = // OK const f2 = // Error, not constant expression Both these should be valid, is not a delegate, but a function pointer. -Steve
Re: need help to get member function const address
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 = {, ..};
Re: need help to get member function const address
On Monday, 16 March 2020 at 18:43:47 UTC, Steven Schveighoffer wrote: enum A0 = 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 = dg.funcptr = A0; dg(); // calls a.d() } -Steve Thanks for the tips, Steve. I need to put them into a big const struct to use on runtime, so they can not be modify on runtime by mistake. I can not put the address into enum, because Error: need this to access d add cast(void*) still get same error: enum callee_ptr = cast(void*) &(__traits(getMember, A, "d")); // Error: need this to access d but I come up with a workaround: static void* getCallee() pure @nogc nothrow { enum callee_ptr = &(__traits(getMember, App, name)); return callee_ptr; } __gshared const AppHelper APP_HELPER = {, ..};
Re: need help to get member function const address
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 = // 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.
Re: need help to get member function const address
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.
Re: need help to get member function const address
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.
Re: need help to get member function const address
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 = // 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: (().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.
need help to get member function const address
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 = // 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: (().d).funcptr cannot be evaluated at compile time =
Re: need help to get member function const address
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 = // static method address is ok enum A0 = &(A.d).funcptr; // Error: need this for d of type void() enum A0 = 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 = dg.funcptr = A0; dg(); // calls a.d() } -Steve
need help to get member function const address
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 = // 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: (().d).funcptr cannot be evaluated at compile time =