Struct copy, how?
Daniel Keep
daniel.keep.lists at gmail.com
Sun Jan 4 16:51:58 PST 2009
Christopher Wright wrote:
> 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.
Obviously, you'd need to have the class support it itself; but the
original question was in regards to a struct deep copy. :P
For reference, I've written a generic serialise/deserialise pair of
functions that do, more or less, the same thing, so it can be done.
-- Daniel
More information about the Digitalmars-d-learn
mailing list