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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:45:32+00:00 2026-05-18T11:45:32+00:00

I’ve got a problem with using boost::bind and boost::function and passing boost::function as a

  • 0

I’ve got a problem with using boost::bind and boost::function and passing boost::function as a callback into another class.

Here’s an example that is the problematic situation:

typedef boost::function<void (bool)> callbackFunction;

class HasCallback
{
public:
 HasCallback() : value(0)
 {
 }

 int value;

 void CallBackFunction(bool changed)
 {
  std::cout << "HasCallback class. CallBackFunction called. Parameter: " << value << std::endl;
 }
};

class ReceivesCallback 
{

public:
 void AddCallback(callbackFunction newFunc)
 {
  callbacks.push_back(newFunc);
 }

 void execute(int &i)
 {
  for(std::vector<callbackFunction>::iterator it = callbacks.begin(); it != callbacks.end(); it++)
  {
   (*it)(i++);
  }
 }

 void RemoveHandler(callbackFunction oldFunc)
 {
  for(std::vector<callbackFunction>::iterator it = callbacks.begin(); it != callbacks.end(); it++)
  {
   if((*it) == oldFunc)
   {
    callbacks.erase(it);
    break;
   }
  }

 }

private:
 std::vector<callbackFunction> callbacks;
};


int main()
{
 HasCallback hc;
 ReceivesCallback rc;
 rc.AddCallback(boost::bind(&HasCallback::CallBackFunction, &hc, _1));
 hc.value = 123;



 HasCallback hc2;
 rc.AddCallback(boost::bind(&HasCallback::CallBackFunction, &hc2, _1));
 hc2.value = 321;

 int a = 0;

 rc.RemoveHandler(boost::bind(&HasCallback::CallBackFunction, &hc2, _1));
 rc.execute(a);
}

The problem I’m having is that this doesn’t even compile. It fails within ReceivesCallback::RemoveHandler in the if((*it) == oldFunc) line with the error saying that there’s more than one overload of the operator== for the thing i’m trying to do.
I keep searching for this and can’t find what I’m doing wrong. Also, I keep finding contradicting information, one saying that it’s possible to compare boost::function-s and another saying it’s not. I can see the operator== functions within boost/function_base.hpp and i believe this is supposed to work, I just can’t seem to figure out how. Can someone help me out here? My suspicion is that it fails because the parameters of the boost::bind need to be specified fully(be concrete values) but this is something i cannot get in the code I’m developing, I just need to know whether the passed handler is registered or not, since I’m binding to an object it should have all the information neeeded to make the distinction.

  • 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-18T11:45:32+00:00Added an answer on May 18, 2026 at 11:45 am

    See Boost.Function FAQ for an explanation : Why can’t I compare boost::function objects with operator== or operator!= ?.

    Boost.Functions only provides comparison of a boost::function with an arbitrary function object. I believe that making your RemoveHandler member function template could fix the issue :

     template<class Functor>
     void RemoveHandler(const Functor &oldFunc)
     {
         for(std::vector<callbackFunction>::iterator it = callbacks.begin(); it != callbacks.end(); it++)
         {
             if((*it) == oldFunc)
             {
                 callbacks.erase(it);
                 break;
             }
         }
     }
    

    Here, oldFunc gets to keep its actual type without being ‘wrapped’ in a boost::function.

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

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have just tried to save a simple *.rtf file with some websites and

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.