Garmaine Staff asked 2 years ago

I have a base Base class. The goal is to force specialize std::hash for std::shared_ptr with all the classes that inherit from Base.

I have tried the following approach with a dummy template parameter but the compiler error obviously complains that this is a redefinition of struct std::hash<std::shared_ptr<_Tp>>.

class Base {
};

class Derived : public Base {
};

namespace std {

template<typename T,
         typename std::enable_if<std::is_base_of<Base, T>::value, Base>::type* = nullptr
>
struct hash<std::shared_ptr<T>> {    
    size_t operator()(const std::shared_ptr<T>& d) const {
        return 616;
    }
};
}

My question is: is it possible to conditionally specialize an std class like this?