Function objects as parameters.

M

Martin Proulx

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

Rolf Magnus

Martin said:
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.

Make it a template.
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);

That would only work if binary_function had a virtual operator(), so
that the system could at runtime decide which function to call.
 
C

Cy Edmunds

Martin Proulx said:
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

[snip]

If you want the full genericity provided by functors you must write template
functions and/or classes and give up on separate compilation. In other
words, in the header file:

template <typename LOGGER>
void doTheJobAndLog(LOGGER logger)
{
// function defined here
}

Note that there is no issue about how to declare LOGGER. With templates the
rule is: if it works, it's right; otherwise it's wrong.
 
C

Chris \( Val \)

|
[snip]

| Note that there is no issue about how to declare LOGGER. With templates the
| rule is: if it works, it's right; otherwise it's wrong.

Nice rule :).

Cheers.
Chris Val
 

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,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top