By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Function objects as parameters.

P: n/a

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

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Martin Proulx wrote:

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(???);
template <typename T>
void doTheJobAndLog(const T& func);
//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.
};

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


--
Windows 2000: Designed for the Internet...
The Internet: Designed for UNIX.
-- from a Slashdot post

Jul 22 '05 #2

P: n/a
"Martin Proulx" <mp*****@okiok.com> wrote in message
news:Ps*******************@news20.bellglobal.com.. .

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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3

P: n/a

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:KA***************@twister.nyroc.rr.com...
| "Martin Proulx" <mp*****@okiok.com> wrote in message
| news:Ps*******************@news20.bellglobal.com.. .

[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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.