What does QMultiHash::insert(key, value) return?
-
I used to think Qt has a good quality documentation, but apparently not always. What does this method return?
I'm asking because my code relies on it returning an iterator to the newly inserted item, which was the case in Qt 5.15, but with Qt 6.5 it returns an iterator to the first item. Quite a breaking change, as you can imagine.
Seems like it might be returning the first item with the same key, rather than the newly inserted item. The important part is that the docs don't say what the return value is. -
The result for Qt6 is the one I expected from reading the code - it returns the iterator to the first element for the key (in the bucket).
Will create a bug report for the documentation update. -
Since it's using std::multimap internally: https://en.cppreference.com/w/cpp/container/multimap/insert
But yes, the documentation should be updated.
-
@Christian-Ehrlicher, weird: it says "Returns an iterator to the inserted element", but that's not what is actually returned.
-
The please provide a testcase.
Note: Qt5 and Qt6 may differ here. -
Apologies, I've made a mistake that's probably an important distinction: my question is about
QMultiHash
, notQMultiMap
. What isQMultiHash::insert()
supposed to return? The docs similarly don't mention the return value.Here's the demo. This code returns 2 with Qt 5.15 (expected) and 1 with Qt 6.5 (unexpected, and cost me over an hour of debugging).
#include <QMultiHash> #include <QDebug> struct key { bool operator==(const key& other) const { return x == other.x; } int x; int y; }; inline uint qHash(const key& k) { return qHash(k.x); } int main(int argc, char *argv[]) { QMultiHash<key, int> m; m.insert({1, 1}, 1); const auto it = m.insert({1, 2}, 2); qInfo() << it.key().y; return 0; }
-
The result for Qt6 is the one I expected from reading the code - it returns the iterator to the first element for the key (in the bucket).
Will create a bug report for the documentation update. -
@Christian-Ehrlicher
Thank you very much!To me this behavior makes little sense because I can't think of what it could be useful for (unlike the behavior of Qt 5). But that doesn't matter; what matters is that programmers should know how to the use the interface correctly and what to expect - your bug report will solve that.
-
-