Boost logo

Boost :

Subject: [boost] [container][set, map, flat_set, flat_map, +multi_xxx] operator< and operator== seems to ignore custom KeyCompare
From: Thorsten Ottosen (thorsten.ottosen_at_[hidden])
Date: 2013-07-17 10:58:10


Hi Ion & others,

I think that there might be a problem in the implementation of operator<
for these containers.

There might be a problem with operator== too.

The problem is that they all use some variant of the following definition:

template <class Key, class Value, class KeyOfValue,
           class KeyCompare, class A>
inline bool
operator<(const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& x,
           const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& y)
{
   return std::lexicographical_compare(x.begin(), x.end(),
                                       y.begin(), y.end());
}

There is no KeyCompare (or other compare object) passsed to
lexicographical compare.

Reading up on the standard, it seems that all standard containers are
implemented this way.

Consequence: when having e.g. set<T*>, the comparison is not portable,
since only std::less<T*> is guaranteed to be. So isn't this a defect?

The other issue is perhaps more sensitive. What should users expect from

   typedef set<std::string,CaseInsensitiveLess> Set;
   Set s1{ "foo", "BAR" };
   Set s2{ "FOO", "bar" };
   assert( s1 < s2 ); // true?
   assert( s1 == s2 ); // true?

I was at least surprised by this behavior.

regards

Thorsten


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk