Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Synthesis of == from operator<=>
Forum Updated to NodeBB v4.3 + New Features

Synthesis of == from operator<=>

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 643 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    Consider this class:

    class X
    {
    public:
        int m_X = 0;
        int m_Y = 0;
        
        std::strong_ordering operator<=>(const X& other) const = default;
    };
    

    I can now write

    X a;
    X b;
    auto equal = (a == b);
    

    Perfect.
    However, when I have to implement operator<=> myself, this no longer works:

    class X
    {
    public:
        int m_X = 0;
        int m_Y = 0;
        
        std::strong_ordering operator<=>(const X& other) const
        {
            return std::tie(m_X, m_Y) <=> std::tie(other.m_X, other.m_Y);
        }
    }; 
    

    I get compiler errors such as
    error: C2676: binary '==': 'X' does not define this operator or a conversion to a type acceptable to the predefined operator
    (MSVC 2022), or
    error: no match for 'operator==' (operand types are 'X' and 'X')
    (MinGW 11.2)

    I have been digging around cppreference a bit, and also took a look at P0515 but couldn't come up with a clear idea why one works and the other does not.
    Can someone please explain?

    JoeCFDJ 1 Reply Last reply
    0
    • A Asperamanca

      Consider this class:

      class X
      {
      public:
          int m_X = 0;
          int m_Y = 0;
          
          std::strong_ordering operator<=>(const X& other) const = default;
      };
      

      I can now write

      X a;
      X b;
      auto equal = (a == b);
      

      Perfect.
      However, when I have to implement operator<=> myself, this no longer works:

      class X
      {
      public:
          int m_X = 0;
          int m_Y = 0;
          
          std::strong_ordering operator<=>(const X& other) const
          {
              return std::tie(m_X, m_Y) <=> std::tie(other.m_X, other.m_Y);
          }
      }; 
      

      I get compiler errors such as
      error: C2676: binary '==': 'X' does not define this operator or a conversion to a type acceptable to the predefined operator
      (MSVC 2022), or
      error: no match for 'operator==' (operand types are 'X' and 'X')
      (MinGW 11.2)

      I have been digging around cppreference a bit, and also took a look at P0515 but couldn't come up with a clear idea why one works and the other does not.
      Can someone please explain?

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      @Asperamanca from here https://cplusplus.com/doc/tutorial/operators/
      <=> is available with C++20

      Are you building it on VS or MinGW?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        It's by design. The TLDR: if you declare a custom spaceship operator, you have to declare the equality operator. If that operator should use the default implementation then just declare it as default.

        See this StackOverflow thread for more details.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply
        5
        • SGaistS SGaist

          Hi,

          It's by design. The TLDR: if you declare a custom spaceship operator, you have to declare the equality operator. If that operator should use the default implementation then just declare it as default.

          See this StackOverflow thread for more details.

          A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          @SGaist Many thanks, I must have missed that one!

          1 Reply Last reply
          0
          • A Asperamanca has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved