Questions tagged [spaceship-operator]

The <=> comparison operator is often referred to as the spaceship operator. It performs a 3-way comparison (returning less, equal, or greater) on the two operands.

The spaceship operator (so named because of its appearance) is used to compare items for sorting in various languages (such as Perl, Ruby, and Groovy). The standard definition is that:

  • a <=> b is less than zero if a < b
  • a <=> b is zero if a == b
  • a <=> b is greater than zero if a > b

The spaceship operator can also be used to provide default implementations of the <, >, and even == operators if the language (or libraries) is clever enough.

119 questions
301
votes
6 answers

What is the Ruby <=> (spaceship) operator?

What is the Ruby <=> (spaceship) operator? Is the operator implemented by any other languages?
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
295
votes
6 answers

What is the <=> ("spaceship", three-way comparison) operator in C++?

While I was trying to learn about C++ operators, I stumbled upon the following table that listed a strange comparison operator. What does this <=> operator do? Since 2017 cppreference.com updated that page and now contains detailed information…
q-l-p
  • 4,304
  • 3
  • 16
  • 36
269
votes
3 answers

What is <=> (the 'Spaceship' Operator) in PHP 7?

PHP 7 introduced the Spaceship (<=>) operator. What is it and how does it work?
Deepak Mankotia
  • 4,404
  • 6
  • 30
  • 55
192
votes
10 answers

What is this operator <=> in MySQL?

I'm working on code written by a previous developer and in a query it says, WHERE p.name <=> NULL What does <=> mean in this query? Is it something equal to =? Or is it a syntax error? But it is not showing any errors or exceptions. I already know…
zzlalani
  • 22,960
  • 16
  • 44
  • 73
115
votes
1 answer

C++20 behaviour breaking existing code with equality operator?

I ran into this while debugging this question. I trimmed it down all the way to just using Boost Operators: Compiler Explorer C++17 C++20 #include struct F : boost::totally_ordered1> { …
sehe
  • 374,641
  • 47
  • 450
  • 633
77
votes
3 answers

non-defaulted operator <=> doesn't generate == and != in C++20

I'm running into a strange behavior with the new spaceship operator <=> in C++20. I'm using Visual Studio 2019 compiler with /std:c++latest. This code compiles fine, as expected: #include struct X { int Dummy = 0; auto…
Zeenobit
  • 4,954
  • 3
  • 34
  • 46
62
votes
3 answers

How is the three-way comparison operator different from subtraction?

There's a new comparison operator <=> in C++20. However I think in most cases a simple subtraction works well: int my_strcmp(const char *a, const char *b) { while (*a == *b && *a != 0 && *b != 0) { a++, b++; } // Version 1 …
iBug
  • 35,554
  • 7
  • 89
  • 134
57
votes
5 answers

Why must I provide 'operator ==' when 'operator <=>' is enough?

#include struct A { int n; auto operator<=>(A const& other) const { if (n < other.n) { return std::strong_ordering::less; } else if (n > other.n) { return…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
45
votes
3 answers

More silent behaviour changes with C++20 three-way comparison

To my surprise, I ran into another snag like C++20 behaviour breaking existing code with equality operator?. Consider a simple case-insensitive key type, to be used with, e.g., std::set or std::map: // Represents case insensitive keys struct CiKey :…
sehe
  • 374,641
  • 47
  • 450
  • 633
32
votes
3 answers

Combined Comparison / "Spaceship" Operator (<=>) in Javascript?

Ruby has something called a Combined Comparison or "Spaceship" Operator, it looks like this: <=> It does the following: a <=> b := if a < b then return -1 if a = b then return 0 if a > b then return 1 Credit Is there a similar…
29
votes
3 answers

Is the three-way comparison operator always efficient?

Herb Sutter, in his proposal for the "spaceship" operator (section 2.2.2, bottom of page 12), says: Basing everything on <=> and its return type: This model has major advantages, some unique to this proposal compared to previous proposals for C++…
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
25
votes
1 answer

Why do we need the spaceship <=> operator in C++?

Why do we need such an operator in C++ and how is it useful in modern C++ programming? Any real world code examples where this can be applied will help. This question is geared to understand the practical application in real world without reading…
Ram
  • 3,045
  • 3
  • 27
  • 42
23
votes
2 answers

Practical meaning of std::strong_ordering and std::weak_ordering

I've been reading a bit about C++20's consistent comparison (i.e. operator<=>) but couldn't understand what's the practical difference between std::strong_ordering and std::weak_ordering (same goes for the _equality version for this manner). Other…
Mr. Anderson
  • 1,609
  • 1
  • 13
  • 24
22
votes
1 answer

Three-way comparison and constexpr function template: which compiler is right?

Consider: #include template constexpr int f() { return 1; } unsigned int x; using T = decltype(x <=> f()); GCC and MSVC accept the declaration of T. Clang rejects it, with the following error message: :7:26: error:…
cpplearner
  • 13,776
  • 2
  • 47
  • 72
21
votes
1 answer

Is there a std::less/std::greater for the spaceship operator?

All the basic comparisons (<, <=, ==, !=, >=, >) have an associated function object (std::less, std::less_equal, std::equal_to, std::not_equal_to, std::greater_equal, std::greater). Does the spaceship operator <=> have a similar function object? If…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
1
2 3 4 5 6 7 8