An struct copy constructor that can cope with an AA?

mark mark at qtrac.eu
Mon Mar 9 13:23:19 UTC 2020


I have this struct:

struct Deb {
     string name;
     ...
     Unit[string] tags; // set of tags

     Deb copy() const {
         Deb deb;
         ...
         foreach (key, value; tags) // XXX
             deb.tags[key] = value;
         return deb;
     }

     void clear() {
         name = "";
         ...
         tags.clear;
     }

...
}

I am populating an AA with these structs:

Deb[string] debForName;

I'm using this approach (pseudo-code):

Deb deb;
foreach (datum; data) {
     populateDeb(datum, deb);
     debForName[deb.name] = deb.copy; // YYY
     deb.clear;
}

(1) XXX Is there a nicer way to copy an AA?
(2) YYY Is there a nicer way to copy a struct? Use a copy 
constructor or implement a dup or idup?


More information about the Digitalmars-d-learn mailing list