spaceship-operator

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

孤街浪徒 提交于 2021-01-20 14:13:48
问题 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::totally_ordered1<F, boost::totally_ordered2<F, int>> { /*implicit*/ F(int t_) : t(t_) {} bool operator==(F const& o) const { return t == o.t; } bool operator< (F const& o) const { return t < o.t; } private: int t; }; int main() { #pragma GCC diagnostic ignored "-Wunused" F { 42 } == F{ 42 }; // OKAY 42 == F

Why have comparison operators been removed from standard library containers in C++ 20?

Deadly 提交于 2020-12-02 06:58:48
问题 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 other standard library containers like set and map. How do I do the comparisons in the new standard? Also, will C++20 start giving errors on older code? 回答1: If you continue to browse on the reference site a little, you might come to the section on default comparisons, which simply states that: In brief

Why have comparison operators been removed from standard library containers in C++ 20?

拜拜、爱过 提交于 2020-12-02 06:58:11
问题 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 other standard library containers like set and map. How do I do the comparisons in the new standard? Also, will C++20 start giving errors on older code? 回答1: If you continue to browse on the reference site a little, you might come to the section on default comparisons, which simply states that: In brief

Why can I invoke == with a defaulted <=> but not a user-provided one?

风格不统一 提交于 2020-08-19 04:27:10
问题 #include <compare> struct A { int n; auto operator <=>(const A&) const noexcept = default; }; struct B { int n; auto operator <=>(const B& rhs) const noexcept { return n <=> rhs.n; } }; int main() { A{} == A{}; // ok B{} == B{}; // error: invalid operands to binary expression } compiled with clang-10 as clang -std=c++20 -stdlib=libc++ main.cpp Why does A{} == A{} work but not B{} == B{} ? 回答1: In the original design of the spaceship operator, == is allowed to call <=> , but this is later

Why can I invoke == with a defaulted <=> but not a user-provided one?

☆樱花仙子☆ 提交于 2020-08-19 04:25:08
问题 #include <compare> struct A { int n; auto operator <=>(const A&) const noexcept = default; }; struct B { int n; auto operator <=>(const B& rhs) const noexcept { return n <=> rhs.n; } }; int main() { A{} == A{}; // ok B{} == B{}; // error: invalid operands to binary expression } compiled with clang-10 as clang -std=c++20 -stdlib=libc++ main.cpp Why does A{} == A{} work but not B{} == B{} ? 回答1: In the original design of the spaceship operator, == is allowed to call <=> , but this is later

Is there a wrapper for floating point numbers in C++20 that would enable me to default the spaceship operator?

筅森魡賤 提交于 2020-07-08 14:01:31
问题 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 that IEEE 754 comparisons involving NaN(s) are weird and do not provide total ordering. Talk gives a way to work around this problems, for example using strong_order or manually ignoring NaN values when implementing <=> (assuming that values are never NaN). My questions is if there are some library

Practical meaning of strong_ordering and weak_ordering

本小妞迷上赌 提交于 2020-06-12 02:37:25
问题 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 the _equality version for this manner). Other than being very descriptive about the substitutability of the type, does it actually affect the generated code? Does it add any constraints for how one could use the type? Would love to see a real-life example that demonstrates this. 回答1: Does it add any

Equality operator does not get defined for a custom spaceship operator implementation in C++20

一个人想着一个人 提交于 2020-04-29 07:51: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 <compare> struct X { int Dummy = 0; auto operator<=>(const X&) const = default; // Default implementation }; int main() { X a, b; a == b; // OK! return 0; } However, if I change X to this: struct X { int Dummy = 0; auto operator<=>(const X& other) const { return Dummy <=> other.Dummy; } }; I get the

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

こ雲淡風輕ζ 提交于 2020-01-22 04:56:33
问题 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 return *a - *b; // Version 2 return *a <=> *b; // Version 3 return ((*a > *b) - (*a < *b)); } They have the same effect. I can't really understand the difference. 回答1: The operator solves the problem with numeric overflow that you get with subtraction: if you subtract a

When is the spaceship operator used outside a sort?

眉间皱痕 提交于 2020-01-12 14:34:14
问题 This is a best practice question. I've only seen the Perl spaceship operator (<=>) used in numeric sort routines. But it seems useful in other situations. I just can't think of a practical use. Can anyone give me an example of when it could be used outside of a Perl sort? 回答1: I'm writing a control system for robot Joe that wants to go to robot Mary and recharge her. They move along the integer points on the line. Joe starts at $j and can walk 1 meter in any direction per time unit. Mary