3 ms·
Special operator for one type? Please no.
by pleasecalllater 9y ago
Special operator for one type? Please no.
- idea1434 9y agoNo, it could be used for any type. It would be equivalent to (a<b)&&(a==b)&&(a>b), but would be most useful for floating point, where this operator would evaluate to false if a or b is NaN.
- pleasecalllater 9y agoNope, this operator will always evaluate to false. Show me any pair of numbers, which is not NaN, which will be at the same time, equal, smaller, and greater. Of course I can imagine such a set, and operations... (I have a little bit of a mathematical background) but for programming it will be quite useless. Why not `is_nan()` insted?
- daemin 9y agoNo, the operator is like the same operator in Ruby. You define an operator<=> for one or two types and then whenever you use one of the normal comparison operators in code (say a < b), the compiler replaces it with a<=>b < 0.
- pavlov 9y agoWell, this is C++, so the spaceship operator can be overloaded for all sorts of clever purposes. For example, when comparing strings, you could use it to coerce the arguments into spaceship objects, then run a little Space War simulation to lighten the user's day. Just another day of C++ operator overloading.