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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:34:24+00:00 2026-06-15T08:34:24+00:00

I have a set like this: set<weak_ptr<Node>, owner_less<weak_ptr<Node> > > setName; It works fine.

  • 0

I have a set like this: set<weak_ptr<Node>, owner_less<weak_ptr<Node> > > setName;

It works fine. But I would like to change it to an unordered set. However, I get about six pages of errors when I do that. Any ideas how to do that?

After looking through all the pages of error messages I found to lines that might help.

/usr/include/c++/4.7/bits/functional_hash.h:60:7: error: static assertion failed: std::hash is not specialized for this type
/usr/include/c++/4.7/bits/stl_function.h: In instantiation of ‘bool std::equal_to<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::weak_ptr<Node>]’:
  • 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-15T08:34:26+00:00Added an answer on June 15, 2026 at 8:34 am

    Please read Richard Hodges answer below as mine is incorrect, despite being the accepted solution.


    Since unordered_sets are hash-based you have to provide a hash function object for the std::weak_ptr data-type.

    If you take a look at the unordered_set template-parameters

    template<class Key,
        class Hash = std::hash<Key>,
        class Pred = std::equal_to<Key>,
        class Alloc = std::allocator<Key> >
        class unordered_set;
    

    you’ll notice that std::unordered_set provides you with a default std::hash<> template parameter. But since std::hash does only provide specializations for a specific set of data types, you might have to provide your own.

    The error-message you quoted tells you, that no std::hash<> specialization for std::weak_ptr<> exists, so you have to provide your own hashing function for that:

    template<typename T>
    struct MyWeakPtrHash : public std::unary_function<std::weak_ptr<T>, size_t> {
       size_t operator()(const std::weak_ptr<T>& wp)
       {
          // Example hash. Beware: As zneak remarked in the comments* to this post,
          // it is very possible that this may lead to undefined behaviour
          // since the hash of a key is assumed to be constant, but will change
          // when the weak_ptr expires
          auto sp = wp.lock();
          return std::hash<decltype(sp)>()(sp);
       }
    };
    

    Edit:
    You also need to provide an equality function, since no std::equal_to for weak_ptr is provided.
    Taking a possible way to do this from "Equality-compare std::weak_ptr" on Stackoverflow:

    template<typename T>
    struct MyWeakPtrEqual : public std::unary_function<std::weak_ptr<T>, bool> {
    
       bool operator()(const std::weak_ptr<T>& left, const std::weak_ptr<T>& right)
       {
          return !left.owner_before(right) && !right.owner_before(left);
       }
    };
    

    All combined this gives us the following:

    std::unordered_set<std::weak_ptr<T>,
                       MyWeakPtrHash<T>,
                       MyWeakPtrEqual<T>> wpSet;
    

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

Sidebar

Related Questions

I dont what is going on but I have this menu set like this
OK I have backgrounds set up like this: HTML <div id=container> <div id=content> CONTENT
I have click events set up like this: $('.dialogLink') .click(function () { dialog(this); return
I have 2 tables set up like this (irrelevant rows omitted): > product -
I have a set of NSTimeIntervals like this: NSArray *mySet = [NSArray arrayWithObjects: [NSNumber
I have set up a screen rc file like this: log on shelltitle apt
I have a query that creates a result set like this: Rank Name 1
I have set up a NInject (using version 1.5) binding like this: Bind<ISessionFactory>().ToMethod<ISessionFactory>(ctx =>
I have set up a php script to receive url data like this: http://giffgaff.liamwli.co.uk/?name=liamwli
I have this set of labels and radio buttons that look like this: Entertainment:

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.