Pointers in C++ may in general only be compared for equality. By contrast, less-than comparison is only allowed for two pointers that point to subobjects of the same complete object (e.g. array elements).
So given T * p, * q
, it is illegal in general to evaluate p < q
.
The standard library contains functor class templates std::less<T>
etc. which wrap the built-in operator <
. However, the standard has this to say about pointer types (20.8.5/8):
For templates
greater
,less
,greater_equal
, andless_equal
, the specializations for any pointer type yield a total order, even if the built-in operators<
,>
,<=
,>=
do not.
How can this be realised? Is it even possible to implement this?
I took a look at GCC 4.7.2 and Clang 3.2, which don't contain any specialization for pointer types at all. They seem to depend on <
being valid unconditionally on all their supported platforms.