4 ms·
This is interesting... As non-C++ developer, I've never thought I needed this. Is this an answer to a question anyone is asking?
by Demiurge 7y ago
This is interesting... As non-C++ developer, I've never thought I needed this. Is this an answer to a question anyone is asking?
- wyldfire 7y agoYes, your favorite high level language might provide something like this implicitly.
- maxlybbert 7y agoPerl has had this for decades. C’s strcmp() has similar semantics. I thought the motivation was that sort can be more efficient when each comparison can return more information (the standard library already has a sort function that relies only on using less-than operations). But people eventually realized that certain operations have to be implemented together to make sense. For instance, the sort function that relies only on less-than operations defines two values as equal if neither value is less than the other. Many people consider this weird, and wonder why sort doesn’t require both less-than and equality operations. The spaceship operator essentially implements a group of operations, and guarantees they’ll be consistent: less-than, equal, and greater-than (plus not-equal, less-than-or-equal, and greater-than-or-equal). Personally, I’m not thrilled about a special purpose solution to a single example of a problem (use this one operator to implement this group of related operations), but I do see some value in making it easy to implement this group of related operations.
- ksherlock 7y agoYes. Aside from reducing repetitive code (replacing 7 overloaded comparison operators) it's probably more efficient. std::map, for example, currently uses a less-than comparator to order items, requiring multiple comparisons to check if items are equal.
- mehrdadn 7y agoThe part you probably needed is 3-way comparisons. The weak vs. strong vs. distinction is less likely to be needed IMO.
- de_watcher 7y agoC++ just has this under-the-hood access to be able to define any comparison operator to do arbitrary things. C++20 is now more aware that this things are used for comparison, and it helps to make definitions shorter. Still handles non-totally-ordered cases fine.
- iainmerrick 7y agoAs someone who has written comparison functions in C++ (probably incorrectly), yes, this would have been a godsend. Similarly, recent-ish innovations like std::move and std::unique_ptr may look complicated at first, but they genuinely made life a lot easier.