U
user
Hello,
I'd like to know how to correctly declare a member function so that it takes as
parameter any function object, of which the operator() takes arguments of
specific types.
The idea is to be able to build this function object using the standard binders,
or boost's bind.
Here's an example that shows what can work with function pointers, or a
binary_function object. I feel that both of them are too restrictive and I'm
hoping to find something more generic.
Thanks!
Martin
-------
#include <boost/bind.hpp>
#include <functional>
void myLogFunction2(const int priority, const char* message);
void myLogFunction3(const int priority, const char* message, const char* message);
class AnExample
{
public:
typedef void (*LogMsgCallback)(const int priority, const char*);
// This works, but is too restrictive.
void doTheJobAndLog(LogMsgCallback logger);
// Can I write a signature that takes any function object of
// which the operator() takes two arguments
// (const int priority, const char*) ?
//doTheJobAndLog(???);
//Something like this might work for some cases, but not
//for function objects resulting from Boost's bind.
void doTheJobAndLog(binary_function<int, const char*, void> logger);
};
int main(void)
{
AnExample example;
// This is what works.
example.doTheJobAndLog(myLogFunction2);
// I'd like to call the member function in this way:
example.doTheJobAndLog(boost::bind(myLogFunction2, _1, _2));
example.doTheJobAndLog(boost::bind(myLogFunction3, _1, _2, "example"));
// Or this way: (which currently works if the method
// that takes the binary_function object is present)
example.doTheJobAndLog(ptr_fun(myLogFunction2));
return 0;
}
I'd like to know how to correctly declare a member function so that it takes as
parameter any function object, of which the operator() takes arguments of
specific types.
The idea is to be able to build this function object using the standard binders,
or boost's bind.
Here's an example that shows what can work with function pointers, or a
binary_function object. I feel that both of them are too restrictive and I'm
hoping to find something more generic.
Thanks!
Martin
-------
#include <boost/bind.hpp>
#include <functional>
void myLogFunction2(const int priority, const char* message);
void myLogFunction3(const int priority, const char* message, const char* message);
class AnExample
{
public:
typedef void (*LogMsgCallback)(const int priority, const char*);
// This works, but is too restrictive.
void doTheJobAndLog(LogMsgCallback logger);
// Can I write a signature that takes any function object of
// which the operator() takes two arguments
// (const int priority, const char*) ?
//doTheJobAndLog(???);
//Something like this might work for some cases, but not
//for function objects resulting from Boost's bind.
void doTheJobAndLog(binary_function<int, const char*, void> logger);
};
int main(void)
{
AnExample example;
// This is what works.
example.doTheJobAndLog(myLogFunction2);
// I'd like to call the member function in this way:
example.doTheJobAndLog(boost::bind(myLogFunction2, _1, _2));
example.doTheJobAndLog(boost::bind(myLogFunction3, _1, _2, "example"));
// Or this way: (which currently works if the method
// that takes the binary_function object is present)
example.doTheJobAndLog(ptr_fun(myLogFunction2));
return 0;
}