is this a bug? opUnary!"++" Error: var has no effect

Timon Gehr timon.gehr at gmx.ch
Wed Feb 8 13:58:45 PST 2012


On 02/08/2012 10:48 PM, Zach the Mystic wrote:
> My goal is to be able to overload the "++" operator transparently to the
> code, but I can't.
>
> import std.stdio;
>
> struct Arc {
> int I = 0;
> // This is void, but the error appears under all return types
> void opUnary(string op)() if( op == "++" ) {
> ++I;
> }
> }
>
> struct HasArc {
> Arc myArc;
> }
>
> void main() {
> HasArc has;
> writefln(" Arc.I = %s", has.myArc.I); // Arc.I = 0
>
> has.myArc++; // Error: var has no effect in expression (__pitmp1481)
>
> // Okay, try this instead:
> auto UselessVar = has.myArc++; // Works fine
>
> writefln(" Arc.I = %s", has.myArc.I); // Arc.I = 1
> }
>
> Obviously the expression has an effect. Bug? If not, what is the right
> way to have the "++" operator pass that error test?
>
> Thanks, Zach

This is indeed a bug.

The expression is rewritten internally into (pseudo code)

(auto __pitmp1481 = has.myArc, has.myArc.opUnary!"++"(), __pitmp1481);

This introduces a sub-expression with no effect. You may want to file a 
bug report. For now, just using ++has.myArc; should work around the issue.


More information about the Digitalmars-d-learn mailing list