|
Boost Users : |
From: Thomas Pollinger (tpolling_at_[hidden])
Date: 2003-02-10 16:22:26
Apologies if this topic has already been discussed. I have looked around
on various mailing lists, in addition I have tried to make it work with
the current C++ STL standard with std::mem_fun and std::bind1st but with
no success.
The problem is basically to bind a member function pointer with an
instance of its declaring class and to assign the result to an existing
C library function that expects a C style function pointer. This
certainly works for ordinary C functions and static C++ functions,
however fails for bound member function pointers.
Here is what I am trying to achieve:
#include <boost/bind.hpp>
// C function declared in some library
struct E;
extern void setCallBack(void (*callback_fct)(E*));
class A {
public:
void init();
void callBack(E *e);
....
};
void A::init() {setCallBack(boost::bind(&A::callBack, this));}
void A::callBack(E *e) {...}
I tried also using the STL calls, but without success either:
...
setCallBack(std::bind1st(std::mem_fun(&A::callBack), this));
I like the elegant approach of boost::bind and would like to use it for
this case. The workaround would be to declare a static function and to
store the instance somewhere to access the other members of the A
instance - but I'd prefer the other way if there is a solution to make
it work.
BTW, I am using gcc v 3.2 on Linux/i386 - but I guess that this is a
more general problem than a specific compiler issue. The error I've
gotten is
/opt/src/boost/boost/bind.hpp: In instantiation of
`boost::_bi::result_traits<boost::_bi::unspecified, void (A::*)(E*)>':
/opt/src/boost/boost/bind/bind_template.hpp:16: instantiated from
`boost::_bi::bind_t<boost::_bi::unspecified, void (A::*)(E*),
boost::_bi::list1<boost::_bi::value<A*> > >'
controller.cpp:28: instantiated from here
/opt/src/boost/boost/bind.hpp:59: `void (A::*)(E*)' is not a class,
struct, or union type
Chaning boost::bind(...) to boost::bind<void>(...) to explicitly
delclare the return type yields:
controller.cpp:28: cannot convert `boost::_bi::bind_t<void, void
(A::*)(E*),
boost::_bi::list1<boost::_bi::value<A*> > >' to `void (*)(E*)' for argument
`1' to `void setCallBack(void (*)(E*))'
which probably reveals the root of the problem, that of not being able
to cast a boost function pointer type to a regular C function type.
I tried applying some cast operators, but that failed already in the
syntactical stage of the parser, e.g. static_cast<void (*)(E*)>.
-Thomas
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net