full copies on assignment

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 27 01:59:43 PDT 2016


On Thursday, 26 May 2016 at 10:51:30 UTC, John Nixon wrote:
> On Wednesday, 25 May 2016 at 15:44:34 UTC, Marc Schütz wrote:
>> 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:
>
>> Or add an explicit constructor:
>>
>>     struct CS {
>>         // ...
>>         this(const CS rhs) { this = rhs; }
>
> Unfortunately this results in "Error: cannot implicitly convert 
> expression (rhs) of type const(CS) to CS'.

Hmm... this is the full program that works for me:

import std.stdio;

struct CS {
     char[] t;
     this(const CS rhs) {
         this = rhs;
     }
     CS opAssign(const CS rhs) {
         writeln("CS.opAssign called");
         this.t = rhs.t.dup;
         return this;
     }
};

void test_fun(const ref CS rhs) {
     auto cs = CS(rhs);
     writeln("cs = ",cs);
}

void main() {
     CS rhs;
     rhs.t = "string".dup;
     test_fun(rhs);
     return;
}



More information about the Digitalmars-d-learn mailing list