stdhash

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

最后都变了- 提交于 2019-11-30 17:10:26
Can the C++11 std::hash type be used to hash function pointers? There is a hash partial specialization defined as template <typename T> struct hash<T*>; but since function pointers are different from other pointer types in C++ (e.g. they can't be cast to void* ), I'm not sure whether it is safe to use it for types like int(*)() or void(*)(int, int) . Is this permitted? Is there any specific wording in the new ISO spec that supports or refutes this? Thanks! Great question. I don't know the answer for sure, and I'm happy to defer to anyone with better knowledge than me, but my thinking is that

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

☆樱花仙子☆ 提交于 2019-11-30 16:28:04
问题 Can the C++11 std::hash type be used to hash function pointers? There is a hash partial specialization defined as template <typename T> struct hash<T*>; but since function pointers are different from other pointer types in C++ (e.g. they can't be cast to void* ), I'm not sure whether it is safe to use it for types like int(*)() or void(*)(int, int) . Is this permitted? Is there any specific wording in the new ISO spec that supports or refutes this? Thanks! 回答1: Great question. I don't know

Unexpected collision with std::hash

柔情痞子 提交于 2019-11-30 08:00:38
问题 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 = std::hash<std::string>()("generated_id_0"); size_t hash1 = std::hash<std::string>()("generated_id_1"); //hash0 == hash1 I know I can use boost::hash<std::string> or others, but I want to know what is wrong with std::hash . Am I using it wrong? Shouldn't I somehow "seed" it? 回答1: There's

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

你说的曾经没有我的故事 提交于 2019-11-29 15:34:22
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 element is 0, second 1 etc), but where exactly I should put this specialization and how should it look like? I can't modify the library sources. enum LibEnum { A, B, C, D}; std::unordered_set <LibEnum> mySet; //need std::hash for LibEnum //how should it look like? You can just specialise std::hash for your type: namespace std { template <> struct

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

一曲冷凌霜 提交于 2019-11-28 22:45:52
问题 Standard library implements std::hash as a template struct that is specialized for different types. It is used like this: #include <iostream> #include <functional> int main() { std::hash<int> hasher; std::cout << hasher(1337) << std::endl; return 0; } My question is what is the reasoning behind this design choice. Why it isn't implemented as a template function and used like this: #include <iostream> #include <functional> int main() { std::cout << std::hash<int>(1337) << std::endl; return 0;

Specializing std::hash to derived classes

旧城冷巷雨未停 提交于 2019-11-28 13:53:06
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 <functional> #include <type_traits> #include <iostream> class Hashable { public: virtual ~Hashable() {} virtual std::size_t Hash() const =0; }; class Derived : public Hashable { public: std::size_t Hash() const { return 0; } }; // Specialization of std::hash to operate on Hashable or any class derived from // Hashable. namespace std { template<class C> struct hash {

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

人盡茶涼 提交于 2019-11-28 09:35:36
问题 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 element is 0, second 1 etc), but where exactly I should put this specialization and how should it look like? I can't modify the library sources. enum LibEnum { A, B, C, D}; std::unordered_set <LibEnum> mySet; //need std::hash for LibEnum //how