The Context
While I was reading* Consistent comparison, I noticed an odd 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 somewhere 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.
Last example, found in a source code 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 of these expressions is clear. But the usage of the verb to compare does not conform to the syntactic rules of English grammar. This is jargon.
The questions**
- What does it mean to say that an object compares less than zero? Does it mean that if the object is compared with
0
using the<
operator the result will yield true? Are there other implications? If an object compares<0
, does this imply(object == 0)
will yield false and(object > 0)
will yield false? - What is the rationale for using the verb to compare instead of to be? What is the difference, for example, between "object compares <0" and "object is <0"?
- More generally what is the jargon to plain English translation of expressions of the type:
LHS
operand
comparescomparison operator
RHS
operand
* English is a foreign language to me.
** I have asked similar questions on English Language Learners and on English Language & Usage.
Yes, an "object compares less than 0" means that
object < 0
will yieldtrue
. Likewise,compares equal to 0
meansobject == 0
will yield true, andcompares greater than 0
meansobject > 0
will yield true.As to why he doesn't use the phrase "is less than 0", I'd guess it's to emphasize that this is all that's guaranteed. For example, this could be essentially any arbitrary type, including one that doesn't really represent an actual value, but instead only supports comparison with 0.
Just, for example, let's consider a type something like this:
[For the moment, let's assume the
friend template<...>
stuff is all legit--I think you get the basic idea, anyway).This doesn't really represent a value at all. It just represents the result of "if compared to 0, should the result be less than, equal to, or greater than". As such, it's not that it is less than 0, only that it produces
true
orfalse
when compared to 0 (but produces the same results when compared to another value).As to whether
<0
being true means that>0
and==0
must be false (and vice versa): there is no such restriction on the return type for the operator itself. The language doesn't even include a way to specify or enforce such a requirement. There's nothing in the spec to prevent them from all returningtrue
. Returningtrue
for all the comparisons is possible and seems to be allowed, but it's probably pretty far-fetched.Returning
false
for all of them is entirely reasonable though--just, for example, any and all comparisons with floating point NaNs should normally returnfalse
. NaN means "Not a Number", and something that's not a number isn't less than, equal to or greater than a number. The two are incomparable, so in every case, the answer is (quite rightly) false."
a
compares less than zero" means thata < 0
is true."
a
compares== 0
means thata == 0
is true.The other expressions I'm sure make sense now right?
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:
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
isstrong_order::less
. And the standard text in P0768 tells us thatstrong_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 literal0
fits all of the implications of the words "compares less than 0".It means that the expression will return an object that can be compared to <0 or >0 or ==0.
If a and b are integers, then the expression evaluates to a negative value (probably -1) if a is less than b.
The expression evaluates to 0 if a==b
And the expression will evaluates to a positive value (probably 1) if a is greater than b.
I think the other answers so far have answered mostly what the result of the operation is, and that should be clear by now. @VTT's answer explains it best, IMO.
However, so far none have answered the English language behind it. "The object compares less than zero." is simply not standard English, at best it is jargon or slang. Which makes it all the more confusing for non-native speakers.
An equivalent would be:
A comparison of the object using <0 (less than zero) always returns true.
That's quite lengthy, so I can understand why a "shortcut" was created:
The object compares less than zero.
"compares <0" in plain English is "compares less than zero".
This is a common shorthand, I believe.
So to apply this onto the entire sentence gives:
Which is quite a mouthful. I can see why the authors would choose to use symbols.