Copying a delegate

Stewart Gordon smjg_1998 at yahoo.com
Sun Sep 19 17:13:14 PDT 2010


On 19/09/2010 04:35, Jonathan M Davis wrote:
<snip>
> That doesn't work because you're just copying the pointer. Is there a way to
> actually do a deep copy of the delegate? I can see why this would be problematic
> if the delegate had reference types in its scope (since presumably, they'd have
> to be shallow copied unless you somehow had the equivalent of a copy constructor
> or postblit constructor for delegates), but even the ability to a do a shallow
> copy would be better than nothing. Is there any way to do that, or am I out of
> luck?

You mean to duplicate whatever the delegate's context pointer points to?

The trouble is that the context pointer could point to either an object 
or a stack frame.  Any code to duplicate a delegate on this basis would 
need to consider both possibilities.  Moreover, a class may or may not 
provide a dup method, and there's no predictable vtbl slot for it. 
Things get even more complicated when you realise that occasionally 
there may be an object allocated on the stack.

As such, this would be a complex feature, and IMO unlikely to be 
implemented.  I can't even think of any real practical use for it.  Can you?

Stewart.


More information about the Digitalmars-d-learn mailing list