5

If i is an int, expressions like ++i + ++i are undefined behavior since there are 2 unsequenced modifications of i. However, if i is some int-like class, ++i + ++i instead has indeterminately sequenced modifications, and is therefore defined behavior (with a deterministic result in this case). Is there a case where it would be better to have operations on primitives be unsequenced instead of indeterminately sequenced? If so, why doesn't this case apply to user-created types? If not, why are primitive operations unsequenced at all?

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
m42a
  • 1,322
  • 2
  • 10
  • 14
  • 2
    Being undefined behaviour presumably allows the compiler to apply certain classes of optimisations. It's unlikely that these optimisations apply to UDTs, in general, as ++i is then just syntax sugar for a function call. – Oliver Charlesworth Apr 01 '13 at 15:08

1 Answers1

4

Generally, there is as much "unsequenced" as possible.

It is not possible to make two functions run interleaving in C++. Therefor, it is not possible to run two operator++ implementations interleaving.

Therefor, for class types implementing operator++, this is indeterminately sequenced.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212