stdhash

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

杀马特。学长 韩版系。学妹 提交于 2020-07-20 08:02:35
问题 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 whether the same parameters were used for the run. Does std::hash give the same result for the same input for different compiled builds and different machines? e.g. std::hash<string>{}("TestcaseParamVal0.7Param0.4"); Will this always be a unique number?

Specializing std::hash to derived classes

你离开我真会死。 提交于 2020-01-09 11:40:13
问题 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

My std::hash for std::tuples… Any improvements? [closed]

旧时模样 提交于 2019-12-19 11:40:08
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . Some may have noticed that std::hash does not support tuples. So I added an overload which just seems "nicer" than the solution I saw up till now. Anyone got ideas to further cut down this code? Please note that this is a compiler killer! The only one that could compile it

specialize std::hash<T> for dependent types

∥☆過路亽.° 提交于 2019-12-12 12:19:38
问题 I have defined this template class structure: template<typename T> 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 of specifying hashing object directly on template parameters for unordered_map is not a great idea), thus I wanted to specialize the hash class for these items. This will not work, because the compiler cannot match Outer<T>::Inner with the type

Can I override std::hash?

醉酒当歌 提交于 2019-12-10 13:50:03
问题 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 that I can't alter the definition of std::hash anyway ? 回答1: Yes it's okay, and you don't have to modify the standard library in any way, just use template specialization: namespace std { template<> struct hash<YourSpecialType> { // ... }; } 回答2: You

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

女生的网名这么多〃 提交于 2019-12-10 04:35:30
问题 Is the floating point specialisation of std::hash (say, for double s or float s) 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, how will std::hash behave? So, can I rely on a double as an std::unordered_map key to work as expected? I have seen "Hashing floating point values" but that asks about boost; I'm asking about the C++11 guarantees. 回答1: std::hash has same

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

给你一囗甜甜゛ 提交于 2019-12-06 17:25:35
问题 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 relation is transitive ( a == b && b == c => a == c ), you can define a ( non-trivial ) hash function which is consistent with the definition of equality ( a == b => h(a) == h(b) ). Always. But the standard doesn't include many std::hash

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

懵懂的女人 提交于 2019-12-05 07:00:21
Is the floating point specialisation of std::hash (say, for double s or float s) 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, how will std::hash behave? So, can I rely on a double as an std::unordered_map key to work as expected? I have seen " Hashing floating point values " but that asks about boost; I'm asking about the C++11 guarantees. std::hash has same guarantees for all types over which it can be instantiated: if two objects are equal, their hash codes will be

unordered_map::find with key std::pair of pointers with custom hash crashes in VS2012

江枫思渺然 提交于 2019-12-01 13:34:30
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

unordered_map::find with key std::pair of pointers with custom hash crashes in VS2012

拜拜、爱过 提交于 2019-12-01 10:51:15
问题 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