Operator less than between non-null raw pointer and nullptr

Are the operations nullptr < ptr

and ptr < nullptr

correct for a non-empty raw pointer ptr != nullptr

? Quotes from the C ++ standard are welcome.

+3


source to share


1 answer


This comparison is well-formed, but the result is unspecified.

[expr.rel] / 3 Comparison of pointers to objects is defined as follows:

- If two pointers point to different elements of the same array or to its subobjects, the pointer to the element with a higher index is compared more.

- If one pointer points to an element of the array or its subobject, and the other pointer points to one of the last elements of the array, the last pointer compares the larger one.

- If two pointers point to different non-static data members of the same object or to subobjects of such elements, recursively, the pointer to the later declared element is compared more if the two elements have the same access control (clause 11) and provided, that their class is not a union.

[expr.rel] / 4 If two operands p

and q

compare are equal (5.10), p<=q

and p>=q

, both give true

and p<q

and p>q

both give false

. Otherwise, if the pointer p

is compared to more than a pointer q

, p>=q

, p>q

, q<=p

and q<p

, all give true

and p<=q

, p<q

, q>=p

, and q>p

all give false

. Otherwise, the result of each of the operators is not specified.



A null pointer does not fall into any of the three [expr.rel] / 3 clauses , and therefore it does not compare any more or less than a non-null pointer. This case then falls into the "otherwise" clause [expr.rel] / 4 .

+4


source







All Articles