I was recently answering a question on the undefined behaviour of doing p < q
in C when p
and q
are pointers into different objects/arrays. That got me thinking: C++ has the same (undefined) behaviour of <
in this case, but also offers the standard library template std::less
which is guaranteed to return the same thing as <
when the pointers can be compared, and return some consistent ordering when they cannot.
Does C offer something with similar functionality which would allow safely comparing arbitrary pointers (to the same type)? I tried looking through the C11 standard and didn't find anything, but my experience in C is orders of magnitude smaller than in C++, so I could have easily missed something.