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.

1 Answers1

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.

ceorron
  • 1,230
  • 1
  • 17
  • 28