Make `& Class.foo` illegal
H. S. Teoh
hsteoh at quickfur.ath.cx
Fri Aug 28 05:27:13 UTC 2020
On Fri, Aug 28, 2020 at 12:25:15AM +0000, sarn via Digitalmars-d wrote:
[...]
> I don't know if you've seen them before, but these two articles
> explain the reasoning behind D not having member function pointers:
> https://www.drdobbs.com/cpp/member-function-pointers-in-d/231600610
> https://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible
[...]
Thanks! I was on the fence before, but these two articles convinced me
that member function pointers are not worth the trouble. Just use a
delegate instead, or a lambda that receives the target object as a
parameter:
class C {
void method1(int x);
void method2(int y);
}
C c1 = ...;
C c2 = ...;
void function(C, int) mfp;
mfp = (C self, int arg) { return self.method1(x); }
mfp(c1, 1); // calls c1.method1(1)
mfp(c2, 1); // calls c2.method1(1)
mfp = (C self, int arg) { return self.method2(x); }
mfp(c1, 1); // calls c1.method2(1)
mfp(c2, 1); // calls c2.method2(1)
This works even if the method to be bound is static. None of the
specialcasing and pathological corner-cases of actual member function
pointers.
I say we should just forbid taking the address of non-static member
functions.
T
--
To err is human; to forgive is not our policy. -- Samuel Adler
More information about the Digitalmars-d
mailing list