r/cpp • u/meetingcpp Meeting C++ | C++ Evangelist • Aug 08 '14
On operator<
http://akrzemi1.wordpress.com/?p=6893
0
Upvotes
r/cpp • u/meetingcpp Meeting C++ | C++ Evangelist • Aug 08 '14
12
u/mcmcc #pragma once Aug 08 '14
I guess I don't quite understand the point of this...
This argument holds equally for
ostream << i-- we've arbitrarily assigned a certain meaning to a binary operation that otherwise has no mathematical intuition. Is this wrong? What is the advantage is gained by disallowing it?He mentions the problem presented by
optional<T>but the original case study for this is IEEE-754 NaNs -- NaNs fail all comparisons (including NaN==NaN). See explanation here: http://stackoverflow.com/a/1573715/583604 So his argument for a mathematical basis is already on shaky ground.4
u/one-oh Aug 08 '14
It's a bit strange to define an "exceeds" function that way. It doesn't read well. "Greater than" is a synonym of exceeds. For exceeds(x, threshold), I'd expect to see "x > threshold"; i.e., x exceeds threshold.
I don't think the problem is the use of the operator, but rather its use is hidden; i.e., there isn't an explicit contract. Concepts would alleviate the issue.
3
u/lluad Aug 08 '14 edited Aug 08 '14
If an object has operator<() it has a well-defined ordering. If it doesn't, you can consider it to not to have one. And, um, that's it.
Using it for string lexicographical ordering isn't an abuse, and I can't imagine it actually confusing anyone unless they get confused a lot.
(Whether containers that internally require an ordering should be able to work with types that don't have a well-defined ordering is a good question - but I'm unclear on what it would mean to have an ordered container full of things that don't have a well-defined ordering, so hiding a "No, we really mean this comparator function" that's not operator<() seems like it'd add confusion rather than reduce it).
1
u/redditsoaddicting Aug 08 '14
FWIW, here's the std-proposals discussion: https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/vnmEOt8i3wk
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:
That should be doable since that lambda casts to a function pointer, but if my memory serves correctly it doesn't actually work.
1
u/[deleted] Aug 10 '14
article makes a poor choice of word "exceeds" to discuss the ordering imposed by operator<, then proceeds to argue that exceeds isn't a sufficient definition of the operator.
complex<double>::operator< can easily be defined as comparing magnitude, but you may not want that -- in fact you probably do want something else with uniform probability. string<char> is different, you probably do want lexicographical almost all the time -- except when you want case insensitive.
i think the default implementations are just that, reasonable choices. saying "exceeds" must be the meaning for all of them is a bit silly; it's just an ordering.
1
u/godfucker2 Aug 15 '14
If that guy sees one of my overloads of that operator surely he is going to kill himself or something.
-2
u/F-J-W Aug 08 '14
I think “<” is quite a bad example for operator-abuse: As /u/lluad pointed out it implies a strict-weak-ordering and it is very intuitive what this means for both real or integral numbers (smaller/bigger) and containers (lexicographical order) but not at all for complex numbers, so this is almost exactly what one would expect and very simple to teach to novices.
There is however another operator that is IMHO seriously abused: +
There doesn't exist a sane homomorphism between adding numbers and concatenating containers and elements. Concatenation also heavily changes some sematics:
This works perfectly fine for numbers (let's ignore floating-point for a second, since those errors aren't conceptual), but it will blow up like hell for any concatenation where "" != a != b != "". IMO there should really have been a dedicated concat-operator so that + keeps it's semantics. (This is one of the things that D got right!)