I want to store some pointers into std::set, but the standard guide says it's invalid.
If two pointers p and q of the same type point to different objects that are > not members of the same object or elements of the same array or to different > functions, or if only one of them is null, the results of pq, p<=q, and > p>=q are unspecified.
It seems the <, > operators are not supported by naive pointer type, as below.
Object * a = new Object;
Object * b = new Object;
a == b; // valid
a < b; //invalid
Object * arr = new Object[10];
&arr[3] == &arr[4]; // valid
&arr[3] < &arr[4]; // valid
How can I put pointers into std::set or key of std::map? Should I define some comparison-supporting wrapper
Edit 1
Some other questions say that even though <, > are not directly supported by C++ pointers, std::less works fine with pure pointers(without any extra information). But I can't find any reference for that.
While it is true that built-in operator
<
only works for pointers into the same array,std::set
andstd::map
don't use that operator - they usestd::less
. This, in turn, is guaranteed to impose a total order on all pointers: