On Wednesday 12 February 2003 12:53 pm, Thomas Pollinger wrote:
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.
True for comparison. However a C function pointer and a boost::function pointer with the same signature virtually have the same type.
I _strongly_ disagree with this. The syntax (and even semantics) may be similar, in that boost::function can do everything that a function pointer can do (except comparisons), but the two are distinct entities.
One would expect that a boost::function pointer should be castable to a C style pointer, as you can have std::string return a const char * pointer to its C style string.
This assumes that boost::function can be implemented with a single function pointer. As you mention below, this doesn't work if the boost::function object references a function object with instance data.
The problem certainly is that boost::function is itself a class and you're handling instances of that class. Any access to its instance data has to happen through its members which seems to rule out the possibility of returning a static or C function compatible type with fct_ptr(). That is why I was not sure whether there is something missing in C++ where you can bind a member function to an instance of its declaring class and as a result getting a C-style compatible function pointer.
Actually, there was a proposal to add this to C++0x by some of the folks at Borland (Borland C++ has closures as an extension). This solves part of the problem, but not all of it. I often use function objects much more complex than a simple closure with boost::function. Here's a snippet from my own code that binds three values and composes function objects. boost::function can store this function object, but no simple closure extension will be able to store it: boost::bind(boost::mem_fn(&Self::allocateStorage), this, boost::bind(&Attribute::type, _1), "", onHeap) Doug