Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2003-03-04 10:43:16


On Tuesday 04 March 2003 10:02 am, Marc Jacobs wrote:
> I'm trying to use a member function as a callback to a C-style library.
> I've got the bind working by itself, but the resulting function object does
> not convert to the type required by the library. Clearly a boost::bind
> object is not a simple function pointer and the type is being checked by
> the compiler. Is there any way to coerce the types and still use
> boost::bind? I have no control over the declaration or definition of the
> C-style library. Here's a sample:
[snip code]

Unfortunately, this isn't generally possible. boost::bind objects can carry
state with them but function pointers cannot. However, if you have some way
to associate arbitrary data with each callback (even if its just a void
pointer), such as:

  typedef void (*callback_type)( int i, void* user_data );

Then you can use a combination of bind and function to create a "thunk"
routine mapping the void* to a boost::function object. The technique is
described here:
http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Generalizing_C-Style_Callbacks

        Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk