Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > callback and boost:function

Reply
Thread Tools

callback and boost:function

 
 
Lars Schouw
Guest
Posts: n/a
 
      01-11-2006
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

 
Reply With Quote
 
 
 
 
Lars Schouw
Guest
Posts: n/a
 
      01-11-2006
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

 
Reply With Quote
 
 
 
 
Lars Schouw
Guest
Posts: n/a
 
      01-11-2006
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

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      01-12-2006
On 11 Jan 2006 00:49:43 -0800 in comp.lang.c++, "Lars Schouw"
<> 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/


 
Reply With Quote
 
Lars Schouw
Guest
Posts: n/a
 
      01-12-2006
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

 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      01-12-2006
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


 
Reply With Quote
 
Lars Schouw
Guest
Posts: n/a
 
      01-13-2006
typedef boost::function< double, X*, double, void* > tFnc;

give me:
c:\sletmig\templatedcallback\test.cpp(1 : 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

 
Reply With Quote
 
Barbier de Reuille Pierre
Guest
Posts: n/a
 
      01-13-2006
On 12 Jan 2006 16:46:45 -0800
"Lars Schouw" <> wrote:

> typedef boost::function< double, X*, double, void* > tFnc;
>
> give me:
> c:\sletmig\templatedcallback\test.cpp(1 : 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.
 
Reply With Quote
 
Lars Schouw
Guest
Posts: n/a
 
      01-24-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ATlas and Asp.net Client callback Papanii Okai ASP .Net 5 04-13-2006 11:13 AM
Handeing Session and authentication timeout through callback scrip =?Utf-8?B?TmFocmlu?= ASP .Net 3 01-27-2006 01:35 PM
CallBack and Form Problem... =?Utf-8?B?Um90aGFyaWdlcg==?= ASP .Net 2 01-13-2006 01:28 PM
Callback and DDR Profiles problems or is it to do with isdn? Phil Cisco 0 02-26-2004 09:49 AM
Radius / IAS and ISDN Callback jt Cisco 0 10-17-2003 11:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57