Given int foo[] = {0, 1, 2, 3};
I want to know if iterators that point past the "one past-the-end" are invalid. For example: auto bar = cend(foo) + 1;
There are a ton of complaints and warnings that this is "undefined behavior" in Stack Overflow questions like this: c++ what's the result of iterator + integer when past-end-iterator? Unfortunately the only source is hand waving.
I'm having more and more trouble buying that, for example:
int* bar;
Is uninitialized, but certainly does not invoke undefined behavior, and given enough tries I'm sure I could find an instance where the value in this uninitialized bar
had the same value as cend(foo) + 1
.
One of the big confusions here is that I am not asking about dereferencing cend(foo) + 1
. I know that would be undefined behavior and the standard forbids it. But answers like this: https://mcmap.net/q/41431/-are-end-1-iterators-for-std-string-allowed which cite only that dereferencing such an iterator is illegal do not answer the question.
I also know that C++ only guarantees that cend(foo)
will be valid, but it could be numeric_limits<int*>::max()
, in which case cend(foo) + 1
would overflow. I'm not interested in that case unless it is called out in the standard as the reason we can't have an iterator past the "one past-the-end". I know that int*
really just holds an integer value, and as such is subject to overflow.
I would like a citation from a credible source that moving an iterator beyond the "one past-the-end" element is undefined behavior.
s
andt
point to is the "one past the end" which is a defined element to point to. This question is about the one past the "one past the end pointer, or you might say "two past the end". – Bernardinabernardine