据我了解,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/