From: Daryle Walker (darylew_at_[hidden])
Date: 2006-07-18 18:27:17
On 7/11/06 8:50 AM, "Peter Dimov" <pdimov_at_[hidden]> wrote:
> Philippe Vaucher wrote:
>>> How do you make a std::set of structs, pairs, or tuples?
>> Maybe it was already said (didn't read the whole thread), but I
>> thought I'd add my 0.02$.
>> I think we should not define meaningless operator< for the
>> structs/whatever, and let the user define one if he wants to use
>> those structs in a set.
> That's the claim being made in this thread, true.
> The user can't necessarily define operator< for a foreign class because
> different users may have different ideas about what operator< should do, so
> their translation units won't be able to coexist. It is the responsibility
> of the author of the class to define its operator< (if there is one.)
> This leaves us with the option of defining and using custom comparators. So,
> if the author of pair<> does nothing, every time you need to make a set out
> of pair<X, Y>, you'll need to define a separate function object for it. This
> isn't very convenient; most of these function objects would end up being
> exact copies.
When you mention "separate" function objects that will mostly be "exact
copies," do you really mean objects, which are at the run-time level, or do
you actually mean class (templates) at the compile-time level? You wrote
the former, but I think you meant the latter.
If you really did mean the former, then you're too late. It already
happens. Each associative container object keeps a separate copy of its
comparison function object, even if all those comparison objects are
equivalent. Having separate comparison objects for each container object
allows comparison classes with state that gives different comparison objects
slightly different criteria (so two containers of the same type could sort
differently). If you meant the latter, then if you like someone else's
comparison object, then use his/her class instead of making a copy of it!
> So maybe the author of pair<> needs to provide a comparator.
> template<class P> struct pair_compare
> bool operator()( P const & p ) const
> // ???
> What would you put in the ??? portion? (Warning, this is a trap.)
Why is this a trap?
Anyway, why should the author provide a comparator class after not providing
an operator "<"? If the author couldn't decide on a standardized order,
then any provided comparator class would use a criteria determined by fiat.
You're back to providing your own class if you disagree with the author.
I know it's inconvenient after using sets/maps with types that let you skip
specifying the comparison parameter, but maybe there was a reason the author
didn't provide a standardized order.
-- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com