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

aliak something at something.com
Mon Feb 12 03:13:43 UTC 2018


Hi,

Is there a way to get post increment and pre increment working 
properly in this scenario?

import std.stdio;

struct A {
     int[] a;
     this(int a) { this.a = [a]; }
     auto opUnary(string op)(){
         return A(mixin(op ~ "this.a[0]"));
     }
}

void main() {
     auto a = A(0);
     int b = 0;
     writeln(a++, ":", b++); // 1, 0 <-- wrong post increment 
result
     writeln(++a, ":", ++b); // 2, 2
}

If I switch the order of the mixin expression (i.e. "this.a[0]" ~ 
op) then the pre increment does not work.

Any tips?

Cheers
- Ali


More information about the Digitalmars-d-learn mailing list