This is x-posted from an entry in my journal.
I'm quite pleased with myself. :-) I've been participating on stackoverflow.com recently. HazyBlueDot had an interesting question in which (s)he was trying to use
::boost::function to get around a broken library interface.
In particular, the library interface allowed you to register a callback function, but it did not provide you a way of giving it a
void * or something to pass back to you so you could put its call of your function back in context. HazyBlueDot was trying to use
boost::function in combination with
boost::bind to add in the pointer and then call his own function. The only problem is that the result
boost::function object then couldn't produce an ordinary function pointer to pass to the callback.
This, of course, cannot be done in a static language like C++. It requires on the fly generation of first-class functions. C++ simply can't do that. But, there are various interesting tricks you can pull to generate functions at compile time with templates in ways that can help with this problem, even if they can fully solve it.
I'm particularly pleased with my solution, which looked something like this:( Cut so that people who find that source code makes their eyes bleed don't have to lookCollapse )
This basically allows you to automatically generate a 'thunk' function, a normal non-member function that can be passed to the callback, that then calls another function and adds the contents of a global variable you specify as a template parameter. It doesn't fully solve the problem, but it partially solves it. And I think in this case it will do something pretty close to what HazyBlueDot wants.
This should work even if you have the global variable in question inside an anonymous namespace.