On Monday, 31 March 2014 at 18:16:32 UTC, monarch_dodra wrote:
On Monday, 31 March 2014 at 16:18:13 UTC, Dominikus Dittes Scherkl wrote:
(btw. I would _never_ever_ write a line like "i++;" That's garbage. It should always be "++i;"

Using "i++" can make a lot of sense, especially when you are iterating indexes.
Of course the post-increment is often very usefull - iff you use
it's return value. Therefore the semicolon.
i++;
as a whole line is very bad style, I think.

i = i++;

This does not conform to spec because "i" is written to and read to, all in between two sequence points. This is illegal, and produces undefined behavior.
Why?
If the operators had the same precedence, ok.
But ++ has to come first (no matter if post or pre).
something like
j = --i++;
yeah, that is undefined (pre- and post having same priority).

But does the spec really say a value may not at the same time be read from and written to? Then i=i; would also be illegal. And how about i += i; ? I think this is allowed and useful, ne? What about i = ++i; (ok does nothing diffrent from ++i; but is still ok and not a NOP)?

i = i++; is unambigous if the compiler doesn't optimize it. And if it optimizes, it should replace it by NOP. But it is likely that this is a typo or a logic-bug by the user, so the compiler should instead warn or even forbid it.

As a general rule in one expression one variable should not be
allowed to be modified more than once.

e.g.
i++ = i;
j = i++ / i++;
i += i++;
etc. should all be forbidden.

Go! Go! Undefined behavior!

Reply via email to