简体   繁体   English

unordered_map ::在VS2012中查找带有自定义哈希崩溃的关键std ::指针对

[英]unordered_map::find with key std::pair of pointers with custom hash crashes in VS2012

I needed a std::unordered_map with key a std::pair<T*, T*> so I "stole" the following code: 我需要一个带有键的std::unordered_map std::pair<T*, T*>所以我“偷”了下面的代码:

template <class T>
inline void hash_combine(std::size_t & seed, const T & v)
{
  std::hash<T> hasher;
  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

namespace std
{
  template<typename S, typename T> struct hash<pair<S, T>>
  {
    inline size_t operator()(const pair<S, T> & v) const
    {
      size_t seed = 0;
      ::hash_combine(seed, v.first);
      ::hash_combine(seed, v.second);
      return seed;
    }
  };
}

from this stackoverflow answer . 从这个stackoverflow 答案

It works like a charm on linux machines with gcc 4.9.2. 它在gcc 4.9.2的linux机器上就像一个魅力。 However in windows visual studio 2012 it crashes upon calling member function find() of my unordered_map . 但是在windows visual studio 2012中,它在调用我的unordered_map成员函数find()时崩溃了。 A friend of mine debugged the crash on windows machine and he reported that it breaks only in debug compilation mode by giving "vector subscript out of range". 我的一个朋友调试了Windows机器上的崩溃,他报告说它只是在调试编译模式下通过给出“矢量下标超出范围”而中断。

Q: 问:

  1. Is the code posted valid for hashing a std::pair<T*, T*> ? 发布的代码是否对散列std::pair<T*, T*>
  2. Is there a more robust/better way of hashing a std::pair<T*, T*> ? 是否有更强大/更好的散列std::pair<T*, T*>
  3. What causes this strange behaviour? 是什么导致这种奇怪的行为

PS: Deeply sorry for not posting a mcve but It's impossible to do so. PS:非常抱歉没有发布mcve,但这是不可能的。

Specialization of templates in std for types also in std may or may not make your program ill-formed (the standard is ambiguous, it seems to use "user-defined type" in multiple different ways without ever defining it). 在模板专业化std也为各类std可能会或可能不会让你的程序形成不良的(标准是模糊的,似乎在多种不同的方式使用“用户定义类型”而没有定义它)。 See my question on the subject , and active working group defect on the issue . 请参阅我关于此主题的问题 ,以及有关工作组的问题

So create your own hashing namespace: 因此,创建自己的哈希命名空间:

namespace my_hash {
  template<class T=void,class=void>
  struct hasher:std::hash<T>{};

  template<class T, class=std::result_of_t< hasher<T>(T const&) >>
  size_t hash( T const& t ) {
    return hasher<T>{}(t);
  }
  template<>
  struct hasher<void,void> {
    template<class T>
    std::result_of_t<hasher<T>(T const&)>
    operator()(T const& t)const{
      return hasher<T>{}(t);
    }
  };

  // support for containers and tuples:
  template <class T>
  size_t hash_combine(std::size_t seed, const T & v) {
    seed ^= hash(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    return seed;
  }

  template<class Tuple, size_t...Is>
  size_t hash_tuple_like(Tuple const& t, size_t count, std::index_sequence<Is...>) {
    size_t seed = hash(count);
    using discard=int[];
    (void)discard{0,((
      seed = hash_combine(seed, std::get<Is>(t))
    ),void(),0)...};
    return seed;
  }
  template<class Tuple>
  size_t hash_tuple_like(Tuple const& t) {
    constexpr size_t count = std::tuple_size<Tuple>{};
    return hash_tuple_like(t, count, std::make_index_sequence<count>{} );
  }
  struct tuple_hasher {
    template<class Tuple>
    size_t operator()(Tuple const& t)const{
      return hash_tuple_like(t);
    }
  };
  template<class...Ts>
  struct hasher<std::tuple<Ts...>,void>:
    tuple_hasher
  {};
  template<class T, size_t N>
  struct hasher<std::array<T,N>,void>:
    tuple_hasher
  {};
  template<class...Ts>
  struct hasher<std::pair<Ts...>,void>:
    tuple_hasher
  {};
  template<class C>
  size_t hash_container( C const& c ) {
    size_t seed = hash(c.size());
    for( const auto& x:c ) {
      seed = hash_combine( seed, x );
    }
    return seed;
  }
  struct container_hasher {
    template<class C>
    size_t operator()(C const& c)const{ return hash_container(c); }
  };
  template<class...Ts>
  struct hasher< std::vector<Ts...>, void >:
    container_hasher
  {};
  // etc
};

now you pass my_hash::hasher<> as your hasher to a container, and you don't have to do the sketchy business of providing a std specialization for a type (mostly) in std . 现在你将my_hash::hasher<>作为你的hasher传递给一个容器,你不必做一个粗略的业务,即在std为一个类型(主要是)提供std

my_hash::hasher<?,void> exists so you can do SFINAE testing (say, detect if a type is container-like, and forward to hash_container . my_hash::hash provides ADL overriding for types without having to fool around in the my_hash namespace. my_hash::hasher<?,void>存在,所以你可以做SFINAE测试(比如,检测一个类型为容器状,并转发到hash_containermy_hash::hash提供ADL压倒一切的类型,而不必在鬼混my_hash命名空间。

As an example: 举个例子:

template<class T>
struct custom {
  std::vector<T> state;
  friend size_t hash( custom const& c ) {
    using my_hash::hash;
    return hash(state);
  }
};

and custom is now hashable. custom现在可以清洗。 No messy specialization required. 不需要杂乱的专业化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM