opUnary with ++ and -- on a struct that has a dynamic array

aliak something at something.com
Mon Feb 12 08:10:03 UTC 2018


On Monday, 12 February 2018 at 06:16:21 UTC, rumbu wrote:
> writeln(a++) translates to:
>
> A copy = a;
> a.opUnary!"++";
> writeln(copy);
>
> copy.a[] and a.a[] are the same reference, you increment 
> a.a[0]/copy.a[0] in opUnary
>
> to make this work you will need a postblit constructor:
>
> struct A
> {
>    ....
>    this(this)
>    {
>      a = a.dup; //make a copy a;
>    }
> }
>
> In fact, you'll find exactly the same scenario in docs: 
> https://dlang.org/spec/struct.html#struct-postblit
>

Ah! Awesome. Yes that works, thank you!

Cheers




More information about the Digitalmars-d-learn mailing list