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
2
votes
1 answer

How to hash a buffer with std::hash?

I study std::hash's references, and find it can't hash a serialized data, like char*. Is it correct or normal ? How can I hash a serialized buffer?
naive231
  • 1,360
  • 1
  • 11
  • 28
2
votes
1 answer

Override std::hash to use Google's City Hash

So, simple enough question as I'm having a brain dead moment.. How can I override/replace std::hash to use Google's City Hash? My current approach is to have a wrapper around std::string and then specialize std::hash<> for that. But…
Nim
  • 33,299
  • 2
  • 62
  • 101
2
votes
1 answer

How to specialize std::hash for type from other library

So the library I use has an enum (say it's named LibEnum). I need to have an std::unordered_set of LibEnum, but I get compilation error that there is no specialized std::hash for it. I could easily write it and just return the number of value (first…
user1873947
  • 1,781
  • 3
  • 27
  • 47
1
vote
2 answers

How to partially specialize std::hash template?

Let's say we have a snippet of code as follows #include #include #include int main() { std::list myList = {4, 1, 3, 2}; std::unordered_map::iterator, std::string> myMap; …
vamsi3
  • 115
  • 4
1
vote
1 answer

How can I hash a type using the std::hash of its base class?

I have a class template C which inherits from std::string, and I want to make it hashable like a normal std::string. I have written the following code, and it compiles. I wonder if it is the correct implementation. Since a cast from derived class…
calvin
  • 2,125
  • 2
  • 21
  • 38
1
vote
1 answer

Are 2 instances of `std::hash` equivalent?

Assume my_hasher is a hashing function object. Should the following be correct, according to the Standard? my_type k; assert(my_hasher{}(k) == my_hasher{}(k)); The cppreference states the above assertion is correct. However, the Standard…
Igor R.
  • 14,716
  • 2
  • 49
  • 83
1
vote
1 answer

Multiple std::hash specialisation

I have a class class Base { ... virtual size_t GetHash() = 0; ... }; and a number of classes, inherited from Base, that override GetHash(). I want to use these classes as a key in unordered_map or unordered_set. Currently I achieve it by…
1
vote
1 answer

std:hash with access to private members of a class

I would like to hash a class that has two private members e.g.: foo.h class Foo { private: std::string a; std::string b; public: Foo (std::string a, std::string b); bool operator==(const Foo& other) const; …
Luke
  • 884
  • 8
  • 21
1
vote
1 answer

C++ std::hash return type

So I've been reading a lot of articles, documentation pages, posts, benchmarks, etc., concerning the use of std::hash and its standard implementations. Synopsis Looking here it seems that std::hash will always return an std::size_t, which following…
alexpanter
  • 1,222
  • 10
  • 25
1
vote
1 answer

Conditionally specialize std::hash for std::shared_ptr struct

I have a base Base class. The goal is to force specialize std::hash for std::shared_ptr with all the classes that inherit from Base. I have tried the following approach with a dummy template parameter but the compiler error obviously complains that…
rafaLiusz
  • 33
  • 1
  • 5
1
vote
1 answer

Using std::hash for custom class

Do the following two return statements return the same thing? class NonTrivialClass { public: size_t hash() const { // variation 1 return std::hash::_Do_hash(my_val_); // variation…
1
vote
1 answer

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 so I "stole" the following code: template inline void hash_combine(std::size_t & seed, const T & v) { std::hash hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed…
101010
  • 41,839
  • 11
  • 94
  • 168
1
vote
1 answer

Functor for std::hash

I was wondering what would be the optimal way to calculate the hash, considering that the values of ptime that are used as the key differ mostly in the hour and date (minutes and seconds are usually 0). I have done this but I feel that it is quite…
huff
  • 1,954
  • 2
  • 28
  • 49
1
vote
2 answers

Partial std::hash specialization for const and non-const types

I am having trouble with some code doing partial specialization of std::hash for one of my classes. Here's a self contained example. EDIT: Thanks Piotr, I changed the code to include the fixes but the error remains. #include #include…
Paulo Matos
  • 1,614
  • 17
  • 23
1
vote
0 answers

clang++ unable to find std::hash when using c++11

I'm trying to compile some software on FreeBSD 9.2 and have run into a problem. Not being a c++ developer myself, and apparently being unable to find the right bit of into in google, I've been unable to solve the issue. The developer inserted…
mpounsett
  • 1,174
  • 1
  • 10
  • 30