deep copying a struct

aliak something at something.com
Fri Sep 6 10:52:43 UTC 2019


On Friday, 6 September 2019 at 10:37:16 UTC, aliak wrote:
> Are there any library APIs that allow this:

I just put this together. Any holes other the AA related ones?

Will it work with classes?

auto dupDeep(T)(ref T thing) {
     import std.range: ElementType;
     import std.traits: hasAliasing, Unqual, isArray;

     static if (isArray!T) {
         Unqual!(ElementType!T)[] copy;
         foreach (elem; thing) {
             copy ~= elem.dupDeep;
         }
     } else static if (hasAliasing!T) {
         Unqual!T copy;
         foreach (i, field; thing.tupleof) {
             copy.tupleof[i] = field.dupDeep;
         }
     } else {
         Unqual!T copy;
         copy = thing;
     }
     return copy;
}



More information about the Digitalmars-d-learn mailing list