|
Boost Users : |
From: Douglas Gregor (gregod_at_[hidden])
Date: 2003-02-11 10:05:03
On Tuesday 11 February 2003 01:41 am, Thomas Pollinger wrote:
> >Unfortunately, the major problem with function pointers is that you can't
> > send any extra data with them, so there isn't a good way to package up
> > the A pointer and send it to setCallback. However, most C callback
> > interfaces also store a user-defined void* for callback data, which I'm
> > hoping you omitted for brevity. With such a void*, you can do something
> > like this:
>
> In this particular case, there was unfortunatelly no additional
> parameter to pass private data to the C callback registration function.
Well, there's always:
std::map<E*, boost::function<void()> > callbacks;
void do_callback(E* e) { callbacks[e](); }
// ...
setCallBack(do_callback);
> Probably more as a site note (curiosity if you will on a language level):
>
> #include <boost/function.hpp>
>
> int someFoo(int val) {return val + 5;}
>
> void someMain() {
> boost::function<int (int)> bptr = someFoo;
> int (*cptr)(int) = someFoo;
> bptr(5);
> cptr(5);
> if (bptr == cptr) printf("equal\n");
> }
>
> The above code snipet does not compile as the types of bptr and cptr are
> not the same and thus their values cannot be compared although they are
> identical.
Even if bptr and cptr were both boost::function instances of the same type
they would not compare.
> I have not followed the C++ Spec evolution closely and am only familar
> with what appears to be the 98 spec. Are there any plans to uniformize
> this on the language level? This would allow interfacing C style code
> with the nice boost function pointers in a much cleaner way.
The limitation is not a language limitation but a library limitation. One
cannot compare two boost::function objects because one cannot necessarily
compare two arbitrary function objects, and even simpler versions (e.g.,
requiring that the boost::function targets have the same type) run into
trouble.
Doug
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