ISO IEC 14882-2011 §5.7/5 States:
If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.
This section is used here on stackoverflow from time to time. For instance to argue why the increment of a pointer to nullptr
is UB like here. It is then interpreted as, having a pointer that does not point to an element of an array object. Is undefined behaviour.
However, when I read this I understood it to refer to the evaluation of the pointer being UB. Which would mean that having such a pointer is well defined behaviour. And the behaviour becomes undefined when one tries to dereference it.
Which would mean that for example, incrementing a valid pointer beyond the array boundary is legal. Decrementing it again afterwards is legal. And since the pointer will then be the same value as before the increment, the evaluation is legal too.
Which of the two is the case?