
January 11th, 2006, 09:05 AM
| | | 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 | 
January 11th, 2006, 09:25 AM
| | | Re: callback and boost:function
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 | 
January 11th, 2006, 09:35 AM
| | | Re: callback and boost:function
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 | 
January 12th, 2006, 12:35 AM
| | | Re: callback and boost:function
On 11 Jan 2006 00:49:43 -0800 in comp.lang.c++, "Lars Schouw"
<schouwla@yahoo.com> wrote,[color=blue]
>Is there anyway in C++ to setup a callback for a member function that
>takes two arguments[/color]
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/ | 
January 12th, 2006, 01:45 AM
| | | Re: callback and boost:function
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 | 
January 12th, 2006, 01:45 PM
| | | Re: callback and boost:function
Lars Schouw wrote:[color=blue]
> 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.[/color]
I don't know how you came to this conclusion, but this is false.
[color=blue]
> My function I want to call back looks like this:
> struct X {
> double f_cos(double x , void * p);
> };[/color]
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 | 
January 13th, 2006, 12:55 AM
| | | Re: callback and boost:function
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 | 
January 13th, 2006, 01:35 AM
| | | Re: callback and boost:function
On 12 Jan 2006 16:46:45 -0800
"Lars Schouw" <schouwla@yahoo.com> wrote:
[color=blue]
> 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
>[/color]
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. | 
January 24th, 2006, 09:25 AM
| | | Re: callback and boost:function
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 |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|