std::hash specialization using sfinae?

前端 未结 1 1886
小鲜肉
小鲜肉 2020-12-06 06:37

As an exercise I was trying to see if I could use SFINAE to create a std::hash specialization for std::pair and std::tuple when all of

相关标签:
1条回答
  • 2020-12-06 07:15

    You are not supposed to specialize std::hash for types that doesn't depend on a type you defined yourself.

    That said, this hack might work:

    template<class T, class E>
    using first = T;
    
    template <typename T>
    struct hash<first<std::pair<T, T>, std::enable_if_t<std::is_unsigned<T>::value>>>
    {
        size_t operator()(const std::pair<T, T>& x) const
        {
            return x;
        }
    };
    

    Really, though, don't do this. Write your own hasher.

    0 讨论(0)
提交回复
热议问题