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
23
votes
2 answers

Why is std::hash a struct instead of a function?

Standard library implements std::hash as a template struct that is specialized for different types. It is used like this: #include #include int main() { std::hash hasher; std::cout << hasher(1337) <<…
Scintillo
  • 1,634
  • 1
  • 15
  • 29
20
votes
3 answers

Can std::hash be used to hash function pointers?

Can the C++11 std::hash type be used to hash function pointers? There is a hash partial specialization defined as template struct hash; but since function pointers are different from other pointer types in C++ (e.g. they can't be…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
18
votes
3 answers

Using QString as the key in std::unordered_map

I'm trying to use QString as the key in a std::unordered_map, however I get the error: error C2280: 'std::hash<_Kty>::hash(const std::hash<_Kty> &)': attempting to reference a deleted function I can't switch to QHash because the value-type of the…
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
18
votes
3 answers

Why std::hash seems to be identity function

#include int main() { std::hash hash_f; std::cout << hash_f(0) << std::endl; std::cout << hash_f(1) << std::endl; std::cout << hash_f(2) << std::endl; std::cout << hash_f(3) << std::endl; } I compile with "g++…
François
  • 471
  • 1
  • 4
  • 10
17
votes
5 answers

Unexpected collision with std::hash

I know hashing infinite number of string into 32b int must generate collision, but I expect from hashing function some nice distribution. Isn't it weird that these 2 strings have the same hash? size_t hash0 =…
relaxxx
  • 7,566
  • 8
  • 37
  • 64
16
votes
2 answers

C++11: Are there reasons why some Regular Types should not have `std::hash` specialised?

With a Regular Type, I mean the definition of Stepanov in Elements of Programming, basically, that there's the concept of equality and that objects which are copies of each other compare equal. So when you have a Regular Type T, and the equality…
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
12
votes
1 answer

Why is std::hash not specialised for std::reference_wrapper?

I thought it would have been, but I can't find this in my standard library implementation (gcc-4.8.2). Why is std::hash not already specialised for std::reference_wrapper? #pragma once #include namespace std { template
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
11
votes
3 answers

Why isn't std::hash specialized for char*?

Why doesn't the C++ standard specify that std::hash is specialized for char*, const char*, unsigned char*, const unsigned char*, etc? I.e., it would hash the contents of the C string until the terminating null is found. Any harm in injecting my…
Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
10
votes
2 answers

Why is std::hash not an overloaded function?

I'm guessing std::hash is defined as a template struct in order to avoid implicit type conversions done during overloaded function resolution. Is it a correct thing to say? I mean, I would prefer to write std::string s; size_t hash =…
Alexei Sholik
  • 7,287
  • 2
  • 31
  • 41
10
votes
3 answers

Is std::hash guaranteed to be same across stdlib distributions

If I did std::hash using libstdc++ and then did one on the upcoming C++11 VS 2012 library - would they match? I assume that hash implementations are not part of the C++ specification and can vary by distribution?
Matt Clarkson
  • 14,106
  • 10
  • 57
  • 85
9
votes
1 answer

Specializing std::hash to derived classes

I have an abstract base class Hashable that classes that can be hashed derive from. I would now like to extend std::hash to all classes that derive from Hashable. The following code is supposed to do exactly that. #include #include…
Flecto
  • 315
  • 2
  • 7
9
votes
4 answers

Does std::hash guarantee equal hashes for "equal" floating point numbers?

Is the floating point specialisation of std::hash (say, for doubles or floats) reliable regarding almost-equality? That is, if two values (such as (1./std::sqrt(5.)/std::sqrt(5.)) and .2) should compare equal but will not do so with the == operator,…
bitmask
  • 32,434
  • 14
  • 99
  • 159
8
votes
1 answer

Does std::hash give same result for same input for different compiled builds and different machines?

I have some random test parameters for which I need to calculate a hash to detect if I ran with same parameters. I might run the test using the same source recompiled at a different time or run on a different machine. Even so I want to detect…
8
votes
2 answers

Can I override std::hash?

I can replace the actual implementation of std::hash with my own definition of std::hash in C++ 11 ? I mean from my codebase, without touching the standard library. I can't see any use for virtual function/polymorphism in this case, so I suppose…
user2485710
  • 9,451
  • 13
  • 58
  • 102
7
votes
1 answer

Specializing std::hash for templated Key

I was trying to specialize hash for my own type, a templated key. I was basing it off cppreference. I get the compile error "The C++ Standard doesn't provide a hash for this type". I figure I just did it wrong. Could the compiler even support this…
Nyaarium
  • 1,540
  • 5
  • 18
  • 34
1
2 3 4