Make `&` illegal

H. S. Teoh hsteoh at
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:

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

	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

I say we should just forbid taking the address of non-static member


To err is human; to forgive is not our policy. -- Samuel Adler

More information about the Digitalmars-d mailing list