need help to get member function const address
Steven Schveighoffer
schveiguy at gmail.com
Thu Mar 19 14:43:53 UTC 2020
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 = &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()
>> }
>>
>
> 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 = {&getCallee, ..};
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 &A.d but you can
evaluate &A.fn 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 = &A.fn; // OK
const f2 = &A.d; // Error, not constant expression
Both these should be valid, &A.d is not a delegate, but a function pointer.
-Steve
More information about the Digitalmars-d-learn
mailing list