Are the operations nullptr < ptr
and ptr < nullptr
well defined for a non-null raw pointer ptr != nullptr
? Quotes from the C++ standard are welcome.
Such a comparison is well-formed, but its result is unspecified.
[expr.rel]/3 Comparing pointers to objects is defined as follows:
— If two pointers point to different elements of the same array, or to subobjects thereof, the pointer to the element with the higher subscript compares greater.
— If one pointer points to an element of an array, or to a subobject thereof, and another pointer points one past the last element of the array, the latter pointer compares greater.
— If two pointers point to different non-static data members of the same object, or to subobjects of such members, recursively, the pointer to the later declared member compares greater provided the two members have the same access control (Clause 11) and provided their class is not a union.
[expr.rel]/4 If two operands
p
andq
compare equal (5.10),p<=q
andp>=q
both yieldtrue
andp<q
andp>q
both yieldfalse
. Otherwise, if a pointerp
compares greater than a pointerq
,p>=q
,p>q
,q<=p
, andq<p
all yieldtrue
andp<=q
,p<q
,q>=p
, andq>p
all yieldfalse
. Otherwise, the result of each of the operators is unspecified.
A null pointer doesn't fall into any of the three clauses of [expr.rel]/3, and so it compares neither greater nor less than a non-null pointer. This case then falls into the "otherwise" clause of [expr.rel]/4.