An struct copy constructor that can cope with an AA?
Steven Schveighoffer
schveiguy at gmail.com
Mon Mar 9 14:45:15 UTC 2020
On 3/9/20 9:23 AM, mark wrote:
> 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?
aa.dup. However, this isn't going to work in a const member function
(though technically there shouldn't be anything wrong with it).
> (2) YYY Is there a nicer way to copy a struct? Use a copy constructor or
> implement a dup or idup?
I would name it dup instead of copy for consistency with D. A copy
constructor is pretty heavy for a struct to do a complete duplication of
the AA. You should have to opt-in to that.
-Steve
More information about the Digitalmars-d-learn
mailing list