[Development] Question about the lifetime of references taken from QHash

Jaroslaw Kobus Jaroslaw.Kobus at qt.io
Thu Mar 11 12:30:12 CET 2021

Hi All,

asking it here, as I couldn't find any clear answer in QHash docs.

Provided I have only one instance of my QHash (so, no detaches are being done), and the only API of the QHash I'm using is: insert(), remove(), contains() and operator[], how long I may rely on the reference to value returned by QHash::operator[]?

    QHash<int, MyValue> hash;
    hash.insert(1, MyValue(...));
    hash.insert(2, MyValue(...));

    MyValue &val = hash[1];

    // May I rely that now my reference is still valid? Wondering, if any internal rehash on remove could copy internal data, so that it invalidates the reference obtained earlier.

Assuming that I keep and use this reference only until I explicitly called hash.remove() for the key associated with my reference - may I assume that this reference will be valid?

Best regards


More information about the Development mailing list