Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it defined behaviour to compare two null pointers for order

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.

like image 370
Martin Fehrs Avatar asked Dec 05 '25 15:12

Martin Fehrs


1 Answers

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).

like image 135
Davis Herring Avatar answered Dec 08 '25 05:12

Davis Herring



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!