Struct copy, how?

Daniel Keep daniel.keep.lists at gmail.com
Sun Jan 4 00:36:43 PST 2009



nobody wrote:
> "bearophile" <bearophileHUGS at lycos.com> wrote in message 
> news:gjn7s1$1vk4$1 at digitalmars.com...
>> nobody:
>> BCS:
>>>> However this still has a few problems: 1, if claw contains a reference
>>>> type you will now have 2 Claws that refer to the same thing and 2, you
>>>> need to maintain opAssign to be sure that it copies all members.
>>>>
>>>> One way to attack both of these would be to use a template to build a
>>>> generic deep copy that uses compile time refection to copy all the
>>>> members, duping arrays of PODS, copying basic types and recursively 
>>>> deep
>>>> copying reference types and arrays of them.
>>> Guess I'll do that.
>>> Thanks for the help!
>> If you create a generic deep copy I may find it useful.
>>
>> Bye,
>> bearophile
> 
> Heh, unfortunately I wouldn't even know where to begin in order to make 
> something like that. 

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


More information about the Digitalmars-d-learn mailing list