Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-112534

QMultiHash functions (size, isEmpty) return unexpected values

    XMLWordPrintable

Details

    • Windows
    • 87103e04e (dev), 72487c52f (6.5), 1e46bc05d (tqtc/lts-6.2)

    Description

      When using a QMultiHash, I experienced an unexpected value returning from the function QMultiHash::size()

      I went to the docs (I'm using Qt 6.2.7) and found that according to them, QMultiHash doesn't HAVE a size function. I acknowledge that I'm doing something that's outside the advertised API and I can't technically expect anything. But, still, it's a size function. I know what I expected it to do.

       

      I used an idiomatic Qt function that exists in many Qt containers, and expected it to work. THAT'S the bug I'm reporting, I think. I recognise that the function, while public, isn't documented and thus I shouldn't use it. HOWEVER, I used it without even thinking about it; because Qt containers have a size function that I expect to tell me the size of the container.

      This code snippet:

        QMultiHash<int, int> mult;
          std::cout << mult.size() << std::endl; // Size is zero here. Empty new QMultiHash.
          mult[4] = 5;
          std::cout << mult.size() << std::endl; // Size is zero here. Says it contains nothing?
          std::cout << mult[4] << std::endl;  // Yet this recovers the value; clearly not empty.
          mult.insert(6,7);
          std::cout << mult.size() << std::endl; // Size is one here but I can extract two values from it
          std::cout <<mult[4] << std::endl;
          std::cout << mult[6] << std::endl;
      

       

      Outputs:
      0 ("size" of newly constructed QMultiHash)
      0 ("size" of newly constructed QMultiHash, after I've put a value into it using operator[]. THIS WAS UNEXPECTED - THERE IS SOMETHING IN THE CONTAINER. THE SIZE SURELY CANNOT BE ZERO)
      5 (the value I put in, read back from the QMultihash)
      1 (the "size" of the QMultiHash after I put another value into it, using insert. THIS WAS ALSO UNEXPECTED - NAIVELY, SURELY SHOULD BE 2)
      5 (the first value I put in, read back from the QMultihash)
      7 (the second value I put in, read back from the QMultihash)

      Two values are stored in the QMultiHash, but its (undocumented) size() function says one. 

      QMultiHash::keys() returns a list of keys of the expected size. I can reliably figure out the actual size of the QMultiHash. That's not the problem.

       

      The problem is that the isEmpty function and size functions for QMultiHash, while not documented, exist and can be called and return very surprising values if I've put something into the QMultiHash object using operator[]

       

      It's very easy for programmers to accidentally use these functions without realising they're not documented and make no promises about what they do.

       

      I think they should be private functions (or removed, or in some other way made impossible for us to accidentally call them) or should return the values we would expect.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            jamesb James Browning
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There is 1 open Gerrit change