Boost logo

Boost :

Subject: Re: [boost] [container][set, map, flat_set, flat_map, +multi_xxx] operator< and operator== seems to ignore custom KeyCompare
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2013-07-17 17:22:01


El 17/07/2013 16:58, Thorsten Ottosen escribió:
> 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.

It's a bit surprising but that's also how vector::operator< is
implemented (vector has no Compare function object so std::less<T> is
used). The operator is just there to have a default order to be inserted
in parent associative containers.

The standard is quite clear:

(taken from N3680) Table 98 — Optional container operations

-> Expression: a < b

-> Return type: convertible to bool

-> Operational semantics lexicographical_compare
(a.begin(), a.end(), b.begin(), b.end())

-> Assertion/note: pre: < is defined for values of T. < is a total
ordering relationship.

-> Complexity: linear

I guess using Compare for associative containers' operator < could
perfectly work it won't be the standard behaviour.

Best,

Ion


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