Creating an std::unordered_map with an std::pair as key

后端 未结 2 829
情歌与酒
情歌与酒 2020-12-14 18:52

I am trying to create an std::unordered_map with an std::pair as key. As you can imagine, this would require me to explicitly provide a class to generate a hash for a given

相关标签:
2条回答
  • 2020-12-14 18:59

    You need to make the operator() a const method in your custom functors.

    0 讨论(0)
  • 2020-12-14 19:18

    Apparently, libstdc++ refers to your hash object via const PairHash<int, int>*. Thus calling the operator () which is not marked const in your program is a compiler error.

    You can get your code to compile with libstdc++ by making operator() const.

    As of 17.6.3.4 (Hash Requirements), a Hash type must provide a size_t operator(KeyType) const;, so your code is indeed incorrect.

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