Questions tagged [stdhash]

c++11 library to ease hashing of user-defined types

API Reference: http://en.cppreference.com/w/cpp/utility/hash

59 questions
6
votes
2 answers

Why was std::hash not defined for std::weak_ptr in C++0x?

After reading the discussion on operator< for std::weak_ptr, I can't see any reason why defining std::hash to use the control block for std::weak_ptr wouldn't work. I also can't believe that this was ignored by the standards committee. Has anybody…
tgoodhart
  • 3,111
  • 26
  • 37
5
votes
1 answer

specialize std::hash for dependent types

I have defined this template class structure: template struct Outer { struct Inner { /* ...some stuff... */ }; }; I want to put Inner objects into an unordered_map (actually, not directly them but containers of them, so the approach…
pqnet
  • 6,070
  • 1
  • 30
  • 51
5
votes
3 answers

Specializing std::hash for nested class in a template class

I have a template class Baz which contains a nested class Sub. I'd like to define a hash function for this subclass by specializing std::hash. However, it doesn't seem to work. #include struct Foo { struct Sub { }; }; template…
Ambroz Bizjak
  • 7,809
  • 1
  • 38
  • 49
4
votes
2 answers

Specializing std::hash for private member class

I have a class (call it Outer) which has a private member class (Inner). I want to store instances of Outer::Inner in unordered standard containers, so I want to specialize std::hash. However, when writing this: namespace std { …
4
votes
2 answers

std::hash for unique ptr in unordered map

I'm trying to hold a polymorphic type as a key in a map. I came up with the following two structures: Note that Game is an abstract class and the data structure I use is : std::unordered_map _allGames; while gamePtr is a typedef…
Rouki
  • 2,239
  • 1
  • 24
  • 41
4
votes
1 answer

std::hash algorithm and size

I'm using C++11 and the std::hash algorithm. I was wondering, what actual hash implementation is used? I would assume MD5 or SHA, but I can't dig any info from the internets. Also, I'd like to know the actual returned bit-width of the hash, as I…
Ælex
  • 14,432
  • 20
  • 88
  • 129
4
votes
2 answers

g++ linker error: Getting undefined reference error for std::hash

I'm using the unordered_map of TR1 implementation in my code and the linker gives weird errors I cannot even decipher: BPCFG.o: In function `std::__detail::_Hash_code_base,…
Onur Cobanoglu
  • 211
  • 1
  • 6
  • 9
3
votes
1 answer

How to hash QVariant?

I need to use QList as a key to std::unordered_map. The purpose of this is to optimize searching over a table of data by making index over the unique key columns. So I made this code. It's not complete, but lists some basic data types that…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
3
votes
2 answers

Issue with invalid use of incomplete type when using std::tuple_element

The following code implements a hash function for a std::tuple which is then used in a different segment of my code-base in a std::unordered_map of std::tuples. // compute hash function recursively through each std::tuple element template
sjrowlinson
  • 3,297
  • 1
  • 18
  • 35
3
votes
2 answers

std::hash variations of object with arbitrary number of attributes of fundamental type

Discussion: Let's say I have a struct/class with an arbitrary number of attributes that I want to use as key to a std::unordered_map e.g.,: struct Foo { int i; double d; char c; bool b; }; I know that I have to define a…
101010
  • 41,839
  • 11
  • 94
  • 168
3
votes
1 answer

Should std::hash work when T is std::pair?

I was using an ordered set declared as so: std::set > myset; After doing some analysis of the way I was using the set, I concluded that an unordered_set would be a smarter choice. But when I changed…
jzions
  • 431
  • 3
  • 9
3
votes
2 answers

Using std::hash()(std::this_thread::get_id())

I'm currently working on getting a C++ application to compile in both Windows and Linux, during some debugging I've found that std::this_thread::get_id().hash() doesn't compile on Linux with gcc 4.8 (thanks to the comments in this thread). The…
Jeremy Natale
  • 75
  • 1
  • 9
3
votes
1 answer

c++11 speed comparison/cost std::hash equal versus std::string equal directly on 2 large strings

Hi I have a question on std::hash if I have 2 large strings for comparison and I am willing to accept that std::hash will compare equal in most cases is it more performance compliant to do something like the following instead of a direct string…
bjackfly
  • 3,236
  • 2
  • 25
  • 38
3
votes
2 answers

Use boost::hash_value to define std::hash in C++11

Is there an easy way to do the following with C++11 & Boost: use the standard definitions of std::hash whenever available from use boost::hash_value to define std::hash in those cases where std::hash is missing but boost::hash_value is…
Jukka Suomela
  • 12,070
  • 6
  • 40
  • 46
2
votes
1 answer

Generate fixed size hash

I am using the std::hash in cpp utility to generate the hash for the string. My requirement is to generate the fixed size hash of 11 digits. The hash function need not be great to never have collision. The only requirement that I have is to generate…
Hemant Yadav
  • 307
  • 1
  • 4
  • 19