簡體   English   中英

unordered_map ::在VS2012中查找帶有自定義哈希崩潰的關鍵std ::指針對

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

我需要一個帶有鍵的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;
    }
  };
}

從這個stackoverflow 答案

它在gcc 4.9.2的linux機器上就像一個魅力。 但是在windows visual studio 2012中,它在調用我的unordered_map成員函數find()時崩潰了。 我的一個朋友調試了Windows機器上的崩潰,他報告說它只是在調試編譯模式下通過給出“矢量下標超出范圍”而中斷。

問:

  1. 發布的代碼是否對散列std::pair<T*, T*>
  2. 是否有更強大/更好的散列std::pair<T*, T*>
  3. 是什么導致這種奇怪的行為

PS:非常抱歉沒有發布mcve,但這是不可能的。

在模板專業化std也為各類std可能會或可能不會讓你的程序形成不良的(標准是模糊的,似乎在多種不同的方式使用“用戶定義類型”而沒有定義它)。 請參閱我關於此主題的問題 ,以及有關工作組的問題

因此,創建自己的哈希命名空間:

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
};

現在你將my_hash::hasher<>作為你的hasher傳遞給一個容器,你不必做一個粗略的業務,即在std為一個類型(主要是)提供std

my_hash::hasher<?,void>存在,所以你可以做SFINAE測試(比如,檢測一個類型為容器狀,並轉發到hash_containermy_hash::hash提供ADL壓倒一切的類型,而不必在鬼混my_hash命名空間。

舉個例子:

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

custom現在可以清洗。 不需要雜亂的專業化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM