编译器对以下代码的处理方式不同:
#include <compare>
struct A;
struct I {
virtual std::strong_ordering operator <=>(const A&) const {
return std::strong_ordering::equal;
}
};
struct A : I {
virtual std::strong_ordering operator <=>(const A&) const = default;
};
GCC 和 MSVC 都接受它,但 Clang 不接受它返回错误:
warning: explicitly defaulted three-way comparison operator is implicitly deleted [-Wdefaulted-function-deleted]
virtual std::strong_ordering operator <=>(const A&) const = default;
defaulted 'operator<=>' is implicitly deleted because there is no viable three-way comparison function for base class 'I'
error: deleted function 'operator<=>' cannot override a non-deleted function
virtual std::strong_ordering operator <=>(const A&) const = default;
演示:https ://gcc.godbolt.org/z/WGrGTe89z
似乎只有 Clang 在这里,因为I::operator <=>(const I&) const
没有定义,所以A::operator <=>(const A&) const
必须隐式删除,并且删除的方法不能覆盖未删除的方法I
。其他编译器是否也有权接受代码?