473,508 Members | 4,179 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1779
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
4443
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
14908
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
1784
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
2392
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
4272
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
2480
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
9246
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
11050
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
1824
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
1617
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
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7332
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,...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
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
4715
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...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.