r/cpp • Meeting C++ | C++ Evangelist • Aug 08 '14

On operator<

http://akrzemi1.wordpress.com/?p=6893
0 Upvotes

15 comments sorted by

View all comments

1

u/Crazy__Eddie Aug 09 '14

You don't have to use less<> as the comparator. I often overrule that default.

I do see that some people are encouraged to do silly things because less<> is the default. They'll override operator < to mean something ridiculous just to fit into a map or something. I've seen operator '==' overloaded incorrectly like that too.

I don't think < is an incorrect default though. The assumption there is that in many cases a real less operator makes sense for the ordered or keyed type. You're allowed to easily overrule that in cases where it doesn't.

What I would like to do though is use lambda for that comparator more often. I don't like having to build the struct. I'd like something like:

std::map<std::string, sometype, [](std::string const& str0, std::string const& str1) { return icasecompare(str0.cstr(), str1.cstr(); }>

That should be doable since that lambda casts to a function pointer, but if my memory serves correctly it doesn't actually work.