3 ms·
In C++ this is easy, assuming that 'a' is a value of a custom class.
by Koshkin 9y ago
In C++ this is easy, assuming that 'a' is a value of a custom class.
- kazinator 9y agoC++ being a dialect of C, you can simply make a a preprocessor macro expanding to i++. The expression uses the && operator, which is properly sequenced so no undefined behavior occurs in that case. Though you might think that it's an incredibly bad idea, in fact the C standard library allows the getc and getchar functions to be defined as macros, and in fact they historically have been. Thus something like getc(stream) << 8 + getc(stream) stuffs multiple side effects into the same expression without a sequence point (not to mention neglecting to check the return values for EOF). Each getc call wants to inspect and increment some buffer pointer, like #define getc(s) ((s)->in < (s)->in_filled \ ? (s)->buf[(s)->in++] \ : __real_getc(s)) We're not okay on account of the ?: sequencing, because the expression (A?B:C)+(X?Y:Z) doesn't sequence A relative to Z; there no sequence point between them. The two branches of the + can evaluate in parallel/interleaved fashion.
- mark-r 9y agoIn C or C++ you can't rely on multiple ++ in a single statement doing anything sensible.
- deleted 9y ago[deleted]
- cozzyd 9y agoIn this case I think you can since I think && is a sequence point. a + a on the other hand would be undefined.
- foxhill 9y agoactually it is defined, i believe you mean sequence point - in which case multiple updates are indeed undefined. however, unless i'm mistaken, a && b && c is 3 distinct sequence points, in which case, the behaviour is in fact well-defined.