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.
84
questions
2
votes
0answers
44 views
Which equality/comparison operators are synthesized for heterogeneous spaceship operator definitions? [duplicate]
C++20 introduced the new spaceship operator <=> which allows for synthesizing equality and comparison operators based on the strength of the ordering of the three-way comparison.
However, it ...
6
votes
1answer
117 views
Nested generation of comparison operator with three-way operator?
Consider the following two overload operator<=> for S:
#include <compare>
struct S {};
int operator<=>(S, int) { return 0; } #1
S operator<=>(S, S) { return {}; } #2
If ...
7
votes
2answers
162 views
Three-way operator <=> return struct with implicit conversion function
Consider the following useless code:
struct S{
constexpr operator int() const { return 0; }
constexpr auto operator<=>(S) const { return *this; }
};
static_assert(S{} <= S{});
Clang and ...
42
votes
3answers
2k views
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:
//...
-1
votes
2answers
111 views
Why do empty structs in C++20 do not have implicit spaceship operator?
Motivation: sometimes I use std::variant to implement "fancy" enum where some enum states can carry state.
Now if I want to use the <=> for my variant it requires that my empty structs ...
5
votes
1answer
81 views
Why does spaceship allow mixed comparisons (different template instantiations) with nonsense results?
EDIT: This has nothing to do with spaceship. It is just that the use of spaceship obfuscated the real issue in my code (see answer for details).
I was surprised by the output of this program:
(If you ...
8
votes
1answer
69 views
Why does std::tuple call operator <=> twice?
The following code calls operator <=> twice, with arguments reversed. But why?
GCC 10.2 and clang 12 both seem to be using libstdc++-10, whose <tuple> does provide operator <=>, so ...
2
votes
1answer
63 views
MSVC's standard library does not define spaceship operator for std::string, std::shared_ptr, and so?
I'm trying to make my program multi-platform, originally written for Linux. MSVC (I'm using 19.28) is told to have spaceship operator support from version 19.20 (https://en.cppreference.com/w/cpp/...
3
votes
1answer
102 views
Ambiguous Overloaded Operator C++20
I'm trying to test my project in the latest Visual Studio and Clang versions. One of the errors that pops up is related to an ambiguous operator (with reversed parameter order). This does not seem to ...
1
vote
1answer
106 views
Inherited synthesized comparison operator produces warning/error: ISO C++20 considers use of overloaded operator '!='
In the following code snippet clang 11.0.1 generates a warning
template <class T>
struct iterator_facade
{
template<class S>
bool operator==(const S &other) const noexcept
{...
2
votes
1answer
68 views
Performance problems with the C++20 spaceship operator?
Since C++20 the standard library uses the spaceship operator to implement comparison for strings and vectors (according to this video). I am worried that this comes with a potentially huge performance ...
105
votes
1answer
4k views
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 <boost/operators.hpp>
struct F : boost::...
1
vote
0answers
50 views
Why doesn't the spaceship operator allow me to perform an equality operation but allow inequality operations? [duplicate]
Does anyone know why I’m getting a compiler error for the equality operation on a class that has a 3 way operator override? I'm using VS 2019.
class Rectangle
{
public:
constexpr Rectangle(const ...
0
votes
0answers
49 views
How to write custom spaceship operator in c++20 [duplicate]
I don't understand why this code doesn't compile. According to some websites, a == b should be rewritten as a.operator<=>(b) == 0, however both clang and gcc fail to compile.
#include <...
3
votes
1answer
109 views
Checking for three-way-comparison operator support at compile time
I'd like to conditionally enable operator <=> overloads in my code depending on whether or not it is supported given the current version of the compiler and its command line options. For example,...
2
votes
2answers
74 views
generate == operator that uses non-defaulted <=>
In a slight variation of this question. I would like to define a custom type with a custom <=> operator and use that custom <=> operator to generate a ==. Trying the following
#include <...
2
votes
1answer
102 views
Why is there an `equivalent` value for std::strong_ordering?
The valid values of std::strong_ordering are less, equal, equivalent, and greater. However, it appears that strong_ordering::equivalent and strong_ordering::equal are equal (i.e., interchangeable), ...
18
votes
3answers
851 views
Three-way comparison operator with inconsistent ordering deduction
Some time ago I defined my first three-way comparison operator. It compared a single type and replaced multiple conventional operators. Great feature. Then I tried to implement a similar operator for ...
18
votes
1answer
569 views
Three-way comparison and constexpr function template: which compiler is right?
Consider:
#include <compare>
template<class=void>
constexpr int f() { return 1; }
unsigned int x;
using T = decltype(x <=> f());
GCC and MSVC accept the declaration of T. Clang ...
6
votes
1answer
173 views
Rewritten comparison operators and expression templates
I have a finite volume library, strongly influenced by openfoam, which enables the solution to continuum mechanics problems to be written in C++ similarly as one
would in paper. For example, to solve ...
3
votes
3answers
102 views
C++20: Automatically generated operators are unreferencable in derived classes?
I noticed something rather strange while writing C++20 related to spaceship operators.
As I understand, since C++20, comparison operators are automatically generated by the compiler.
However, I have ...
1
vote
0answers
79 views
What will be the type after Three-way comparison C++20 [closed]
When using three-way comparison in C++20, like:
type1 foo = Value1;
type2 bar = Value2;
auto res = foo <=> bar ;
For what type of type1 and type2 the res's type will be std::weak_ordering ,...
20
votes
1answer
994 views
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 ...
5
votes
1answer
160 views
What does comparing the result of the three-way comparison operator with nullptr do?
Given the example from cppreference on <=>, we can simplify the example code to:
struct person {
std::string name;
std::string surname;
auto operator <=> (const person& p) ...
0
votes
0answers
72 views
C++ template class error with operator '<=>' (error C2678)
I'm attempting to make a treap class in C++. I have the .h and .cpp files for this class, as well as a test class. My 'add' method gives me the following error when I attempt to build it: "error C2678:...
1
vote
2answers
108 views
Trying to use spaceship operator in derived classes
I am trying to use spaceship operator in base class so I would have defined all operator by compilator. (source: https://devblogs.microsoft.com/cppblog/simplify-your-code-with-rocket-science-c20s-...
7
votes
1answer
187 views
Why do I need to include <compare> header to get <=> to compile?
I know the technical answer is: because the standard says so.
But I am confused regarding the motivation:
I see nothing "library" in the defaulting the <=>: it may return some type that is ...
3
votes
2answers
180 views
Should the operators == and <=> in C++ 20 be implemented as a member or a free function?
Note: I presume that this is technically duplicate of this question but:
changes to == in C++20 are quite radical, and I am not sure if
reviving 9 year question is the proper thing to do.
I ask ...
3
votes
1answer
114 views
Check if a type is a partial order
Can a type be checked for partial ordering property in C++20? If so, how can such a check be made with the use of concepts?
Is the the following code enough for this purpose?
#include <compare>...
1
vote
1answer
126 views
Why default three-way operator (spaceship <=>) generates equality operator (==) and user define three-way operator not?
Consider this code:
#include <iostream>
#include <compare>
class A {
public:
int i = {};
std::strong_ordering operator<=> (A const& r) const
{
return i <=> r.i;...
8
votes
1answer
481 views
Why does this clang code fail to compile with clang 10 with -std=c++20
Following program fails to compile with clang10 and -std=c++20
#include "clang/AST/ASTContext.h"
int main(){}
With -std=c++17 it works.
This is the compile attempt output(note that I am fine with ...
2
votes
2answers
182 views
How to force std::weak_ordering
While trying out the new Tie-Interceptor three-way comparison operator <=> I was wondering what would be an example such that
struct Foo {
/*
....
*/
auto operator<=>(...
0
votes
1answer
88 views
Why does global spaceship operator not behave as expected?
#include <compare>
#include <forward_list>
template<typename T>
struct A
{
std::forward_list<T> l;
};
template<typename T>
auto operator<=>(const A<T>&...
7
votes
1answer
130 views
Why can I invoke == with a defaulted <=> but not a user-provided one?
#include <compare>
struct A
{
int n;
auto operator <=>(const A&) const noexcept = default;
};
struct B
{
int n;
auto operator <=>(const B& rhs) const ...
10
votes
1answer
604 views
Why have comparison operators been removed from standard library containers in C++ 20?
I was browsing cppreference and saw that vector's comparison operations have been removed in C++20, and the spaceship operator (<=>) has been introduced. The same thing can be seen for many ...
1
vote
1answer
44 views
How to not execute some php 7 code on php 5.6?
recently i had the necessity of use come code for backward compatibility with PHP 5.6 and i did this using if statement, checking php version for choosing what code execute.
This is a little example:
...
2
votes
3answers
136 views
Three way comparison replaces all others comparison operators except ==?
In g++ 10, I am trying to use three way comparison, just for experiment.
I read that other operators do not needed anymore (except ==).
But even if I can use operator (it is implemented on the ...
3
votes
1answer
136 views
Three-way comparison of pointer to functions fails
Why can't I compare pointers to functions with GCC trunk?
using f_t = int(*)(int);
f_t a { nullptr }, b { nullptr };
auto c = a <=> b;
int main (){}
gives
a.cpp:4:13: error: invalid operands ...
9
votes
1answer
135 views
How do you reverse a strong_ordering?
Is there an easier way to achieve the effect of this function?
strong_ordering reverse(strong_ordering v) {
if (v > 0)
return strong_ordering::less;
else if (v < 0)
...
4
votes
2answers
264 views
Is there a wrapper for floating point numbers in C++20 that would enable me to default the spaceship operator?
I was watching "Using C++20 three way comparison - Jonathan Müller - Meeting C++ 2019" talk and it mentioned problems with classes that contain floating point members.
Problem comes from the fact ...
58
votes
3answers
3k views
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 <...
1
vote
1answer
160 views
How does spaceship gets the #id value for .cer files from Apple Developer Portal?
I have the .cer certificate that is downloaded from Apple Developer Portal of the client I don't have authentication for. I am running openssl x509 to read the .cer which doesn't shows any value that ...
6
votes
2answers
2k views
Real world usage example for spaceship operator [closed]
The definition of spaceship operator is meant to have a strong definition of ordering, but does this affect the way your client code is written or just how to define your class comparison operators?
...
3
votes
1answer
286 views
Three-way comparison operator member vs non-member implementation
Two-way comparison operators should be non-members functions if:
you want the first operand to be of a type that is not this class
you want implicit type conversion any of the two operands
The new C+...
0
votes
0answers
44 views
How to make an operator that is like Ruby <=> (a.k.a.: spaceship)
I have a sample code below that is made with swift and using <=>
,I want to convert it to android but I have no clue how to do.I want to convert the following sample
to android with java. Some ...
12
votes
4answers
226 views
Perl lexer: why does “<=>” eq “=” in the context of <=><=><=>?
I was just reading the secret pseudo-constants, namely the Space fleet constant
<=><=><=> Space fleet 0
This seems to be because the outer <=> is doing ...
0
votes
2answers
65 views
Sorting arrays in opposite order in Rby
I'm new to Ruby and need a piece of code to be explained to me. The array to be sorted is this one:
books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia",
"A Brief History of Time",...
8
votes
1answer
412 views
Practical meaning of strong_ordering and 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 strong_ordering and weak_ordering (same goes for ...
22
votes
1answer
8k views
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 ...
16
votes
1answer
971 views
Legitimate appearances of <=> in pre-c++20 code
Messing around in wandbox I discovered that clang will actually issue a warning if it sees <=> appear in C++17 or earlier.
warning: '<=>' is a single token in C++2a; add a space to avoid ...