Re: Portability of comparing pointers to void
"Jiri Palecek" <jpalecek@web.de> schrieb im Newsbeitrag
news:fg2lrn$2sar$1@ns.felk.cvut.cz...
Matthias Hofmann wrote:
template <> struct strict_weak_less<int>
{
bool operator()( int a, int b )
{
// Define any two elements
// to be incomparable unless
// they are equal.
return a != b;
No. What you have written is not a strict weak ordering, because it lacks
antisymmetry (you say "a is less then b whenever they are not equal").
What
you have in mind would probably mean "return false" here.
With "return false", I end up with only a single element in the set: 2.
int main()
{
std::set<int, strict_weak_less<int> > intset;
intset.insert( 2 );
intset.insert( 4 );
return 0;
}
This creates a set where all elements are incomparable with each other.
(I
If you created such a set, it would only consist of one element (in this
example, 2, AFAIK).
I just learned that my program crashes with 'a != b'. It seems like a strict
weak ordering cannot be implemented, only a total ordering. Otherwise, how
do you create a set of elements that are both distinct and incomparable?
have searched for a real life example for a strict weak ordering that is
not
a total ordering, but I have not found any. Does anyone know an example?)
Sure. For example, ordering of strings or sets by their sizes. Note, that
if
you take your strict-weakly-ordered universe and partition it to classes
induced by the "incomparability" relation, you always get a linearly
ordered universe.
But ordering strings is a total ordering, isn't it? Because for strings, if
neither x < y nor y < x, the strings are equal. What about defining the
relation "<" as "is a descendant of" in a familiy tree? It satisfies the
condition that if x is incomparable with y and y is incomparable with z,
then x is incomparable with z. In other words, if x and y are siblings and y
and z are siblings, then x and z are siblings. It's also not a total
ordering because being siblings does not mean being equal.
2.) Why does the standard require a strict weak ordering for associative
containers, but guarantee a total ordering for std::less<T*> in
20.3.3/8? In
a strict weak ordering, the relation "neither a < b nor b < a" is
transitive, but does not mean equality. Incomparability only means
equality in a total ordering, but doesn't an associative container need
to
know whether two elements are equal in order to determine whether the
No, the containers treat such two elements as equal, without calling
operator==.
Yes, it uses operator< to find out wether two elements are equal. But that's
just my point: In a strict weak ordering, the relation "neither a < b nor b
< a" does not mean equality. Equality requires a total ordering, so how come
the standard requires only a strict weak ordering for associative
containers?
element already exists? Shouldn't the standard require a total ordering
for std::less for *any* type, not just for pointers?
It would be impossible to guarantee that for user types.
Well, giving guarantees for user types is generally difficult. It is up to
the user to design their types to comply with the standard requirements.
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]