Search code examples
boostboost-bindboost-function

boost bind to a data member callback behavior


Can someone please explain this piece of code?

struct Class {
    boost::function<void()> member;
};
Class c;

boost::function<boost::function<void()>()> foo = boost::bind(&Class::member, &c);
boost::function<void()> bar = boost::bind(&Class::member, &c);

Why does the definition of bar compile and what is the result of it?

Edit: foo() works as expected, calling c.member(), but bar() doesn't.


Solution

  • The first call is used to "generate" an extractor functor. That functor, when called, will return the member that it was bound to.

    The second call just hides the return type of the functor that is passed in (which is the same as in the first example). So essentially, calling bar will do nothing.