|Submitted by mikeperry on Tue, 08/05/2008 - 08:49|
Pop quiz hotshot: You're on a bus with no Internet connection. You absolutely need to interface some new C++ code with a shoddy old C interface based on function pointers. The new C++ code makes heavy use of functors to carry state between call invocations. The C code must call these C++ functors by way of a function pointer, and only a function pointer. Your function pointer prototype cannot take any additional arguments for the class type. What do you do? What do you do...
My brother actually tossed this teaser at me over the phone right before I got on a bus across town. Apparently an unnamed employee at his unnamed employer (he actually didn't tell me who, but I have my guesses) implemented a solution to this for some ungodly reason and was bragging about it on some mailinglist. However, he used assembly, which I felt was cheating. I told my brother that not only could I do it, I could do it in relatively portable C++ on the bus ride home. He scoffed.
When I got home, I sent him this url.