In Another way of looking at C++ reverse iterators Raymond Chen wrote:
... a quirk of the C++ language: You are allowed to have a pointer "one past the end" of a collection, but you are not allowed to have a pointer "one before the beginning" of a collection.
I understand that it probably means "undefined behavior" and that is pretty much a conversation ender. But I am curious what is the worst that can happen in a realistic system if one ignores this rule. A segmentation fault? an overflow of pointer arithmetic? unnecessary paginations?
Remember that the pointer "before" the beginning (like "end") is not supposed to be referenced either, the problem seem to have the pointer just trying to point to it.
The only situation I can imagine it can be a problem is with a system where memory location "0" is valid. But even then, if that is so there are bigger problems, (e.g. nullptr would be problematic in itself too and wrapping around might still work by convention I guess.)
I am not questioning the implementation of reverse_iterator with an off-by-one special code.
The question occurred to me because if you have a generic implementation of "strided" iterator would require a special logic for negative strides and that has a cost (in code and at runtime).
Arbitrary strides, including negative ones could be naturally appearing with multidimensional arrays. I have a multidimensional array library in which I always allowed negative strides in principle, but now I realized that they should have a special code (different from the positive case) if I allowed them at all and I don't want to expose undefined behavior.