468,525 Members | 2,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,525 developers. It's quick & easy.

callback and boost:function

Guys,

Is there anyway in C++ to setup a callback for a member function that
takes two arguments
I am trying to use boost::function but that only works with
std::bind1st that does only take one parameter.

My function I want to call back looks like this:
struct X {
double f_cos(double x , void * p);
};

Regards
Lars

Jan 11 '06 #1
8 10578
To get ride of the void* I would like to use the boost::lambda library
so I can do _1, _2 as arguments.
But this does also not seems to work with more than one argument.
Lars

Jan 11 '06 #2
I will have a look at the sigc++ library for now as a workaround until
someone comes up with a better solution.
http://libsigc.sourceforge.net
Lars

Jan 11 '06 #3
On 11 Jan 2006 00:49:43 -0800 in comp.lang.c++, "Lars Schouw"
<sc******@yahoo.com> wrote,
Is there anyway in C++ to setup a callback for a member function that
takes two arguments


Why are you trying to setup a callback as a member function?

I suspect you need to read topic "[33.2] How do I pass a
pointer-to-member-function to a signal handler, X event callback,
system call that starts a thread/task, etc?" in Marshall Cline's C++
FAQ. It is always good to check the FAQ before posting. You can
get the FAQ at:
http://www.parashift.com/c++-faq-lite/
Jan 12 '06 #4
David

I already had a look at the FAQ last time I looked into this problem
one year ago.

The FAQ does not describe how to extended it with generic lambda code.
This is an extra plus I would like to be able to have if possible.

Last time I looked at it I got it up and running as described in the
FAQ I will see if I can get it running again.
boost::function is more elegant so it would be nice if I would be able
to make that work somehow.
I am stuck in how to do that at the moment.

Lars

Jan 12 '06 #5
Lars Schouw wrote:
Guys,

Is there anyway in C++ to setup a callback for a member function that
takes two arguments
I am trying to use boost::function but that only works with
std::bind1st that does only take one parameter.
I don't know how you came to this conclusion, but this is false.
My function I want to call back looks like this:
struct X {
double f_cos(double x , void * p);
};


Assuming you want to supply an X pointer and both args you could:

typedef boost::function< double, X*, double, void* > tFnc;

tFnc lFnc = boost::bind( &X::f_cos, _1, _2, _3 );

then call via

lFnc( &x, somedbl, someptr );

Jeff Flinn
Jan 12 '06 #6
typedef boost::function< double, X*, double, void* > tFnc;

give me:
c:\sletmig\templatedcallback\test.cpp(18) : error C2977:
'boost::function' : too many template arguments
c:\dev\boost\boost_1_33_1\boost\function\function_ base.hpp(92)
: see declaration of 'boost::function'

when I compile.... any idea?

Lars

Jan 13 '06 #7
On 12 Jan 2006 16:46:45 -0800
"Lars Schouw" <sc******@yahoo.com> wrote:
typedef boost::function< double, X*, double, void* > tFnc;

give me:
c:\sletmig\templatedcallback\test.cpp(18) : error C2977:
'boost::function' : too many template arguments
c:\dev\boost\boost_1_33_1\boost\function\function_ base.hpp(92)
: see declaration of 'boost::function'

when I compile.... any idea?

Lars


That's because it's a mix between two notations. Thus you write either :

typedef boost::function< double (X*, double, void*) > tFnc;

but it does not work with all compiler (look at the boost::function
websites to know which compilers ...), or you use :

typedef boost::function3<double, X*, double, void*> tFct;

Now, if you want to store the function just use :

tFct fct = &X::f_cos; // No need to use bind !

However, if you want to use a zero argument function as a callback,
use :

boost::function<double> fct = boost::bind(&X::f_cox, x, somedbl,
someptr);

Then call :

double d = fct();

Also, you can bind part of the arguments using _1, _2, ... and the
correct function definition.

Pierre

--
You will have good luck and overcome many hardships.
Jan 13 '06 #8
Pierre

Is it possible to use Lambda to make struct X generic?
aka.
typedef boost::function2<double, GENERIC_TYPE*, double> tFct;

so that I don't have to know the specification of struct X?

Regards
Lars Schouw

Jan 24 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Marian Aldenhövel | last post: by
3 posts views Thread by ThinkRS232 | last post: by
7 posts views Thread by Kirk McDonald | last post: by
1 post views Thread by Noah Roberts | last post: by
2 posts views Thread by Pradeep | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.