boost::function Usage Question
Consider the following code: class Publisher { typedef boost::function<void> Function; public: void SetChecker(const Function& Callback){ } }; class Manager { public: Manager(); private: void Check() {} }; Manager::Manager() { Publisher d; d.SetChecker(std::mem_fun(&Manager::Check)); } How can I get this to work? I always get an error when compiling the last line stating "..boost\function\function_template.hpp(127) : error C2660: '()' : Funktion akzeptiert keine 0 Parameter" Ah, the compiler is VisC++ 6. Thanks for any help Ali
"Albrecht Fritzsche"
Consider the following code:
class Publisher { typedef boost::function<void> Function; public: void SetChecker(const Function& Callback){ } };
class Manager { public: Manager(); private: void Check() {} };
Manager::Manager() { Publisher d; d.SetChecker(std::mem_fun(&Manager::Check));
try: d.SetChecker(boost::bind(&Manager::Check,this));
}
Edward Diener wrote:
class Publisher { typedef boost::function<void> Function; public: void SetChecker(const Function& Callback){ } void Publish(){} };
class Manager { public: Manager(); private: void Check() {} };
Manager::Manager() { Publisher d; d.SetChecker(std::mem_fun(&Manager::Check));
try: d.SetChecker(boost::bind(&Manager::Check,this));
Brilliant - thanks! Would you mind giving me a short
rationale about this, since I am a newbie regarding
function pointers and function (object?) adaptors.
SO, if using an stl algorithm like
vector<Publisher> p;
for_each(p.begin(), p.end(), mem_fun(&Publisher::Publish));
I don't have to bind anything, do I? mem_fun() returns a
mem_fun_t functor object, which will receive the Publisher
object pointer through each intern for_each call?
And that object pointer passing was missing in my code and
was achieved in your code through the binding? I had already
looked at boost::bind but still cannot figure out its interface
template
"Albrecht Fritzsche"
Edward Diener wrote:
class Publisher { typedef boost::function<void> Function; public: void SetChecker(const Function& Callback){ } void Publish(){} };
class Manager { public: Manager(); private: void Check() {} };
Manager::Manager() { Publisher d; d.SetChecker(std::mem_fun(&Manager::Check));
try: d.SetChecker(boost::bind(&Manager::Check,this));
Brilliant - thanks! Would you mind giving me a short rationale about this, since I am a newbie regarding function pointers and function (object?) adaptors.
SO, if using an stl algorithm like
vector<Publisher> p; for_each(p.begin(), p.end(), mem_fun(&Publisher::Publish));
I don't have to bind anything, do I? mem_fun() returns a mem_fun_t functor object, which will receive the Publisher object pointer through each intern for_each call?
The function adaptor which mem_fun becomes takes a pointer or reference to its type as its single argument in order to call the member function. The for_each algorithm passes such a reference to each type in its internal loop, so everything is in sync.
And that object pointer passing was missing in my code and was achieved in your code through the binding?
In the boost::bind case, for a member function, you need to bind in a pointer to the actual object, which passing the "this" parameter does effectively. You don't need mem_fun with boost::bind, ie. d.SetChecker(boost::bind(std::mem_fun(&Manager::Check),this)); since boost::bind is smart and automatically knows how to bind member functions.
I had already looked at boost::bind but still cannot figure out its interface
template
unspecified-3-1 bind(F f, A1 a1);
I have already mentioned to Peter Dimov that the doc for boost::bind might be updated to be more easily understandable. The boost::bind syntax creates a function adaptor, which is what boost::function wants. It is a much smarter version of the C++ standard library's std::bind1st and std::bind2nd with, more or less, unlimited parameters and types. I suggest you first look at the standard library versions and then re-read boost::bind documentation.
Edward Diener wrote:
In the boost::bind case, for a member function, you need to bind in a pointer to the actual object, which passing the "this" parameter does effectively.
I see :-)
I suggest you first look at the standard library versions and then re-read boost::bind documentation.
There I go... Thanks Ali
From: "Albrecht Fritzsche"
And that object pointer passing was missing in my code and was achieved in your code through the binding? I had already looked at boost::bind but still cannot figure out its interface
template
unspecified-3-1 bind(F f, A1 a1); What is this returning?
Following the boost::bind reference is not an easy task. :-) First, you need
template
Peter Dimov wrote:
template
unspecified-3-1 bind(F f, A1 a1); First, you need template
unspecified-5 bind(R (T::*f) (), A1 a1)
Maybe I should consider renaming my types as well, I like that idea :-)
boost::bind(&Manager::Check, this)
returns a function object that, when called, returns
boost::mem_fn(&Manager::Check)(this)
that, in turn (see the mem_fn documentation) is equivalent to
(this->*&Manager::Check)();
The thing to remember is that member functions have an additional implicit argument, the object pointer/reference.
And exactly that was already the first thing I was missing. Since compilers (when hitting such errors) are leading you to far far away code I was kinda lost - thanks for the explanation Ali
participants (3)
-
Albrecht Fritzsche
-
Edward Diener
-
Peter Dimov