full copies on assignment

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 25 08:44:34 PDT 2016


On Tuesday, 24 May 2016 at 20:58:11 UTC, John Nixon wrote:
> On Tuesday, 24 May 2016 at 15:17:37 UTC, Adam D. Ruppe wrote:
>> On Tuesday, 24 May 2016 at 14:29:53 UTC, John Nixon wrote:
>>> This naively doesn’t seem right because the RHS of an 
>>> assignment should not be altered by it.
>>
>> It's because the char[] being shallow copied still leads to 
>> mutable stuff.
>>
>> What I typically do here is just add a method `dup` to the 
>> struct that deep copies. Then you do `CS cs = rhs.dup;` when 
>> you want to construct it, like you'd do with a copy of a naked 
>> array.
>
> Thank you for this suggestion, perhaps a slightly neater 
> workaround.

Or add an explicit constructor:

     struct CS {
         // ...
         this(const CS rhs) { this = rhs; }
     }

Then you can write:

     auto cs = CS(rhs);



More information about the Digitalmars-d-learn mailing list