Re: Is it allowed that std::map equal_range returns more than 1 elements?
Bart van Ingen Schenau ha scritto:
Alberto Ganesh Barbati wrote:
This operator does not induce a Strict Weak Ordering, because the !(x
< y) && !(y < x) is not an equivalence relation. In particular the
(0,0) would be "equivalent" with every other key. As your operator<
does not satisfy this basic requirement, using it in a std::map
produces undefined behaviour.
Can you quote the standard on that for me?
I already did in another post. See also 25.3 for the precise definition
of strict weak ordering.
Because, if it is true, then the harmlessly looking definition
also results in UB. std::less<double> does not give an equivalence
relation if one of the two values is a NAN, or rather under the
equivalence rules of std::set<>, a NAN is equivalent with all other
I admit that I never thought about that. I'm not a floating point expert
but AFAIK the C++ standard does not specify floating point arithmetic in
presence of NaNs, it doesn't even mandate the presence of them. So we
could have a C++ implementation without NaNs or with signaling NaNs
only: in both cases std::less<> would induce a strict weak ordering.
However, I think you are correct: using a floating point type with quiet
NaNs in associative containers with std::less<> is a recipe for
undefined behaviour. Of course, no one will notice until a NaN is
actually inserted in the container... On the other hand, it's easy to
provide a different comparator that doesn't suffer this problem, for
template <typename T>
bool operator()(T x, T y) const
return isnan(y) ? true : x < y;
but I guess no one is going to use that...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]