3

I have implemented begin and end functions for MFC containers. The iterators are pointers to the internal data and may be null pointers for an empty container. What happens if both are null pointers and user compares them with < or <= ?

Other questions or their answers talking about comparisons of null-pointers with non-null pointers. This question is especially about comparing two null-pointers.

Furthermore it is of interest what the standard has to say about it.

2
  • 4
    @AlanBirtles The duplicate discusses unequal pointers. This question asks about equal pointers. AFAIK, that's well-defined. nullptr < nullptr returns false and nullptr <= nullptr returns true. Commented Apr 4 at 17:13
  • 1
    seems like your premise is that comparing other pointer values works out of the box, though once you use std::less for that as suggested in the dupe answer, comparing any pointer value is fine Commented Apr 4 at 17:57

1 Answer 1

4

Yes, this is well defined: a null pointer value “compare[s] equal” to itself ([expr.eq]/3.2), so < and > yield false and <= and >= yield true ([expr.rel]/5).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.