Re: Is it allowed that std::map equal_range returns more than 1 elements?
On Apr 4, 3:13 pm, Bart van Ingen Schenau <b...@ingen.ddns.info>
wrote:
Alberto Ganesh Barbati wrote:
This operator does not induce a Strict Weak Ordering, ... 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?
Because, if it is true, then the harmlessly looking definition
std::set<double> s;
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
values.
I'm not a C++ standards guru, but I think you're missing
a distinction.
Your definition _potentially_ results in Undefined Behavior.
However, if you never insert a NaN (e.g., if you have code
that insures that a NaN is never inserted), there's no UB.
(Semi-OT "gotcha": I've heard of floating point
implementations that implement A < B as (A-B)< 0: if A-B
underflows to zero, you can have A!=B but neither A<B nor B<A,
which makes the non-NaN floats not strictly ordered.
Implementations that truly conform to IEEE-754 won't have this
problem, though.)
It's like writing "*p", where p is a pointer. Every time
you put the expression *p into your code, you have potential
UB, because pointers can be null, but if you write your code so that
it never evaluates *p if p is null, there is no actual UB.
With std::map, it seems obvious that as long as the set of
key values that you actually have in a map object is strictly ordered
by the comparison operator, you will not have a problem.
(I assume that std::map is not allowed to generate different
key values from the ones you give it.)
However, once you add an element with a key that causes the
set of key values in that object to no longer be ordered,
the behavior of the object is no longer defined.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]