Search code examples
c++terminologycomparison-operatorsc++20spaceship-operator

What does "compares less than 0" mean?


Context

While I was reading Consistent comparison, I have noticed a peculiar usage of the verb to compare:

There’s a new three-way comparison operator, <=>. The expression a <=> b returns an object that compares <0 if a < b, compares >0 if a > b, and compares ==0 if a and b are equal/equivalent.

Another example found on the internet (emphasis mine):

It returns a value that compares less than zero on failure. Otherwise, the returned value can be used as the first argument on a later call to get.

One last example, found in a on GitHub (emphasis mine):

// Perform a circular 16 bit compare.
// If the distance between the two numbers is larger than 32767,
// and the numbers are larger than 32768, subtract 65536
// Thus, 65535 compares less than 0, but greater than 65534
// This handles the 65535->0 wrap around case correctly

Of course, for experienced programmers the meaning is clear. But the way the verb to compare is used in these examples is not standard in any standardized forms of English.

Questions*

  • How does the programming jargon sentence "The object compares less than zero" translate into plain English?
  • Does it mean that if the object is compared with0 the result will be "less than zero"?
  • Why would be wrong to say "object is less than zero" instead of "object compares less than zero"?

* I asked for help on English Language Learners and English Language & Usage.


Solution

  • What I am interested in, more exactly, is an equivalent expression of "compares <0". Does "compares <0" mean "evaluates to a negative number"?

    First, we need to understand the difference between what you quoted and actual wording for the standard. What you quoted was just an explanation for what would actually get put into the standard.

    The standard wording in P0515 for the language feature operator<=> is that it returns one of 5 possible types. Those types are defined by the library wording in P0768.

    Those types are not integers. Or even enumerations. They are class types. Which means they have exactly and only the operations that the library defines for them. And the library wording is very specific about them:

    The comparison category types’ relational and equality friend functions are specified with an anonymous parameter of unspecified type. This type shall be selected by the implementation such that these parameters can accept literal 0 as a corresponding argument. [Example: nullptr_t satisfies this requirement. — end example] In this context, the behaviour of a program that supplies an argument other than a literal 0 is undefined.

    Therefore, Herb's text is translated directly into standard wording: it compares less than 0. No more, no less. Not "is a negative number"; it's a value type where the only thing you can do with it is comparing it to zero.

    It's important to note how Herb's descriptive text "compares less than 0" translates to the actual standard text. The standard text in P0515 makes it clear that the result of 1 <=> 2 is strong_order::less. And the standard text in P0768 tells us that strong_order::less < 0 is true.

    But it also tells us that all other comparisons are the functional equivalent of the descriptive phrase "compares less than 0".

    For example, if -1 "compares less than 0", then that would also imply that it does not compare equal to zero. And that it does not compare greater than 0. It also implies that 0 does not compare less than -1. And so on.

    P0768 tells us that the relationship between strong_order::less and the literal 0 fits all of the implications of the words "compares less than 0".