1

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?

anton_rh
  • 8,226
  • 7
  • 45
  • 73
  • Why would be undefined? `nullptr` evaluates to `0`. `0-0 = 0`. – simre May 14 '22 at 18:51
  • 1
    A type can have a null pointer to the type. And you can subtract two of them which, of course, yields 0. You can't de-reference them – doug May 14 '22 at 18:52
  • 3
    I think you are looking for this: https://stackoverflow.com/questions/27681633/subtraction-of-two-nullptr-values-guaranteed-to-be-zero Maybe a dup? – simre May 14 '22 at 18:52
  • 2
    @simre, see [this](https://stackoverflow.com/q/55747642/5447906) – anton_rh May 14 '22 at 18:52
  • 2
    @anton_rh That is C! This is C++. In C++ it is totally fine. – simre May 14 '22 at 18:53
  • @simre C++ allows subtraction between two null pointers, but it is not due to "evaluates to `0`" (in part because null pointers do not "evaluate" to `0` in C++). The fact that C (where "evaluates to `0`" has more merit) does not allow this subtraction should tell you that your justification is lacking. Right conclusion, wrong rationale. – JaMiT May 14 '22 at 19:34
  • @JaMiT nullptr is 0: https://en.cppreference.com/w/cpp/types/nullptr_t -> https://en.cppreference.com/w/cpp/types/NULL So even if it wouldn't be explicitly defined, nor explicitly "disallowed", this subtraction would be well defined on the same types... Nullptr is a fancy zero. :D Btw I understand you... – simre May 14 '22 at 19:52
  • @JaMiT Try to read it again and understand what you read because it seems like you don't... Also from the standard: It is kinda zero: https://eel.is/c++draft/lex.nullptr#1 It is kinda zero: https://eel.is/c++draft/conv.ptr#1 It is kinda zero: https://eel.is/c++draft/expr.reinterpret.cast#4 etc... Yes the compiler see it differently than a zero. But under the hood, it is a zero. That's all. It is a fancy zero... – simre May 14 '22 at 20:37
  • @JaMiT Even in your link someone copied the header for that: https://stackoverflow.com/a/56013135/14914614 `nullptr` is `nullptr_t(0)`, internally it is a void*=0. Even operator T*() simply returns a 0. A zero. :) :) – simre May 14 '22 at 20:44

0 Answers0