How to mix boost::bind with C function pointers to implement callbacks?

How to mix boost::bind with C function pointers to implement callbacks?



#include <iostream>
#include <boost/bind.hpp>

using namespace std;

class Foo {
public:
  Foo(const string &prefix) : prefix_(prefix) {}
  void bar(const string &message)
  {
    cout << prefix_ << message << endl;
  }
private:
  const string &prefix_;
};

static void
runit(void (*torun)(const string &message), const string &message)
{
  torun(message);
}

int
main(int argc, const char *argv[])
{
  Foo foo("Hello ");
  runit(boost::bind<void>(&Foo::bar, boost::ref(foo), _1), "World!");
}

template<typename ToRunT>
void runit(ToRunT const& torun, std::string const& message)
{
    torun(message);
}

static void runit(boost::function<void(std::string const&)> const& torun,
                  std::string const& message)
{
    torun(message);
}

template<typename function_ptr>
void runit(function_ptr torun, const string &message)
{
  torun(message);
}

runit(boost::bind<void>(&Foo::bar, boost::ref(foo)), std::string("World!"));


Learn More :