Function objects as parameters.

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,151
Messages
2,570,854
Members
47,394
Latest member
Olekdev

Latest Threads

Top