【问题标题】:Trouble definining hash function for custom type in unordered_multimap on MacOS在 MacOS 上的 unordered_multimap 中为自定义类型定义哈希函数时遇到问题
【发布时间】:2020-07-15 02:33:57
【问题描述】:

所以我想使用自定义类型(此处为SWrapper)作为unordered_multimap 的键类型。我已经定义了一个散列类,它派生自字符串的标准散列函数,并将散列类包含在多映射的类型中。下面显示了一些重现错误的代码。这在使用 g++ 和 clang++ 的 Arch Linux 上编译,但在使用 clang++ 的 MacOS 上,我得到错误:

#include <unordered_map>
#include <functional>
#include <string>

class SWrapper {
    public:
    SWrapper() {
        (*this).name = "";
    }

    SWrapper(std::string name) {
        (*this).name = name;
    }

    bool operator==(SWrapper const& other) {
        return (*this).name == other.name;
    }

    std::string name;
};

class SWrapperHasher {
    size_t operator()(SWrapper const& sw) const {
        return std::hash<std::string>()(sw.name);
    }
};

int main(int argc, char* argv[]) {
    auto mm = std::unordered_multimap<SWrapper, int, SWrapperHasher>();
    return 0;
}

在 Arch Linux(或 clang++)上运行 g++ -std=c++11 -Wall -Wpedantic -Wextra hash_map_test.cpp -o hash_map_test 编译代码不会出错。但是,在 MacOS 上,使用相同的命令,我收到以下错误消息:

In file included from hash_map_test.cpp:1:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/unordered_map:408:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__hash_table:868:5: error: 
      static_assert failed due to requirement 'integral_constant<bool, false>::value' "the
      specified hash does not meet the Hash requirements"
    static_assert(__check_hash_requirements<_Key, _Hash>::value,
    ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__hash_table:883:1: note: in
      instantiation of template class
      'std::__1::__enforce_unordered_container_requirements<SWrapper, SWrapperHasher,
      std::__1::equal_to<SWrapper> >' requested here
typename __enforce_unordered_container_requirements<_Key, _Hash, _Equal>::type
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/unordered_map:1682:26: note: while
      substituting explicitly-specified template arguments into function template
      '__diagnose_unordered_container_requirements'
    static_assert(sizeof(__diagnose_unordered_container_requirements<_Key, _Hash, _Pred>(...
                         ^
hash_map_test.cpp:29:15: note: in instantiation of template class
      'std::__1::unordered_multimap<SWrapper, int, SWrapperHasher, std::__1::equal_to<SWrapper>,
      std::__1::allocator<std::__1::pair<const SWrapper, int> > >' requested here
    auto mm = std::unordered_multimap<SWrapper, int, SWrapperHasher>();
              ^
1 error generated.

我已经尝试解释错误消息,但我真的不知道该怎么做。如果有人对这里发生的事情以及如何在 MacOS 上解决此问题有任何建议,我们将不胜感激!

【问题讨论】:

  • @JaMiT 好点 - 我刚刚尝试使用 clang 在我的 Linux 机器上编译代码,这也有效。将更新问题以反映这一点。
  • @JaMiT 做对了 - 你的哈希 operator() 不公开。
  • @MadMonty - 区别在于 libstdc++ 与 libc++,而不是 clang 与 gcc。如果您使用 -stdlib=libc++ 尝试 Arch linux,它也应该会失败。
  • 感谢大家的意见!已接受以下答案。那么为什么它在 libstdc++ 上编译而没有警告或错误呢?
  • 由于编译器错误,它在 libstdc++ 上编译:gcc.gnu.org/bugzilla/show_bug.cgi?id=41437(感谢 Jonathan Wakely 提供链接)

标签: c++ stl unordered-map unordered-multimap


【解决方案1】:

不幸的是,编译器的投诉并未说明未满足哪个要求。在这种情况下,问题来自SWrapperHasher::operator()private。标记public(将class 更改为struct 会隐式执行此操作)使无序映射合法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 2020-07-08
    • 2018-06-07
    • 1970-01-01
    相关资源
    最近更新 更多