Warn about do nothing expressions?

Frustrated Who at where.com
Sat Mar 29 09:31:47 PDT 2014


On Friday, 28 March 2014 at 19:57:13 UTC, MattCoder wrote:
> On Friday, 28 March 2014 at 19:35:22 UTC, Frustrated wrote:
>> which means, i = is the previous value of j(before increment).
>> This should hold true when j is an alias for i.
>
> In this case: i = i++;  Means:
>
> "i" is incremented by one, but the value assigned in "i" is the
> previous one. That's it.
>
> For me there's nothing wrong here.
>
> Matheus.


One thing you are getting wrong/leaving out is that i++, even in 
an assignment, increments i.

i = j++;

even if i is assigned the previous value of j, j STILL gets 
incremented by one.

Change i to j and it will work out.

Only if a temp value can the D behavior work as it does. This is 
wrong though because not all compilers have to use temp values.

Again, it is also inconsistent with the fact that something 
should get incremented.

"If ++ follows the variable, e.g. counter++, the value returned 
is the value in counter before it has been incremented. "

i = i++;
=> i = i; // value before i is incremented
    i++;   // now it is incremented.

which increments i due to last statement.

You can't increment i until you return the value.


mov ax, [i]  // put i in a register
mov ax, ax   // i = i // easily optimized out
inc ax       // i++
mov [i], ax

vs

mov ax, [i]
mov bx, ax
mov ax, bx
inc bx
mov [i], ax

I think most people would agree that the 2nd case is worse(one 
extra register is used for no real reason except to avoid the 
increment).






More information about the Digitalmars-d mailing list