non-defaulted operator <=> doesn't generate == and != in C++20

前端 未结 3 412
野性不改
野性不改 2020-12-30 18:25

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

3条回答
  •  盖世英雄少女心
    2020-12-30 19:00

    The other answers explain really well why the language is like this. I just wanted to add that in case it's not obvious, it is of course possible to have a user-provided operator<=> with a defaulted operator==. You just need to explicitly write the defaulted operator==:

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

提交回复
热议问题