问题
std::less<T *>
is guaranteed to provide total order, regardless of whether both pointers point into the same array.
In the latest draft of the standard, is the same true for the transparent function object std::less<void>
(std::less<>
) when you call its operator()
?
Obviously, the same question applies to std::greater
, but I assume they are specified the same.
回答1:
The current draft from github does not contain any language to that effect; in fact, its definition of less<>
says explicitly "returns std::forward<T>(t) < std::forward<U>(u)
", which would be undefined behaviour for incomparable pointers. So... don't do it, I suppose.
If you need a heterogeneous pointer comparator, it's probably best to write your own template predicate which uses std::less<T*>()
at the appropriate moment.
回答2:
std::less<void>
is undefined behavior. std::less
requires either a pointer type (for which there is a special rule), or a type for which <
is defined.
来源:https://stackoverflow.com/questions/21079958/stdlessvoid-and-pointer-types