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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:12:00+00:00 2026-06-01T15:12:00+00:00

To narrow it down: I’m currently using Boost.Unordered . I see two possible solutions:

  • 0

To narrow it down: I’m currently using Boost.Unordered. I see two possible solutions:

  1. Define my own Equality Predicates and Hash Functions and to utilize templates (maybe is_pointer) to distinct between pointers and instances;

  2. Simply to extend boost::hash by providing hash_value(Type* const& x) as for hashing; and add == operator overload as free function with (Type* const& x, Type* const& y) parameters as for equality checking.

I’m not sure whether both variations are actually possible, since I didn’t test them. I would like to find out you handle this problem. Implementations are welcome 🙂

EDIT 1:
What about this?

template<class T>
struct Equals: std::binary_function<T, T, bool> {
    bool operator()(T const& left, T const& right) const {
        return left == right;
    }
};

template<class T>
struct Equals<T*> : std::binary_function<T*, T*, bool> {
    bool operator()(T* const& left, T* const& right) const {
        return *left == *right;
    }
};

EDIT 2:

I’ve just defined:

friend std::size_t hash_value(Base const& base) {
    boost::hash<std::string> hash;

    return hash(base.string_);
}

friend std::size_t hash_value(Base* const& base) {
    return hash_value(*base);
}

And then:

Derived d1("x");
Derived d2("x");

unordered_set<Base*> set;

set.insert(&d1);

assert(set.find(&d2) == end());

Debugger says that friend std::size_t hash_value(Base* const& base) is never called (GCC 4.7). Why is that?

EDIT 3:
I found out that template <class T> std::size_t hash_value(T* const& v) in boost/functional/hash.hpp on line #215 (Boost 1.49) is Boost’s specialization for pointers and it simply masks your custom implementation of hash_value such as mine in EDIT 2.
Therefore, it seems like the only way here is to create a custom Hash Functor.

  • 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-01T15:12:01+00:00Added an answer on June 1, 2026 at 3:12 pm

    For the hash function, you have a choice between specializing boost::hash (or std::hash in the newer standard) or defining a new functor class. These alternatives work equally well.

    For the equality operator, you need to define a new functor, because you cannot redefine the equality operator over pointers. It’s a built-in operator (defined in functional terms as bool operator==( T const *x, T const *y )) and cannot be replaced.

    Both of these can be defined generically by using a templated operator() in a non-templated class.

    struct indirect_equal {
        template< typename X, typename Y >
        bool operator() ( X const &lhs, Y const &rhs )
            { return * lhs == * rhs; }
    };
    

    Follow a similar pattern for the hasher.

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

Sidebar

Related Questions

I want to narrow down a collection using params accessible through association class PostsController
Using Maxmind's GeoIP software, we can narrow down the LONG/LAT of an IP address
I am trying to narrow down why my JComponent is not redrawing. It is
I've tried to write the smallest chunk of code to narrow down a problem.
Okay, it took me a little while to narrow down this problem, but it
UPDATE II: OK, I managed to narrow it down a little. I have a
We've had this question already, but I want to narrow it down to already
i have a bug i'm trying to narrow down and it's proving to be
I'm trying to use jquery to narrow down my selections of towns when the
I have read through this which helped me narrow down my validation problems to

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.