DMD 0.174 release
Don Clugston
dac at nospam.com.au
Wed Nov 15 07:07:47 PST 2006
Jarrett Billingsley wrote:
> "Kirk McDonald" <kirklin.mcdonald at gmail.com> wrote in message
> news:ejeepp$15n1$1 at digitaldaemon.com...
>> Jarrett Billingsley wrote:
>>> A few questions:
>>>
>>> 1) For the delegate .funcptr property, would it be possible for it to
>>> include the context pointer as the first parameter? Currently the
>>> .funcptr allows us to get the type / call signature of the delegate but
>>> it's not possible to actually use the resulting function as there is no
>>> way to pass it the context short of dipping into ASM..
>>>
>> Not so: Walter added a .ptr property to delegates in 0.168. Between that
>> and .funcptr, you've got all you need to mess with delegates right there.
>
> Well even with the context pointer, how do you propose passing it to the
> resulting func pointer? The context is passed implicitly as the first
> parameter to any delegate, and without that first parameter in the parameter
> list, you can't pass the context. So
>
> class A { void fork(int x) { } }
>
> A a = new A();
> auto dg = &a.fork;
> auto fp = dg.funcptr;
>
> fp(3); // access violation
>
> What's worse is that because the parameter list doesn't include the context,
> trying to call fp will result in all the arguments being shifted down a
> position from where they should be, i.e. 3 will now be in the 'this' slot,
> and nothing useful will be in the 'x' slot. :S So you _have_ to use ASM to
> call that func pointer, or maybe use some template trickery to create a
> pointer to the function with an extra first param.
I spent four months of my spare time getting something like that to work
in C++ for all compilers. But this is D. With tuples it's easy, you
could even say trivial!
R function(void *, U) makeCallable(R, U...)(R delegate(U) dg)
{
return cast(R function(void *, U))dg.funcptr;
}
More information about the Digitalmars-d-announce
mailing list