Warn about do nothing expressions?

Dominikus Dittes Scherkl Dominikus.Scherkl at continental-corporation.com
Mon Mar 31 09:18:11 PDT 2014


On Monday, 31 March 2014 at 15:56:29 UTC, John Colvin wrote:
>> m_takeIndex = m_takeIndex++; should do exactly the same.
>
> Nope. Nope nope nope. That's not what postfix increment is. 
> That's why prefix increment exists.
>
> Yeah, it might be an odd design (dates back to poor optimising 
> compilers and fast INC/DEC instructions apparently), but that's 
> how it is in C and how it is in D, by inheritance.

I just don't see, why one would expect the assignment could come 
first?!?
The post-increment has a much, much higher priority, so I would 
think it is obvious that first the value is incremented and 
afterwards the old value is assigned to it.
And of course, the old, non-incremented value is assigned. This 
is the essence od POST incrementing - the return value of this 
operator is defined to be the old value, that's the whole point 
why this operator exists. (btw. I would _never_ever_ write a line 
like "i++;" That's garbage. It should always be "++i;"
Yes, the optimizer will get rid of the temporary variable, but it 
is absolutely unintuitive (if I read something like that my first 
thought is: what is the
old value used for? Why using a temporary variable?)

But I fully agree that such a form of complicated NOP ("i = 
i++;") should be forbidden or at least warned about.


More information about the Digitalmars-d mailing list