473,414 Members | 1,626 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Function objects as parameters.


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
3 1769
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
"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fran?ois Bourdages | last post by:
Hi is there a way to know if object (view, function, etc) are invalid ? let say a have a table t1 (field col1, col2) and a view v1 (field t1.col1, t1.col2) if I drop t1.col2, the view v1 is not...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
6
by: simon | last post by:
Always when I need data reader in my programs, I simply have functions, which creates it for me: Dim rdr As SqlDataReader dim sql as string sql="myStoredProcedure" rdr =...
1
by: BobRoyAce | last post by:
I have a class that has several Subs that do DB things, some of which require the same set of parameters to be passed to a stored procedure. One class has 12 parameters and part of code used to...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
3
pbmods
by: pbmods | last post by:
AN INTRODUCTION TO FUNCTION OBJECTS LEVEL: INTERMEDIATE PREREQS: OBJECTS You've seen it before. You're setting up an XMLHttpRequest call, and you need to execute a function when it returns, so...
1
by: bharathv6 | last post by:
i need to do is modify the image in memory like resizing the image in memory etc ... with out saving it disk as i have to return back the image with out saving it disk PIL supports the use of...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
14
by: mesut | last post by:
hi colleagues, I don't know if this is the right group for but it's in C# so I try. I have a #3 procedural function called GetInfo.. and those are 3 overloaded methods. I would like to use the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.