I'm implementing std::vector-like class. Its iterators are just plain pointers. The problem is that if the vector is empty (there is no allocated buffer for elements) it returns nullptr as begin and end iterators. Are null pointers valid iterators?
I tried to learn std::vector source code in GNU C++ standard library. It looks like they use the same approach with null pointers.
But what about using std::distance with null pointers? It is defined as last - first for random access iterators. But it is not valid to subtract null pointers (at least in pure C). Ok, they can compare iterators before subtracting (comparing null pointers is valid), and if they are the same, return 0 without subtracting.
But anyway, last - first is assumed to be valid expression for random access iterators, but it is not because subtracting null pointers is undefined behavior.
If null pointers can't be used as iterators, what can I use instead?
Update. Ok, subtracting null pointers is valid. Are there any other reasons why null pointer can't be valid iterator?
