Search code examples
c++qtoverloadingshared-ptrqhash

Why doesn't Qt's qHash() have an overload for std::shared_ptr?


I just found out, to my surprise, that the following code does not compile out of the box in C++14 using Qt 5.4:

QSet<std::shared_ptr<SomeType>> var;

The problem is that there is no overload of the qHash() method for std::shared_ptr, or any other smart pointer as far as I can see: http://doc.qt.io/qt-5/qhash.html#related-non-members

It seems natural to me to have the following overload (or something similar):

template <typename T>
uint qHash(const std::shared_ptr<T>& ptr, uint seed = 0)
{
    return qHash(ptr.get(), seed);
}

but it does not exist. This cannot simply be something the Qt developers overlooked. Do I need to include a special header? What is the reason why this does not exist?


Solution

  • Speak of the devil, and he doth appear: https://codereview.qt-project.org/113340

    This cannot simply be something the Qt developers overlooked.

    It's not an overlook, it's just that Qt doesn't have endless resources to add qHash overloads to any STL type which has an operator==.