From: Adam Badura on
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.

Such function would be useful in cases when objects are well
ordered and a full order info can be as easily obtained as partial
info. Consider an example of sorting string vectors. For each matching
elements we determine whether left element (string) is smaller then
right element (string). Then we must check separately for equality.
This results in the need to compare those string twice while a simple
"compare" function would give us full information at the cost of one
comparison.

Adam Badura

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Goran on
On Jan 7, 10:32 am, Adam Badura <abad...(a)o2.pl> 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.
>
> Such function would be useful in cases when objects are well
> ordered and a full order info can be as easily obtained as partial
> info. Consider an example of sorting string vectors. For each matching
> elements we determine whether left element (string) is smaller then
> right element (string). Then we must check separately for equality.
> This results in the need to compare those string twice while a simple
> "compare" function would give us full information at the cost of one
> comparison.

{ edits: quoted sig and banner removed. please remove excess quoted material
before posting. -mod }

I'd wager the answer is simple: your generic "compare" function would
simply be implemented in terms of op== and op< (or >), at which point,
you'd have one more (trivial) function name for small-ish number of
use cases.

For strings and raw memory, there are low-level (C) functions already
(seems to be your case). But they don't work well in face of +/-
complex types that majority of C++ code is supposed to have.

BTW, it looks like you're thinking in terms of qsort, which I'd
consider strange for idiomatic C++ code. With C++, if you want
ordering, just use set/multiset/map/multimap and/or populate your
vectors using lower/upper_bound. I can't see why do you need a
comparison function then.

Goran.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
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.
>
> Such function would be useful in cases when objects are well
> ordered and a full order info can be as easily obtained as partial
> info. Consider an example of sorting string vectors. For each matching
> elements we determine whether left element (string) is smaller then
> right element (string). Then we must check separately for equality.
> This results in the need to compare those string twice while a simple
> "compare" function would give us full information at the cost of one
> comparison.
>
> Adam Badura
>

I wonder if you have thought this through. How would you actually use
such a function? How would it be implemented more efficiently than we
can do for ourselves? How would you determine which ordering was to be
used (many types have more then one logical ordering, and often include
equivalence sets)? Even std::string has multiple orderings depending
on the collation rules. How would you deal with types that have no
ordering? What about types that have equivalence sets but no ordering?

It may seem a very simple thing to do but in fact it is very complicated
and far from trivial to provide generically.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Tony Delroy on
On Jan 7, 6:32 pm, Adam Badura <abad...(a)o2.pl> 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.
>
> Such function would be useful in cases when objects are well
> ordered and a full order info can be as easily obtained as partial
> info. Consider an example of sorting string vectors. For each matching
> elements we determine whether left element (string) is smaller then
> right element (string). Then we must check separately for equality.
> This results in the need to compare those string twice while a simple
> "compare" function would give us full information at the cost of one
> comparison.

Yes, it would be nice, and potentially efficient. Something like "x
<=> y" (might conflict with intended use in axioms in a later C++
Standard). Throw in x <=> y ?< a ?= b : c. Similarly, GNU had a nice
notation for "a ? a : b" - another annoying case to handle - but it
just didn't catch on and eventually was deprecated. Guess some things
are too tricky for their own good....

Cheers,
Tony



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: red floyd on
On Jan 7, 1:32 am, Adam Badura <abad...(a)o2.pl> 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.
>
> Such function would be useful in cases when objects are well
> ordered and a full order info can be as easily obtained as partial
> info. Consider an example of sorting string vectors. For each matching
> elements we determine whether left element (string) is smaller then
> right element (string). Then we must check separately for equality.
> This results in the need to compare those string twice while a simple
> "compare" function would give us full information at the cost of one
> comparison.

#include <functional>
template <typename T>
int compare<T>(const T& left, const T& right)
{
if (std::less(left, right))
return -1;
else if (std::less(right, left))
return 1;
else
return 0;
}


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]