Copying array with const correctness
Vindex9
tech.vindex at gmail.com
Wed Oct 8 08:54:20 UTC 2025
Here's a slightly better solution. The lines will be copied.
```d
T[] copyArray(T)(inout(T)[] arr) {
T[] copy = new T[arr.length];
copy.length = arr.length;
static if (is(T == U[], U) && !is(T == immutable(Y)[], Y)) {
foreach(i, ref v; copy) {
v = copyArray(arr[i]);
}
} else {
for (size_t i = 0; i < arr.length; i++) {
static if (is(T == immutable(Y)[], Y)) {
copy[i] = arr[i].idup;
} else {
// it doesn't work with postblit, only with
modern copy ctor
T elem = arr[i];
copy[i] = elem;
}
}
}
return copy;
}
```
More information about the Digitalmars-d-learn
mailing list