callback and boost:function

L

Lars Schouw

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
 
L

Lars Schouw

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
 
D

David Harmon

On 11 Jan 2006 00:49:43 -0800 in comp.lang.c++, "Lars Schouw"
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/
 
L

Lars Schouw

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
 
J

Jeff Flinn

Lars said:
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
 
L

Lars Schouw

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
 
B

Barbier de Reuille Pierre

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
 
L

Lars Schouw

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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top