• std::hash: myth or =?UTF-8?Q?reality=3F?=

    From Sam@21:1/5 to All on Sat Nov 13 15:30:01 2021
    This is a MIME GnuPG-signed message. If you see this text, it means that
    your E-mail or Usenet software does not support MIME signed messages.
    The Internet standard for MIME PGP messages, RFC 2015, was published in 1996. To open this message correctly you will need to install E-mail or Usenet software that supports modern Internet standards.

    I had to do the following to be able to use string views to poke at
    unordered maps with strings for their keys.

    I was expecting to simply be able to use std::hash<void>, but gcc 11.2 was
    not happy. And I find no evidence of std::hash<void>'s existence on cppreference.com.

    So, does it exist but not documented, and not implemented in gcc, or is this
    a rather glaring hole in the standard.

    If I can have std::equal_to<void>, I see no reason not to have
    std::hash<void>, instead of resorting to such hackery:

    #include <unordered_map>
    #include <string>
    #include <string_view>
    #include <utility>
    #include <iostream>

    struct transparent_hash {

    template<typename T,
    typename=std::void_t<decltype(
    std::hash<std::remove_cvref_t<T>>{}(
    std::declval<std::remove_cvref_t<T> &>()
    ))>>
    auto operator()(T && t) const
    {
    std::hash<std::remove_cvref_t<T>> h;

    return h(std::forward<T>(t));
    }

    typedef void is_transparent;
    };

    std::unordered_map<std::string, int,
    transparent_hash,
    std::equal_to<void>> lookup;

    auto find(const std::string_view &f)
    {
    return lookup.find(f);
    }

    int main()
    {
    lookup[std::string{"a"}]=4;

    if (find("a")->second == 4)
    {
    std::cout << "It works" << std::endl;
    }

    return 0;
    }


    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCAAdFiEEMWrVnbBKLOeG9ifkazpiviedvyUFAmGQIEoACgkQazpivied vyWtzw/8CHNDzeFuB2bHskF4WQBjMDgaphta2ZiWjCAfQsooaEj0yMcUzDa8e2wm staBz3f8cSNZLCx8Y4/SITcdANDQzUQJ9bhnmJdDT13JVOODnn3IUyuIFka7Lsqk cbGA3Du/BeGJO7XNSDKbVGLCeZN2NJ0PceTU/U274ETSX58drrNOg3/e+2BS2B1H rLlm0bgLQxBFl53W9qhzdEKgeE8JK10XTTW4muxmFcZRNHccpombmPtOQFJWe40A mYrzPvjBDcHu8FH6AfUbFXkBgggN8r80Q/LWUxLZr7pfprSvcB6TK4l4uWuOG7u2 uq+kNARK5OYv6uRAAh3/iWPdW6fQNNKtfogL3+LcOalMAHQzIx4TfwjUR/Yo4r+y dy0l7jwIxAhax/dtm1HbPwii+uW1xYoTYqEg1eQNGGYBU4r0Lj5CrZOKcAea2CNs 3sheFu7pmFi1bL94SlufUSXwR633QQ8S7K1sX9mHOmJ80IuhcViQ50Kvfr1x1UF+ bkqCBoSlu+xdeMdy9B8Zud4J9yY5yDtvB1LQJs1QFfe1Nkm6KJ3lj3XynijD9OX0 gEz3sbyoEHSKRtZMdYvE2GUZwkY5qECVeOEhXwpHJBssDHZHlvY2NEOAAjf2pwbB w7nESHtAMamdU6g9CN7AEkw1HZw5FTKMqCmSUiGwxJTfjm44gHM=
    =SChU
    -----END PGP SIGNATURE-----

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bo Persson@21:1/5 to Sam on Sun Nov 14 09:02:45 2021
    On 2021-11-13 at 21:30, Sam wrote:
    I had to do the following to be able to use string views to poke at
    unordered maps with strings for their keys.

    I was expecting to simply be able to use std::hash<void>, but gcc 11.2
    was not happy. And I find no evidence of std::hash<void>'s existence on cppreference.com.

    So, does it exist but not documented, and not implemented in gcc, or is
    this a rather glaring hole in the standard.

    If I can have std::equal_to<void>, I see no reason not to have std::hash<void>, instead of resorting to such hackery:


    The usual reason for something *not* being in the standard is that
    nobody has written a paper to propose it. The standard doesn't write itself.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)