Pointers to non-static member functions!
Daniel Murphy
yebblies at nospamgmail.com
Wed Jun 8 08:19:12 PDT 2011
"Steven Schveighoffer" <schveiguy at yahoo.com> wrote in message
news:op.vwridsuteav7ka at localhost.localdomain...
>
> Yes, but removing type safety does not prevent that from happening, plus
> it allows something like this:
>
> class A
> {
> void func();
> int func2();
> }
>
> void main()
> {
> auto a = new A;
> auto dg = &a.func2();
> dg.funcptr = &A.func; // works if dg.funcptr and &A.func are void*, but
> doesn't work today
> int x = dg(); // runs and returns garbage
> }
>
Fun fact: due to bug3797 if you change basically anything except the return
type, it _will_ work today!
> I almost would prefer that hacking delegates would be illegal. Yes, you
> can get the function pointer and data pointer out of a delegate, but
> cannot set them. I can't think of any good use cases for them.
>
I think you're right on this. I'll have a look and see how difficult making
the delegate properties readonly is. I have actually needed to manually
construct a delegate when doing low level calling convention stuff, but you
can always do this using unions or casts if you have to.
> I'm mixed on whether you should be able to get a member "function
> pointer". It would be nice to simply make it a normal function pointer
> that you can call with the appropriate type.
>
> BTW, if the calling convention is different, we may be able to allow it
> via creating a "this" function pointer:
>
> assert(is(typeof(&A.func2) == int function(A this)));
>
> Since this is a keyword, it can be interpreted as a different calling
> convention.
>
> -Steve
I can see the theoretical appeal of having member function pointers or being
able to call member functions as if they were globals, but I don't think the
practical uses really justify introducing new syntax or types or changing
the ABI. I also don't think any option which does any of those things has a
chance of getting incorporated into dmd.
If I can get some kind of consensus I'll try and patch dmd.
I think the following covers most of your points, given that you can still
do the unsafe thing by casting:
Make delegate.funcptr read only and return void*
Make &ClassType.nonstaticmemberfunction return void*
More information about the Digitalmars-d
mailing list