cloning arrays

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 10 15:54:06 PDT 2016


On Saturday, September 10, 2016 17:58:37 Russel Winder via Digitalmars-d-learn 
wrote:
> What is the idiomatic way of cloning an array?
>
> I had wondered about x.dup but am now worried this is not creating a
> shallow copy.

dup creates an array with mutable elements that are copies of what was in
the original array. How deep a copy that is depends on the type. It does
exactly as deep a copy as simply copying the element would do. e.g.

auto e = arr[4];

So, unless postblit constructors are involved, then it's not going to be
doing any deep copying, and if a postblit constructor that does a deep copy
is involved, then any other copying you'd do with it would be deep too
unless you explicitly bit-blitted it rather than copying it, and that's
usually a bad idea.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list