4 ms·
God this is so confusing. Why the $%&* does it take a 17-page 27-minute blog post to explain how to implement comparison operators? Can someone please write th
by forrestthewoods 7y ago
God this is so confusing. Why the $%&* does it take a 17-page 27-minute blog post to explain how to implement comparison operators?
Can someone please write the 3-minute blog post that is easy to understand and follow?
If the answer is "it's too complicated to explain in 3 minutes" then I think that is a telling sign.
- gumby 7y agoHere you go: less than 3 minutes to type out! "The TL;DR is that there's a new operator, <=>, that returns less than 0, 0, or greater than zero, just like, say, strcmp does, but for all sorts of C++ objects. You can define operator<=> and operator== and your compiler will take care of the various other comparators. Doing this speeds up some library functions, doesn't force you to write as much code, and makes comparisons more intuitive. "There are also a bunch of corner case optimizations but if you don't care about them you don't need to know" This was written by someone who cares about the corner cases and underlying theory. It's like FP: IEEE743 is full of weirdo cases that some really smart people spent a lot of time worrying about. All most users care about is that "they have a fractional representation and that == doesn't reliably work except against 0.0." Just because there's hundreds of pages in that spec doesn't make it dumb either.
- furyofantares 7y agoI don't think this is right. In order to implement <=> for a class, you have to understand strong_ordering, weak_ordering, and partial_ordering in order to pick one, right?
- slavik81 7y agoGenerally, no. Most of the time you're going to write these functions as defaulted with an auto return type: auto operator<=>(const T&) const = default; bool operator==(const T&) const = default; The compiler will implement the function by comparing all the class member variables and will pick the type of the ordering as appropriate. You only need to look into further details about ordering types if you want to do something fancy.
- deleted 7y ago[deleted]