Re: Side effects in conditional expressions

August Karlstrom wrote:
On 2010-10-02 14:28, Edward wrote:
It depends. If you're entering the IOCCC, it's acceptable if a little
too readable. Anywhere else, it's asking for trouble. The harder you
have to work to understand the code, the more likely subtle bugs are to
get past you

Exactly. As I have already mentioned in this thread I wouldn't write this kind of code myself. Would you also reject the string copy idiom

while (*t++ = *s++);

with the same motivation?


No, I think that's reasonable (essentially it's describing an LDCPIR, which one would hope the compiler would be clever enough to optimise it into), except that if using the result of an assignment as a condition you should enclose in an extra set of brackets. Typically your compiler should warn you otherwise (as you might have meant == instead). Of course, unless you want to compare to something other than zero, string.h's strcpy() would be more appropriate.
The interesting one, though, is if you want to copy within an array, perhaps even copying between regions which overlap - since then you need to be able to choose whether it LDIRs or LDDRs. In that case you might have something like
to shift down, but
to shift up (of course you'd have to initialise i appropriately).
You have to be careful, though, not to invoke UB with something like that (hence why I've used separate comma expressions to inc/dec i).

"Sanity" is the last refuge of the unimaginative.
visit my useless website --