   [C++] Member function pointer hierarchy casting
  So, I replaced some C-style casts by static_casts this morning, and member function pointer magic code broke.I _could_ completely template-ize this and have little / no trouble. I am wondering, however, if one of the following two options works and is valid, based on the following code.class FooBase{};class FooDerived: public FooBase{};class BarBase{    void g(FooDerived *foo_derived);};class BarDerived: public BarBase{public:    void f();};void x(void (BarBase::*member_function)());void y(void (BarBase::*member_function)(FooBase *));x(static_cast<void (BarBase::*)()>(&BarDerived::f)); // casting member function objecty(static_cast<void (BarBase::*)(FooBase *)>(&BarBase::g)); // casting member function parameter#1 Can I cast the member function object from a Derived to a Base?#2 Can I cast a member function parameter from a Derived to a Base?I've several threads about this subject, but the matter doesn't seem very clear. If I were to template-ize this away, I could end up with class up to 4 template parameters...namely: the original / base parameter, the derived parameter, the original / base object, the derived object. The original / base versions need to be kept, as the class declaration must follow it's own base class to fit in an heterogeneous container...Thank youPS: this is for a signal and slot systemPPS: I especially wonder if there is a defined way to do this, reinterpret_cast works, but how well-defined are these operators and with static_cast?[Edited by - Decrius on May 18, 2010 12:06:40 PM]
  Are you opposed to using boost::function<>?  Or std::tr1::function<>?  If you have no beef about using either of those, I would recommend it over this approach.  Your code will be cleaner and simpler.   ;Quote:#1 Can I cast the member function object from a Derived to a Base?Yes. Although it is undefined to call the function on a base which is not a derived.Quote:#2 Can I cast a member function parameter from a Derived to a Base?If you mean as your code is, can you cast the function signature to have a parameter of a Base instead of a Derived:I would need to look at the standard but yes it is fine to cast the function signature to another. IIRC it will be undefined to call the function before casting it back to it's correct type.I would also suggest a generic function library such as mentioned or Member Function Pointers and the Fastest Possible C++ Delegates;  Ah thanks.I reiterated over the code and was able to remove a few template parameters as well as static_casts. It's now const safe(r) too :). I will go a test this module more thoroughly, it is one of the most important pieces of the library.I am thinking of using FastDelegate in the future. I will need to investigate whether this is usable for my purpose though. Also, I don't have to do it for huge speed improvements: http://wiki.daevsgui.com/Comparison:_Signal_and_slot_system_performance though any speed improvements and readability improvements are very welcome.I also learned that C-style casts are pretty much ... C-style. static_cast is type-safer (typically why my code was no const-safe was because of C-style casts being too non-restrictive).   