64

<=>C++20の新しい宇宙船演算子で奇妙な動作が発生しています。私はVisual Studio 2019コンパイラを/std:c++latest.

期待どおり、このコードは正常にコンパイルされます。

#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;
}

ただし、Xを次のように変更すると、次のようになります。

struct X
{
    int Dummy = 0;
    auto operator<=>(const X& other) const
    {
        return Dummy <=> other.Dummy;
    }
};

次のコンパイラ エラーが発生します。

error C2676: binary '==': 'X' does not define this operator or a conversion to a type acceptable to the predefined operator

これをclangでも試しましたが、同様の動作が得られました。

operator==デフォルトの実装では正しく生成されるのに、カスタムの実装では生成されない理由について、説明をいただければ幸いです。

4

3 に答える 3