Post increment and decrement

Don via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 12 01:12:53 PDT 2015


On Thursday, 12 March 2015 at 04:06:14 UTC, Rikki Cattermole 
wrote:
> On 12/03/2015 1:50 p.m., Andrei Alexandrescu wrote:
>> On 3/11/15 10:23 AM, welkam wrote:
>>> Observation Nr. 1
>>> People prefer to write var++ instead of ++var.
>>>
>>> Observation Nr. 2
>>> Because of observation Nr. 1 and other reasons compilers 
>>> became good at
>>> removing code that is not needed making var++ and ++var to 
>>> produce the
>>> same code if returned value is not used.
>>>
>>> Observation Nr. 3
>>> Because of observation Nr. 2 more people use var++ in place 
>>> where they
>>> really only need ++var.
>>>
>>> Observation Nr. 4
>>> Because of observation Nr. 3 people learning to program may 
>>> mistakenly
>>> learn that var++ is just incrementing. (I am included in that 
>>> list)
>>>
>>> Observation Nr. 5
>>> Because of observation Nr. 4 people can write slower than 
>>> necessary code
>>> for classes with overloaded operator or even get bugs.
>>>
>>> Because of all this why not make only one increment/decrement 
>>> operator
>>> and have post increment/decrement to be called by template 
>>> name, because
>>> it is a template?
>>>
>>> template post_inc(T) {
>>> auto tmp = T;
>>> T++;
>>> return tmp;
>>> }
>>
>> Observation Nr. 6
>> Somebody didn't Read The Fine Manual. Page 369:
>>
>> =========
>> If the result of a++ is not needed, the rewrite is ++a, which 
>> is
>> subsequently rewritten to a.opUnary!"++"().
>> =========
>>
>>
>> Andrei
>
> +1
> Compiler should work for you. This is one of those things it 
> can rewrite to preference. During optimization.

It doesn't even rely on the optimizer. This happens in the 
front-end, in the semantic pass.



More information about the Digitalmars-d mailing list