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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:24:51+00:00 2026-06-06T13:24:51+00:00

how to remove function that bound to member function of this object : std::vector<std::function<void(int)>>

  • 0

how to remove function that bound to member function of this object :

std::vector<std::function<void(int)>> callbacks;

class MyClass {
public:
    MyClass() {
        callbacks.push_back(
            std::bind(&MyClass::myFunc,this,std::placeholders::_1)
        );
    }
    ~MyClass() {
        auto it = std::remove_if( std::begin(callbacks),
                                  std::end(callbacks),
                                  [&](std::function<void(int)>& f) {
                return // <-- this is my question
                       //     true (remove) if f is bound to member function 
                       //     of this
        });
        callbacks.erase(it,std::end(callbacks));
    }
    void myFunc(int param){...}
};
  • 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-06T13:24:53+00:00Added an answer on June 6, 2026 at 1:24 pm

    You can’t in the general case without a buttload of extra work. Type erasure clears this information from the object, and std::function does not expose this information directly.

    Your specific example may only have one member function that could be the candidate to remove, but what about a class with 5 members that could be stored as callbacks? You’ll need to test for all of them, and it’s also possible to bind member functions using a lambda, which is pretty much undetectable.

    Here’s one solution if:

    • all callbacks are registered from within MyClass
    • the container is amended to store extra information
    • you’re willing to do all the extra bookkeeping
    std::vector<std::pair<std::function<void(int)>, void*>> callbacks;
    
    class MyClass{
      static unsigned const num_possible_callbacks = 2; // keep updated
      std::array<std::type_info const*, num_possible_callbacks> _infos;
      unsigned _next_info;
    
      // adds type_info and passes through
      template<class T>
      T const& add_info(T const& bound){
        if(_next_info == num_possible_callbacks)
          throw "oh shi...!"; // something went out of sync
        _infos[_next_info++] = &typeid(T);
        return bound;
      }
    public:
      MyClass() : _next_info(0){
        using std::placeholders::_1;
        callbacks.push_back(std::make_pair(
            add_info(std::bind(&MyClass::myFunc, this, _1)),
            (void*)this));
        callbacks.push_back(std::make_pair(
            add_info([this](int i){ return myOtherFunc(i, 0.5); }),
            (void*)this));
      }
    
      ~MyClass(){
        using std::placeholders::_1;
    
        callbacks.erase(std::remove_if(callbacks.begin(), callbacks.end(),
            [&](std::pair<std::function<void(int)>, void*> const& p) -> bool{
              if(p.second != (void*)this)
                return false;
              auto const& f = p.first;
              for(unsigned i = 0; i < _infos.size(); ++i)
                if(_infos[i] == &f.target_type())
                  return true;
              return false;
            }), callbacks.end());
      }
    
      void myFunc(int param){ /* ... */ }
      void myOtherFunc(int param1, double param2){ /* ... */ }
    };
    

    Live example on Ideone.

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

Sidebar

Related Questions

Consider the following class: class Something : ISomething { public void DoesSomething(int x) {
I have the following function that I am using to remove the characters \04
I'm looking for a small function that allows me to remove the extension from
http://php.net/manual/en/function.trim.php The manual entry does say that I can remove any specified leading or
I created this example The remove function doesn't work, and I can't figure out
Is there php function to remove the space inside the string? for example: $abcd=this
I'm trying to remove the eval statement in this function. I'm used to the
I found this vector template class implementation, but it doesn't compile on XCode. Header
Using boost-bind , the resulting boost-function may receive more arguments than the bound object
I have html that looks something like this: <div> <table> <tr onclick=show();> <td class=cell>Click

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.