From: Maxim Yegorushkin on 10 Jan 2010 02:36 On 07/01/10 09:32, Adam Badura wrote: > Why C++ does not contain a generic compare function? Such function > would return a negative value if left< right, zero if left == right > and a positive value if left> right. It could be either overloaded > for user types or like swap use some template magic. It can be as simple as: // Returns -1, 0 and 1 for t < u, t == u and t > u respectively // Overload for your own types as necessary. template<class T, class U> int compare(T t, U u) { return (t > u) - (t < u); } -- Max [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Adam Badura on 11 Jan 2010 02:24 > It can be as simple as: > > // Returns -1, 0 and 1 for t < u, t == u and t > u respectively > // Overload for your own types as necessary. > template<class T, class U> > int compare(T t, U u) > { > return (t > u) - (t < u); > } It is not about how short that function could be. And beside your function (as the default one) has following drawbacks: 1) It requires both operator < and operator >. Providing both is not hard if one can be provided however STD seems to entirely depend only on < and ==. 2) It will not work with overloaded versions of those operators which do not return bool but for example an int which is 0 for false and and undetermined positive number for true. 3) I think STD prefers to use std::less (and std::greater in this case as well) rather then row operators. But I am not sure here. Adam Badura -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Joe Gottman on 11 Jan 2010 02:32 Maxim Yegorushkin wrote: > On 07/01/10 09:32, Adam Badura wrote: >> Why C++ does not contain a generic compare function? Such function >> would return a negative value if left< right, zero if left == right >> and a positive value if left> right. It could be either overloaded >> for user types or like swap use some template magic. > > It can be as simple as: > > // Returns -1, 0 and 1 for t < u, t == u and t > u respectively > // Overload for your own types as necessary. > template<class T, class U> > int compare(T t, U u) > { > return (t > u) - (t < u); > } > The problem with this is that operator - is not defined for bool. Instead, why not just define this as return (t < u) ? -1 : ((u < t) ? 1 : 0); Joe Gottman -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Tony Delroy on 11 Jan 2010 05:07 On Jan 11, 4:36 am, Maxim Yegorushkin <maxim.yegorush...(a)gmail.com> wrote: > On 07/01/10 09:32, Adam Badura wrote: > > > Why C++ does not contain a generic compare function? Such function > > would return a negative value if left< right, zero if left == right > > and a positive value if left> right. It could be either overloaded > > for user types or like swap use some template magic. > > It can be as simple as: > > // Returns -1, 0 and 1 for t < u, t == u and t > u respectively > // Overload for your own types as necessary. > template<class T, class U> > int compare(T t, U u) > { > return (t > u) - (t < u); > > } That is simple as in concise, but not so simple in its use of the integral values associated with booleans. It's also throwing efficiency out the door, as it performs two comparisons even in situations when only the first is necessary. Finally, it requires operator> and operator< be defined for T and U, more of a burden than using < twice. The following address both issues (and in one less character ;-P)... return t < u ? -1 : u < t; As per the thread, that still risks two from-scratch comparisons, which might involve redundant work. Cheers, Tony -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Francis Glassborow on 11 Jan 2010 05:03
Adam Badura wrote: >> It can be as simple as: >> >> // Returns -1, 0 and 1 for t < u, t == u and t > u respectively >> // Overload for your own types as necessary. >> template<class T, class U> >> int compare(T t, U u) >> { >> return (t > u) - (t < u); >> } > > It is not about how short that function could be. > And beside your function (as the default one) has following > drawbacks: > 1) It requires both operator < and operator >. Providing both is not > hard if > one can be provided however STD seems to entirely depend only on < and > ==. Well replace the return statement with return (u < t) - (t < u); -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |