2

I am facing an issue hashing __uint128_t. Following is my code:

#include <iostream> int main () { __uint128_t var = 1; std::cout << std::hash<__uint128_t> () (var) << "\n"; return 0; } 

I am getting the error as:

test.cpp: In function ‘int main()’: test.cpp:5:40: error: use of deleted function ‘std::hash<__int128 unsigned>::hash()’ 5 | size_t h = std::hash<__uint128_t> () (var); | ^ 

How can I get the hash for __uint128_t? (Probably a very basic question but I have been stuck here for a while). Also, I would like to know the meaning of the error. Thanks in advance.

3
  • You can separate it into two 64-bit values and combine their hashes. Commented Apr 3, 2021 at 12:38
  • Okay, do you know what the error is trying is to say? Commented Apr 3, 2021 at 12:53
  • 1
    __uint128_t is not a standard type so std::hash is not required to support it. Commented Apr 3, 2021 at 14:52

1 Answer 1

6

Taking a look at the docs on https://en.cppreference.com/w/cpp/utility/hash. You will have to write your own.

Here is some code for what a basic __uint128_t hash function might look like:

namespace std { template<> struct hash<__uint128_t> { size_t operator()(__uint128_t var) const { return std::hash<uint64_t>{}((uint64_t)var ^ (uint64_t)(var >> 64)); } }; } 

Note not tested or compiled.

Sign up to request clarification or add additional context in comments.

1 Comment

Plus one. Could you further include the referenced hash_combine function instead of a simple xor?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.