Restricting ++ and --

Justin Johansson no at spam.com
Sun Oct 25 05:54:21 PDT 2009


bearophile Wrote:

> This post is born from a bug I've just removed.
> In the past I have read more than one C coding standard (or better, lists of coding tips) that warn against bugs caused by ++ and --. They suggest to not use them compound in expressions. They allow to use them when alone on an instruction.
> 
> Python designers have totally avoided to put those operators in the language, with the rationale they are bug-prone while saving just a little of typing.
> 
> Removing those operators from D, as Python, may look excessive. So a possible compromise can be:
> - Deprecate the pre versions:  --x  and ++x
> - Make them return void, so they can't be used as expressions like this:
> y = x++;
> foo(x--);
> You have to use them as:
> x++; y = x;
> x--; foo(x);
> (So ++ and -- become similar to the Inc() and Dec() functions of Pascal).
> 
> What problems such changes may cause?
> 
> Bye,
> bearophile

In the absence of optimization it is well known that pre-inc/dec operations are
faster that post-inc/dec operations by simple reason of the number of
memory-register-memory transfers required (at least historically this was so).

Accordingly since the year dot I have always coded my for loops like so:

for ( int i = 0; i < n; ++i) { ... }

rather than

for ( int i = 0; i < n; i++) { ... }

It would be easier for me to give up red wine than the pre-inc habit.

To me the proposal sounds over zealous.  I don't think much of
nanny-state governments either.

Cheers
Justin Johansson




More information about the Digitalmars-d mailing list