Struct copy, how?

Christopher Wright dhasenan at gmail.com
Sun Jan 4 07:38:57 PST 2009


Daniel Keep wrote:
> Off the top of my head, it wouldn't be terribly hard.
> 
> What you would need is a global 'ddup' (deep-dup) function.  The generic 
> case would look something like:
> 
> T ddup(T)(ref T value)
> {
>     T result;
>     foreach( i,f ; value.tupleof )
>         result.tupleof[i] = ddup(f);
>     return result;
> }
> 
> You'd then have to use either specialisation or lots of static if's to 
> account for reference types, allocating new memory as necessary.
> 
> Can't see any particular reason why you couldn't do it...
> 
>   -- Daniel

You want ddup to be polymorphic. Let's say you have a class with no 
default constructor, or a class that starts listening on a socket with 
some default local port -- in the first case, you couldn't ddup it 
without some ugliness; in the second, you could ddup it, but you'd get 
an exception because the port's already in use.

It would suffice to have an IClonable interface, and to check structs 
for a ddup property.


More information about the Digitalmars-d-learn mailing list