No == operator found while comparing structs in C++

前端 未结 8 984
说谎
说谎 2020-12-02 09:21

Comparing two instances of the following struct, I receive an error:

struct MyStruct1 {
    MyStruct1(const MyStruct2 &_my_struct_2, const int _an_int =          


        
8条回答
  •  猫巷女王i
    2020-12-02 10:00

    Starting in C++20, it should be possible to add a full set of default comparison operators (==, <=, etc.) to a class by declaring a default three-way comparison operator ("spaceship" operator), like this:

    struct Point {
        int x;
        int y;
        auto operator<=>(const Point&) const = default;
    };
    

    With a compliant C++20 compiler, adding that line to MyStruct1 and MyStruct2 may be enough to allow equality comparisons, assuming the definition of MyStruct2 is compatible.

提交回复
热议问题