为什么我必须为每个命名空间定义一个散列函数作为unordered_set?

Why do I have to define a hash function for each namespace as the unordered_set?

我想为我正在写的 class 创建一个散列函数,我想让散列函数成为 class 的朋友,这样我就不必编写不必要的getter 方法。为此,我遵循了 this SO post 中接受的答案。但我希望能够将对象插入 std::unordered_setboost::unordered_set。所以我是这样写的:

#include <iostream>
#include <unordered_set>
#include <boost/functional/hash.hpp>
#include <boost/unordered_set.hpp>

class Vec;
namespace std { // line [c]
    template<>
    struct hash<Vec> {
    public:
        size_t operator()(const Vec &v) const;
    };
}

class Vec {
private:
    std::vector<int> v;
public:
    friend size_t std::hash<Vec>::operator ()(const Vec& v) const; // line [d]
    friend bool operator == (const Vec& lhs, const Vec& rhs) { return lhs.v == rhs.v; }
};

namespace std { // line [e]
    size_t hash<Vec>::operator()(const Vec &v) const {
        return boost::hash<std::vector<int> >()(v.v);
    }
}

int main() {
    Vec v;
    std::unordered_set<Vec> s1; // line [f]
    s1.insert(v); // line [g]
    boost::unordered_set<Vec> s2; // line [a]
    s2.insert(v); // line [b]
}

但是我发现在尝试编译这个时我有一个很长的错误列表。然后,当我删除行 [a,b] 时,它会按预期进行编译和 运行。然后,我没有删除行 [a,b],而是 (1) 将它们留在原处,(2) 删除了行 [f,g],并且 (3) 将行 [c,d,e] 更改为 boost std,代码将再次正确编译。最后,我尝试在 boost 命名空间中重复声明哈希结构:

#include <iostream>
#include <unordered_set>
#include <boost/functional/hash.hpp>
#include <boost/unordered_set.hpp>

class Vec;
namespace std {
    template<>
    struct hash<Vec> {
    public:
        size_t operator()(const Vec &v) const;
    };
}
// new: {
namespace boost {
    template<>
    struct hash<Vec> {
    public:
        size_t operator()(const Vec &v) const;
    };
}
// }

class Vec {
private:
    std::vector<int> v;
public:
    friend size_t std::hash<Vec>::operator ()(const Vec& v) const;
    // new: {
    friend size_t boost::hash<Vec>::operator ()(const Vec& v) const;
    // }
    friend bool operator == (const Vec& lhs, const Vec& rhs) { return lhs.v == rhs.v; }
};

namespace std {
    size_t hash<Vec>::operator()(const Vec &v) const {
        return boost::hash<std::vector<int> >()(v.v);
    }
}
// new: {
namespace boost {
    size_t hash<Vec>::operator()(const Vec &v) const {
        return boost::hash<std::vector<int> >()(v.v);
    }
}
// }

int main() {
    Vec v;
    std::unordered_set<Vec> s1;
    s1.insert(v);
    boost::unordered_set<Vec> s2;
    s2.insert(v);
}

我的问题是:为什么我必须在 stdboost 命名空间中创建哈希函数才能使其正常工作?我会说我对原因有直觉,但我想要一个非常详细的解释。而且我想要任何替代解决方案来解决上面代码段中有很多重复代码的事实(但不是 boost::unordered_set<Vec, my_vec_class_hash> 之类的东西,因为我希望它是“自动的”)。

您可以通过使用 Boost 的支持 ADL 的定制点来大大减少混乱 hash_value:

class Vec {
  private:
    std::vector<int> v;

    friend size_t hash_value(const Vec& v) {
        return boost::hash_range(begin(v.v), end(v.v));
    }
    friend bool operator==(const Vec& lhs, const Vec& rhs) {
        return lhs.v == rhs.v;
    }
};

In fact, the hash function can be even simpler with return boost::hash_value(v.v); in this case.

这已经足以使 Boost 的无序容器与您的类型一起工作:

boost::unordered_set<Vec> s2;
s2.insert(v);

添加标准支持

现在这不是问题:

template <> struct std::hash<Vec> : boost::hash<Vec> {};

现场演示

Live On Coliru

#include <boost/functional/hash.hpp>
#include <boost/unordered_set.hpp>
#include <iostream>
#include <unordered_set>

class Vec {
  private:
    std::vector<int> v;

    friend size_t hash_value(const Vec& v) {
        return boost::hash_value(v.v);
        //return boost::hash_range(begin(v.v), end(v.v));
    }
    friend bool operator==(const Vec& lhs, const Vec& rhs) {
        return lhs.v == rhs.v;
    }
};

template <> struct std::hash<Vec> : boost::hash<Vec> {};

int main() {
    Vec v;
    std::unordered_set<Vec> s1;
    s1.insert(v);
    boost::unordered_set<Vec> s2;
    s2.insert(v);
}