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

1.1k Views Asked by At

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?

2

There are 2 best solutions below

2
On BEST ANSWER

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==.

0
On

This cannot simply be something the Qt developers overlooked.

It is, and it's not something that was overlooked - it simply is impossible to write all the code at once. One has to stop somewhere and do a release. Feel free to submit a code revision that will fix this :) Make sure, though, that the revision will build on all platforms, and with all supported compilers - some of them have libraries without std::shared_ptr!