Converting function pointers to delegates

Ryan Voots simcop2387 at simcop2387.info
Mon Apr 14 10:45:50 PDT 2014


I'm porting some code from D1 to D2 and this is the final error 
i'm getting from DMD and GDC.  I have no idea what is going on 
with it though.

/**
  * Convert any function pointer to a delegate.
  * _ From: 
http://www.digitalmars.com/d/archives/digitalmars/D/easily_convert_any_method_function_to_a_delegate_55827.html 
*/
R delegate(P) toDelegate(R, P...)(R function(P) fp)
{	struct S
	{	R Go(P p) // P is the function args.
		{	return (cast(R function(P))(cast(void*)this))(p);
		}
	}
	return &(cast(S*)(cast(void*)fp)).Go;
}

DMD tells me:
src/yage/core/misc.d(164): Error: e2ir: cannot cast this of type 
S to type void*


The code itself you can see at: 
https://bitbucket.org/simcop2387/yage/src/0542cea5eefabf4c90b822cf26ec1062ed0fcb64/src/yage/core/misc.d?at=default#cl-161

and where it gets called: 
https://bitbucket.org/simcop2387/yage/src/0542cea5eefabf4c90b822cf26ec1062ed0fcb64/src/yage/core/repeater.d?at=default#cl-47

To be perfectly honest I'm not sure what it's doing to figure out 
how to fix it.


More information about the Digitalmars-d-learn mailing list