Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:27:56+00:00 2026-06-18T06:27:56+00:00

It’s unclear to me from the C++11 standard where user-defined hash<T> functors should be

  • 0

It’s unclear to me from the C++11 standard where user-defined hash<T> functors should be defined.

For example, in 23.5.2 Header <unordered_map>, it shows:

template <class Key,
        class T,
        class Hash = hash<Key>,
        class Pred = std::equal_to<Key>,
        class Alloc = std::allocator<std::pair<const Key, T> > >
    class unordered_map;

This suggests that, by default, hash<T> is searched for in the global namespace, whereas equal_to<> is searched for in the std namespace.

Why the difference in namespace between hash<> and equal_to<>?

(Actually, in the description at http://www.cplusplus.com/reference/unordered_map/unordered_map/, neither specifies the std namespace.)

Thus, when defining a hash<> functor for a user type, should we enclose it within a namespace std { } block, or can it remain in the current namespace?

If the code does not have a using namespace std;, how do the STL containers like unordered_map know to look in the std namespace for the predefined hash<> functors associated with the primitive types? It seems like the default Hash = hash<Key> would fail to find these.

Sorry if these are stupid questions..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:27:57+00:00Added an answer on June 18, 2026 at 6:27 am

    First of all, there is no “argument dependent lookup” for templates. So hash<Key> will always refer to the same template, either in std or in global namespace, independent of Key. If it had resolved to different templates in different translation units, it would cause undefined behavior by ODR violation. This alone suggests that hash here means std::hash, that is as-if unordered_map was declared like this:

    namespace std {
        template<class T> struct hash;
    
        template <class Key,
            class T,
            class Hash = hash<Key>,    // resolves to std::hash<Key> for all Keys
            class Pred = std::equal_to<Key>,
            class Alloc = std::allocator<std::pair<const Key, T> > >
        class unordered_map;
    }
    

    However, the types declared in the standard headers are not required to be written in source (they could be in principle built-in to the compiler or pre-compiled by some other magic). The standard requires each standard header to declare only the types in its synopsis, which means that by omitting the std::hash declaration the standard permits some hypothetical implementation to avoid the above namespace pollution. This explains why you do not see the above declaration in the synopsis.

    To further back-up the above conclusion, we go to §20.8.12 Class template hash [unord.hash] that reads:

    The unordered associative containers defined in 23.5 use specializations of the class template hash as the default hash function.

    This paragraph refers to the std::hash, which we can infer from the synopsis of <functional>.

    Bottom line: This is an inconsistency in the standard formatting. There are plenty of inconsistencies, so this specific case is not surprising at all. In such cases one has to understand what has been intended by deducing what is the only sensible thing.

    Specialization. You specialize templates in the namespace that they were declared. You are explicitly granted the right to specialize standard templates for your own type:

    namespace std {
        template<> struct hash<YourClass> {
            // specialization goes here
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.