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

  • Home
  • SEARCH
  • 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 328769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:31:34+00:00 2026-05-12T09:31:34+00:00

How can i hash (std::tr1::hash or boost::hash) a c++ pointer-to-member-function? Example: I have several

  • 0

How can i hash (std::tr1::hash or boost::hash) a c++ pointer-to-member-function?

Example:

I have several bool (Class::*functionPointer)() (not static) that point to several diferent methods of the class Class and i need to hash those pointer-to-member-function.

How can i do that?

Also how can i compare (std::less) those member function pointers so i can store them in a std::set?

  • 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-05-12T09:31:34+00:00Added an answer on May 12, 2026 at 9:31 am

    All C++ objects, including pointers to member functions, are represented in memory as an array of chars. So you could try:

    bool (Class::*fn_ptr)() = &Class::whatever;
    const char *ptrptr = static_cast<const char*>(static_cast<const void*>(&fn_ptr));
    

    Now treat ptrptr as pointing to an array of (sizeof(bool (Class::*)())) bytes, and hash or compare those bytes. You can use unsigned char instead of char if you prefer.

    This guarantees no false positives – in C++03, pointers to member functions are POD, which means among other things that they can be copied using memcpy. This implies that if have the same byte-for-byte values, then they are the same.

    The problem is that the storage representation of member function pointers could include bits which do not participate in the value – so they will not necessarily be the same for different pointers to the same member function. Or the compiler might, for some obscure reason, have more than one way of pointing to the same function of the same class, which are not byte-wise equal. Either way you can get false negatives. You’ll have to look into how member function pointers actually work on your implementation. It must implement operator== for member function pointers somehow, and if you can find out how then you can probably figure out an order and a hash function.

    That’s potentially hard: member function pointers are awkward, and the storage is likely to include different amounts of non-participating “slack space” according to what kind of function is pointed to (virtual, inherited). So you’ll probably have to interact quite significantly with your compiler’s implementation details. This article might help get you started: http://www.codeproject.com/KB/cpp/FastDelegate.aspx

    A cleaner alternative might be to do a linear search through an array in order to “canonicalise” all your function pointers, then compare and hash based on the position of the “canonical” instance of that function pointer in your array. Depends what your performance requirements are. And even if there are requirements, does the class (and its derived classes) have so many functions that the linear search will take that long?

    typedef bool (Class::*func)();
    vector<func> canon;
    
    size_t getIndexOf(func fn_ptr) {
        vector<func>::iterator it = find(canon.begin(), canon.end(), fn_ptr);
        if (it != canon.end()) return it - canon.begin();
        canon.push_back(func);
        return canon.size() - 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 208k
  • Answers 208k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Please refer to this http://myjavaroom.blogspot.com/2009/11/cas-single-sign-on-and-jboss-seam.html May 12, 2026 at 9:35 pm
  • Editorial Team
    Editorial Team added an answer I got it, somehow the sound file suffix is in… May 12, 2026 at 9:35 pm
  • Editorial Team
    Editorial Team added an answer This is what I believe your classes should look like:… May 12, 2026 at 9:35 pm

Related Questions

How do I allow the STL implementation to pick up my custom types? On
CString is quite handy, while std::string is more compatible with STL container. I am
I have a list of about a hundreds unique strings in C++, I need
I have a website that requires a user to authenticate themselves with a user

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.