Iter 2: Reusable, robust c++ std::hash for GMP's big integer type This is the 2nd iteration of a code review. The 1st iteration (completed) is at
Iter 1: Reusable, robust c++ std::hash&lt;mpz_class&gt; for GMP&#39;s big integer type
1. Goal
My intention is to provide a fast hashing algorithm to hash GMP's big integer type mpz_class and mpz_t so I can use these types as keys for an unordered_map. The code shall be reusable for others.
2. Current Approach
Since C++17, the standard library provides the specialization hash&lt;string_view&gt; which is used to produce the initial hash value.
First, the magnitude data of the big integer is wrapped into a string_view and then its hash value is calculated using hash&lt;string_view&gt;. This produces an initial hash value which only depends on the magnitude, but not on the sign, of the big integer.
To keep the hashes of positive and negative big integers different, the initial hash value is scrambled once for negative big integers only.
3. Code
File hash_mpz.h:
#ifndef HASH_MPZ_H_
#define HASH_MPZ_H_

#include &lt;gmpxx.h&gt;

namespace std {

template&lt;&gt; struct hash&lt;mpz_srcptr&gt; {
    size_t operator()(const mpz_srcptr x) const;
};

template&lt;&gt; struct hash&lt;mpz_t&gt; {
    size_t operator()(const mpz_t x) const;
};

template&lt;&gt; struct hash&lt;mpz_class&gt; {
    size_t operator()(const mpz_class &amp;x) const;
};

}

#endif /* HASH_MPZ_H_ */

File hash_mpz.cpp:
#include &quot;hash_mpz.h&quot;
#include &lt;cstddef&gt;
#include &lt;string_view&gt;

constexpr size_t pi_size_t() {
    if (sizeof(size_t) == 4) {
        return 0xc90fdaa2; // floor(pi/4 * 2^32)
    } else if (sizeof(size_t) == 8) {
        return 0xc90fdaa22168c234; // floor(pi/4 * 2^64)
    } else {
        throw std::logic_error(
                &quot;sizeof(size_t) not supported. only 32 or 64 bits are supported. you can easily add the required code for other sizes.&quot;);
    }
}

inline size_t scramble(size_t v) {
    return v ^ (pi_size_t() + (v &lt;&lt; 6) + (v &gt;&gt; 2));
}

namespace std {

size_t std::hash&lt;mpz_srcptr&gt;::operator()(const mpz_srcptr x) const {
    string_view view { reinterpret_cast&lt;char*&gt;(x-&gt;_mp_d), abs(x-&gt;_mp_size)
            * sizeof(mp_limb_t) };
    size_t result = hash&lt;string_view&gt; { }(view);

    // produce different hashes for negative x
    if (x-&gt;_mp_size &lt; 0) {
        result = scramble(result);
    }

    return result;
}

size_t hash&lt;mpz_t&gt;::operator()(const mpz_t x) const {
    return hash&lt;mpz_srcptr&gt; { }(static_cast&lt;mpz_srcptr&gt;(x));
}

size_t hash&lt;mpz_class&gt;::operator()(const mpz_class &amp;x) const {
    return hash&lt;mpz_srcptr&gt; { }(x.get_mpz_t());
}

}

File main.cpp:
#include &lt;iostream&gt;
#include &lt;gmpxx.h&gt;
#include &lt;unordered_map&gt;

#include &quot;hash_mpz.h&quot;

using namespace std;

int main() {
    mpz_class a;

    mpz_ui_pow_ui(a.get_mpz_t(), 168, 16);

    cout &lt;&lt; &quot;a      : &quot; &lt;&lt; a &lt;&lt; endl;
    cout &lt;&lt; &quot;hash( a): &quot; &lt;&lt; (hash&lt;mpz_class&gt; { }(a)) &lt;&lt; endl;
    cout &lt;&lt; &quot;hash(-a): &quot; &lt;&lt; (hash&lt;mpz_class&gt; { }(-a)) &lt;&lt; endl;

    unordered_map&lt;mpz_class, int&gt; map;
    map[a] = 2;
    cout &lt;&lt; &quot;map[a] : &quot; &lt;&lt; map[a] &lt;&lt; endl;

    return 0;
}

4. Question
Is there anything which can benefit from further improvement?
Make functions that should not be exported static
Functions that should only be available locally should be marked static. This applies to pi_size_t() and scramble() in hash_mpz.cpp.
Avoid using std::endl
Use &quot;\n&quot; instead of std::endl, the latter is equivalent to &quot;\n&quot;, but also forces a flush of the output. This is rarely necessary, and might hurt performance, especially when writing to a file, or when standard output is redirected to a file.
Consider not using namespace std
It's very good that you are not using namespace std in the header files. But consider not using it at all, since even if only used in .cpp files it can result in namespace conflicts that are hard to debug. If you do find yourself typing std:: a lot and want to avoid it, consider only importing the names that you do use from std::, like so:
using std::cout;
using std::unordered_map;

Final nitpicks

There's still one unnecessary std:: inside the namespace std block in hash_mpz.cpp.
You don't need return 0 at the end of main().