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 8776461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:03:55+00:00 2026-06-13T19:03:55+00:00

To prepare a struct to be used in an unordered_set , a hashing function

  • 0

To prepare a struct to be used in an unordered_set, a hashing function is required. This can either be accomplished by overloading operator size_t() (ew) or annoyingly making something like this:

namespace std
{
 template<> struct hash<MyStruct> : public unary_function<MyStruct, size_t>
 {
  size_t operator()(const MyStruct& mystruct) const
  {
   return 0; //hash here
  }
 };
}

Is there any way to create an interface like so:

struct Hashable
{
 virtual size_t hash() = 0;
};

And setup std::hash to work for any of its implementations? I’m pretty sure templates don’t work that way, so that’s left me in a bind. Is there a safe size_t idiom that could work sorta like the safe bool idiom for casting to size_t? Or something else? It’s silly writing out a new std::hash specialization for every single struct when a common interface and a member function in each struct would be far more convenient.

  • 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-13T19:03:57+00:00Added an answer on June 13, 2026 at 7:03 pm

    There is actually another solution:

    template <typename T>
    struct Hashable {
        size_t operator()(T const& t) { return hash_value(t); }
    };
    
    template <typename T, typename E = std::equal<T>, typename A = std::allocator<T>>
    using MySet = std::unordered_set<T, Hashable<T>, E, A>;
    

    Now, all you have to do is defining a free-function hash_value that accepts T or T const& as argument and returns a size_t.

    EDIT: changed hash to hash_value, as it was in Boost.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I prepare the configuration settings (App.config, maybe?) I need to use Enterprise
I don't can install , this all code in console don't work compiling !!
I have the following struct which will be used to hold plugin information. I
Edit: Prepare my objects for the use within a HashMap. after reading a bit
Trying to prepare good build environment for my js library. According to reviews on
I need to prepare some input data to run through a program, the data
I am trying to prepare my activity's how to use windows but I am
How should I prepare the code if it something fails? With try-catch statement or?
I want to prepare a text for the use in a LaTeX document. I
Should I prepare a empty DB inadvance while using the SQliteOpenHelper to create db

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.