Thread: std::collate::hash historical question

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    std::collate::hash historical question

    I'm working with some C++14 code. A library we're using has its own partial port of C++17's std::string_view, but without a corresponding specialization of std::hash.

    I assumed that the standard contained no hash function that I could use, aside from std::hash<std::string>, but it turns out I'm wrong.

    There is a hash function buried in iostream's locales library: std::collate<CharT>::hash, std::collate<CharT>::do_hash - cppreference.com

    The cppreference page mentions its use by std::regex, so I was even more surprised to find that std::collate::hash is defined in C++98, which predates std::hash and std::regex.

    My question, for the language lawyers among you, is why was std::collate::hash included in the C++98 standard?
    Last edited by CodeMonkey; 12-27-2021 at 04:57 PM. Reason: "facets" -> "locales"
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    The cppreference page mentions its use by std::regex
    You're refering to this page, which is not the one you link. Note that it says the facet is used for comparison as well as hashing. regex is presumably using the locale-aware comparisons.

    why was std::collate::hash included in the C++98 standard?
    Why not?

    Here's the gcc implementation (somewhat simplified by me, mostly just removing leading underscores) original:
    Code:
    template<typename CharT>
    long
    collate<CharT>::do_hash(const CharT* lo, const CharT* hi) const
    {
        unsigned long val = 0;
        for ( ; lo < hi; ++lo)
            val = *lo + ((val << 7) | (val >> (sizeof(long) * CHAR_BIT - 7)));
        return static_cast<long>(val);
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Neat, nice find.

    > Why not?

    Sure, why not, but I would assume that the hashing interface was added for use elsewhere in the standard library. Where, though? In C++98, there was no regex, unordered_map, etc. I also can't find an analogous facility in the C locale library.

    It's mysterious to me.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What sort of code would historical figures write?
    By Alpo in forum General Discussions
    Replies: 10
    Last Post: 06-02-2014, 10:26 AM
  2. Hash Table resizing question
    By gibson221 in forum C Programming
    Replies: 1
    Last Post: 10-22-2012, 02:30 PM
  3. Question about Hash (once more)
    By audinue in forum Tech Board
    Replies: 6
    Last Post: 01-13-2009, 04:58 AM
  4. quick hash table question
    By Brian in forum C Programming
    Replies: 7
    Last Post: 07-30-2005, 07:22 PM
  5. Hash Function question
    By IfYouSaySo in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2005, 12:32 AM

Tags for this Thread