Re: std::unique specification vs. reality
"Jordan DeLong" <fracture@allusion.net> skrev i meddelandet
news:20060603185844.GA15095@allusion.net...
On Wed, 31 May 2006 17:00:33 -0400, Bo Persson wrote: [...]
However, both the MSVC and G++ STL's do not do this. They compare
each
element against the first element of the non-unique sub-sequence,
not against the element preceding it.
As they all compare equal, we can't really tell which is which, can
we?
Sure we can; just take their addresses or something.
But that is not involved in the predicate for std::unique, is it?
If they all compare equal, there is no difference in comparing any two
objects.
There's also situations with user defined classes that have extra
state
beyond whatever defines "equality":
struct foo {
int i;
int some_other_state;
bool operator==(foo const& o) const
{ return i == o.i; }
};
Sure, but that is not involved in std::unique. It always keeps the
first element.
[...]
For example (untested):
template<class T>
bool
within_threshold(T a, T b, T t)
{
return std::abs(a - b) < t;
}
I don't think this is a valid predicate for std::unique.
It turns out this not a settled matter. Someone emailed me this url:
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#202
Whether that predicate is allowed isn't really the only question
here
though.
As I understand it, an "equivalence relation" implies that if
pred(a,b)==true and pred(b,c)==true, then pred(a,c) must also be
true.
In your case, this doesn't hold for 0.92, 1.0, and 1.05.
"Equivalence relation" does imply that, but the standard doesn't say
that the supplied BinaryPredicate has to be an "equivalence
relation".
This is obviously not settled yet. However, in the latest draft of the
standard (2006-04) a new paragraph is present, 25.2.8/2:
"Requires: The comparison function skall be an equivalence relation."
From this, I believe that the requirement is missing from the current
standard, but the intention was to have it in there. If so, my
interpretation seems valid.
Bo Persson
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]