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:

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.

It works like a charm on linux machines with gcc 4.9.2. However in windows visual studio 2012 it crashes upon calling member function find() of my unordered_map. 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".


  1. Is the code posted valid for hashing a std::pair<T*, T*>?
  2. Is there a more robust/better way of hashing a std::pair<T*, T*>?
  3. What causes this strange behaviour?

P.S: Deeply sorry for not posting a mcve but It's impossible to do so.

-------------Problems Reply------------

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). 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);
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[];
seed = hash_combine(seed, std::get<Is>(t))
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);
struct hasher<std::tuple<Ts...>,void>:
template<class T, size_t N>
struct hasher<std::array<T,N>,void>:
struct hasher<std::pair<Ts...>,void>:
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); }
struct hasher< std::vector<Ts...>, void >:
// 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<?,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.

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. No messy specialization required.

Category:c# Views:10 Time:2019-02-07

Related post

  • hadoop MapReduce: find max key value pair from output of mapper 2011-12-13

    It sounds like a simple job, but with MapReduce it doesn't seem that straight-forward. I have N files in which there is only one line of text for each file. I'd like the Mapper to output key value pairs like < filename, score >, in which 'score

  • JavaScript and ASP.NET - Cookies with Key / Value Pairs 2010-04-06

    This question mixes client-side scripting with server-side parsing. In some cases, I'm writing a cookie to the user's browser using the document.cookie property. In other cases, I'm writing the same cookie to the user's browser through the ASP.NET Re

  • Perl to JSON, with key/value pairs 2012-04-09

    I am trying to output some data from Perl to JSON. I can do a simple output, but would like to structure it better. I have an array with an id, a start time and an end time. This is the code I am using to output: print header('application/json'); my

  • i bought a copy of office 2010 from my job a year ago and now I want to up grade my system. I cannot find the key that I installed office with...what can I do? 2014-08-27

    i bought a copy of office 2010 from my job a year ago and now I want to up grade my system. I cannot find the key that I installed office with...what can I do? --------------Solutions------------- Is this a retail version of Office 2010? Do you have

  • With Hibernate, how can I query a table and return a hashmap with key value pair id>name? 2011-01-19

    I've got this table : table name : Account Fields : id (varchar), name(varchar), other fields... I want to query this table with hibernate mechanism (to use the second cache level). The result of the hibernate query must be a hash map where the key i

  • Java - making objects with key/value pairs? 2011-08-05

    I want to make the following types of objects. This is my higher-level desire that I'd like to figure out in Java: ListObject(key, String): every key corresponds to a String value; key is a string itself ListObject(key, String[]): every key correspon

  • how to fill a jquery autocomplete list with key value pairs from web service 2010-02-07

    Can somebody please give an example on passing key value pairs to a jQuery autocomplete list using a webservice. Tanks for the help. $('#txtBox1').autocomplete("Autocomplete.asmx/GetKeyValu", { dataType: "xml", datakey: "string", max: 10, minChars: 0

  • Separate String with Key value Pair 2011-01-03

    I've a string array which has got key and value which are separated using a delimiter. I need to separate the key value pair and move it to a HashMap. The string is as follows. String[] str={"12345$abcd","12332$abcgd","5555$afdsd"}; /* I need to move

  • SQL query select and ordering all from same table with key/value pairs 2012-02-19

    OK, I have a single table with keys and values that I need to query and order from that looks somewhat like this: Let's say I want to pull all distinct items (resource_no) that are in the webcategory of "dog" (and any other values) and order them by

  • WSDL enumeration restriction with key/value pairs 2012-03-16

    I'm working on a SOAP webservice which features many inputfields using enumeration restrictions. These enumerations are much like an HTML select/option setup; I expect a certain value to be returned but the label of that value should be exposed using

  • Add dynamic key, value pairs to JavaScript array or hash table 2012-02-22

    I'm trying to add a key value pair to an existing javascript associative array. The key needs to be a variable. This is for JSON encoding. I realize there are many plugins and frameworks for this, but I want a simple answer. ary.push({name: val}); wh

  • finding foreign keys in ms sql server with information_schema 2011-11-10

    I'm trying to find all the tables that have foreign keys pointing to a particular table. So I wrote this: select t1.TABLE_NAME as pointsfrom, t2.TABLE_NAME as pointsto from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS r join INFORMATION_SCHEMA.TABLE_CO

  • array_push() with key value pair 2009-08-30

    I have an existing array and I want to add a value to the array. Normally I would use array_push() to accomplish this but somehow it's not working. Here is my code: $data = array( "dog" => "cat" ); array_push($data['pussy'], 'wagon'); I don't simp

  • How to store key-value pairs in Redis with erlang and then access that store with php? 2011-12-01

    I'm new to redis, I'm storing a list in redis through erlang but I want to access that list from another server through php... is that possible? --------------Solutions------------- That shouldn't be a problem - just make your redis instance availabl

  • Joining an array of keys to a hash with key value pairs like excel vlookup 2012-04-02

    I've got an unsorted array of keys like this: keys = ["ccc", "ddd", "ggg", "aaa", "bbb"] and a hash hash = {"ddd" => 4, "aaa" => 1, "bbb" => 2, "eee" => 5, "fff" => 6} I'd like to join these two data structures to return a hash in the

  • How to create data structure with key duplicates in php instead of default hash? 2009-11-25

    I want to create wrapper class, which will enable keys duplicates while default hash does not allow it. Class should use member overloading mechanism introduced in php5, so it would imitate all the behavior standard hash has. For example, I want to h

  • Scala map with keys of same type but with irregular types for values and subtypes of values themselves being irregular 2011-07-09

    Possible Duplicate: Scala map containing mix type values I have a situation where in a previous groovy program is constructing a structure like mp = [k1: "string", k2: [d1: [1,2,3], d2: 1975], k3: 345, k4: ["one","two"]] which amounts to: [String: St

  • search for a key value pair in a list of hash maps 2011-04-28

    I am reading two csv files containg a set of attributes File 1 attributes = name, class, rollno, File 2 attributes = rollno, city,town I need to match the two files and for every matching rollno I have to append the File 2 attributes into File1 and c

  • How do you grab a key/value pair from an array of hashes in ruby? 2012-04-23

    I have an array of hashes (or atleast I think they are hashes) and I need to pull out the ID for each of them. I'm sure ruby has some quick way of doing this ... I just can't figure it out. I don't want to iterate through the arrays and build a new o

  • struct with 2 cells vs std::pair? 2010-08-31

    Possible Duplicate: What is the difference between using a struct with two fields and a pair? Dear all, I have a little question about pairs and struct. Is there any advantage to use a std::pair instead of a struct with two cells ? I have used pairs

  • Difference between std::pair and std::tuple with only two members? 2011-07-14

    Is there a difference between an std::pair and an std::tuple with only two members? (Besides the obvious that std::pair requires two and only two members and tuple may have less...) --------------Solutions------------- There are some differences: std

  • How to transform XML with entries of key value pairs to a HTML table? 2012-02-16

    I have an multiple entities (<data> in my example) with key-value pairs. Every entity contains the same keys in the same order, but I don't know which and how many. How to transform this to a HTML table using XSLT, having the keys in the table

  • Literate Coding Vs. std::pair, solutions? 2009-06-25

    As most programmers I admire and try to follow the principles of Literate programming, but in C++ I routinely find myself using std::pair, for a gazillion common tasks. But std::pair is, IMHO, a vile enemy of literate programming... My point is when

  • Is there a convenient way to wrap std::pair as a new type? 2008-10-14

    Often times I find myself using std::pair to define logical groupings of two related quantities as function arguments/return values. Some examples: row/col, tag/value, etc. Often times I should really be rolling my own class instead of just using std

  • union members may not have constructors, but `std::pair` okay? 2009-12-11

    union members may not have destructors or constructors. So I can't template the following class Foo on my own MyClass if MyClass has a constructor: template<class T> struct Foo { T val; Foo(T val_) : val(val_) {} size_t hash() const { union {T

Copyright (C), All Rights Reserved.

processed in 0.092 (s). 11 q(s)