Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to ensure comparison operator is "complete"?
Qt 6.12 LTS is out! See what's new in the release blog

How to ensure comparison operator is "complete"?

Scheduled Pinned Locked Moved C++ Gurus
13 Posts 4 Posters 9.0k Views 1 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
    #3

    I often have non-trivial comparisons, like fuzzyCompare for qreal values.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #4

      How should any algorithm know whether a new attribute in your class is relevant for a comparison?

      Even if the answer in your case would be "any one is relevant", that would be out of the scope of a compiler. One would have to analyze the source code and then to analyze the operator== method.

      The method Andre did outline, works too. But it cannot handle the cases where your forgot to make the new attribute a property. So you're not gaining that much from that.

      For our projects, I just would add a HUGE comment at the place where the attributes are added in the header:

      @
      /************************

      • CAUTION!
      • if you add an attribute here
      • ADD IT TO THE comparison methods too!
        ************************/
        @

      :-)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #5

        I was afraid this would be the answer...

        well, one could hope.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #6

          Yes, hope dies last :-)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #7

            Perhaps you can find a static code analysis tool that can warn for these cases?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #8

              Yes, I also thought this would be in the realm of static code analysis.
              If anyone knows about a static code analysis tool that can provide that, please speak up.

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #9

                Maybe this is a stupid question but do you need the operator==() for comparison?
                (edit:I meant to say, do you need == within the comparison?)
                It could be that it suffices for your application to compare the allocated memory
                blocks using sizeof(Class1/2) and memcmp()? OK, this is C not C++...

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #10

                  As the topic starter stated, he needs to do non-trival comparisons, like fuzzy comparisons of reals. That is never going to work with doing a memcmp. -Otherwise, relying on the default comparison operator would have been just as effective.-

                  Edit: there is no such thing as a default comparison operator in C++, thanks for correcting me.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Asperamanca
                    wrote on last edited by
                    #11

                    [quote author="Andre" date="1333107436"]Rely on the automatically generated ones?[/quote]

                    Took me a while to notice...since when do automatically generated comparison operators exist?

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #12

                      [quote author="Asperamanca" date="1334563036"][quote author="Andre" date="1333107436"]Rely on the automatically generated ones?[/quote]

                      Took me a while to notice...since when do automatically generated comparison operators exist?

                      [/quote]

                      Sorry, you are right: they don't. I thought there were, but I was wrong on this count. I stand corrected.

                      StackOverflow has an interesting "discusson":http://stackoverflow.com/questions/217911/why-dont-c-compilers-define-operator-and-operator on the why (not) of automatic operator==() definition.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Asperamanca
                        wrote on last edited by
                        #13

                        I think it's the same one I stumbled upon. ;-)

                        1 Reply Last reply
                        0

                        • Login

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