TITLE: pointer comparisons (Newsgroups: comp.std.c++, 24 Feb 97) ???: >> Consider: >> >> bool >> compare( void (*p1)() , void (*p2)() ) >> { >> return p1 < p2 ; >> } BAU: christian.bau@isltd.insignia.com (Christian Bau) >May I add another question to this: Why does comparing unrelated pointers >result in undefined behavior? (I know there is no reason why in this >example p1 should be less than p2, but it would not be unreasonable that >for any two pointers that are not equal, either p1 < p2 or p1 > p2 should >always be true). CLAMAGE: Stephen.Clamage@eng.sun.com (Steve Clamage) You can compare two pointers of compatible type for equality (or inequality). You can also compare two pointers into the same object or array (or one item off the end of an array) for greater-than/less-than. If you compare pointers for greater-than/less-than but they do not point into the same object or array, the results are unspecified (not undefined). The assumption that one pointer must be either greater than, less than, or equal to the other is not valid. Consider also systems that use handles for pointers. The actual objects can move around during the course of program execution. The ordering of the handles tells you nothing about the ordering of the objects, and indeed the ordering of the objects could change.