c++ - C++20 中主要比较运算符 (==、<=>) 的反转

标签 c++ language-lawyer c++20

据我了解,C++20标准允许编译器自动调用 == 的反向比较运算符和<=>当代码中使用的方向运算符缺失但存在反向运算符时。就像this示例:

struct A {
    constexpr A() = default;
};

struct B
{
    constexpr bool operator==(A const& other) const {
        return true;
    }
};

static_assert(B{} == A{}); // This is defined
static_assert(A{} == B{}); // Fails in C++17, automatically generated in C++20

我查看了[class.compare]标准的一部分,但我找不到它的说明位置。

有人可以指出标准中的确切位置吗?

最佳答案

https://timsong-cpp.github.io/cppwp/n4868/over.match#oper-3.4.4

我认为它在:

表达式中重载解析/运算符

[over.match.oper]

For the equality operators, the rewritten candidates also include a synthesized candidate, with the order of the two parameters reversed, for each non-rewritten candidate for the expression y == x.

关于c++ - C++20 中主要比较运算符 (==、<=>) 的反转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76203831/

相关文章:

c++ - C++ 中函数的关键字?

C++ 多重访问说明符和初始化顺序

c++ - "Undefined Behavior"真的允许*任何事情*发生吗?

c++ - 在所有版本的 C 和 C++ 中,有符号、无符号、长整型和短整型都是有效类型吗?

c++ - 如何理解 "manifeSTLy constant-evaluated"的定义?

java - 将 Java 转换为 Symbian C/C++ - 字节和对象数组

c++ - 为 C++/Boost 库(netbeans 或 eclipse)设置环境

c++ - C++算法中 "functions"和 "function-like entities"有什么区别?

c++ - GCC 的 std::invocable 实现是不正确的还是不完整的?

c++ - 转发到聚合初始化器?