非空原始指针和 nullptr 之间的运算符小于

Operator less than between a non-null raw pointer and nullptr

操作 nullptr < ptrptr < nullptr 是否为非空原始指针 ptr != nullptr 明确定义?欢迎引用 C++ 标准。

这样的比较是合式的,但其结果未指定。

[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 and q compare equal (5.10), p<=q and p>=q both yield true and p<q and p>q both yield false. Otherwise, if a pointer p compares greater than a pointer q, p>=q, p>q, q<=p, and q<p all yield true and p<=q, p<q, q>=p, and q>p all yield false. Otherwise, the result of each of the operators is unspecified.

空指针不属于[expr.rel]/3三个子句中的任何一个,因此它比较既不大于也不小于非- 空指针。那么这种情况就属于[expr.rel]/4.

的"otherwise"子句