   C++, storing member function pointers
  For those C++ guru's out there...I have a class which uses function pointers.  Its not a template class but has a few template functions which work with function pointers.  I simply cast them to a void* internally (as the class itself is not a template class), and cast them back when needed.I'd like to add the ability to handle member function pointers as well.  The problem is, I'm under the impression I can't cast a member function pointer to a void* (its not a normal pointer).  So the question is, if I wanted to store an arbitrary member function pointer, what type would I use?   
  What are you trying to do?  It sounds like whatever you're trying to do, is a fail-bomb just waiting to 'splode.   ;  The class is a wrapper class of sorts.  Its sorta like boost::function but does a little more processing on the types.  Its part of a library I wrote that allows me to write shader functions (pixel shaders, vertex shader, compute shaders, whatever) in C++, and handles all the constant linking, texture access, everything for me.  Its really nice for quickly prototyping ideas.It works fine as is, nothing 'splodes.  Normal functions work fine, I just want to add member function support as well.   ;Quote:Original post by Ryan_001The problem is, I'm under the impression I can't cast a member function pointer to a void* (its not a normal pointer).  So the question is, if I wanted to store an arbitrary member function pointer, what type would I use?IIRC you can't cast a function pointer to a void* either. It may work on some compilers, but it's undefined behaviour and might explode on other compilers.I'd use templates to remove the need for casting and store it as the correct function-pointer/member-function-pointer type, or just use boost::function.   ;  Digging around in boost::function you find function_buffer, which stores all pointer data (templates are stripped away by this point): class X; /**  * A buffer used to store small function objects in  * boost::function. It is a union containing function pointers,  * object pointers, and a structure that resembles a bound  * member function pointer.  */ union function_buffer {   // For pointers to function objects   mutable void* obj_ptr;   // For pointers to std::type_info objects   struct type_t {// (get_functor_type_tag, check_functor_type_tag).const BOOST_FUNCTION_STD_NS::type_info* type;// Whether the type is const-qualified.bool const_qualified;// Whether the type is volatile-qualified.bool volatile_qualified;   } type;   // For function pointers of all kinds   mutable void (*func_ptr)();   // For bound member pointers   struct bound_memfunc_ptr_t {void (X::*memfunc_ptr)(int);void* obj_ptr;   } bound_memfunc_ptr;   // For references to function objects. We explicitly keep   // track of the cv-qualifiers on the object referenced.   struct obj_ref_t {mutable void* obj_ptr;bool is_const_qualified;bool is_volatile_qualified;   } obj_ref;   // To relax aliasing constraints   mutable char data; };They use:mutable void (*func_ptr)();for function pointers and:struct bound_memfunc_ptr_t {   void (X::*memfunc_ptr)(int);   void* obj_ptr;   } bound_memfunc_ptr;Which is what I kinda imagined you would have to do.  It still seems strange though.  As far as not storing function pointers in void*, I've never heard that before.  Do you know where in the standard it mentions that?  And why would you not be able to store function pointers in a void*?   ;Quote:Original post by Ryan_001As far as not storing function pointers in void*, I've never heard that before.  Do you know where in the standard it mentions that?  And why would you not be able to store function pointers in a void*?My reference is The C++ FAQ. You may be able to on some compilers, but I'm guessing this restriction is in the spec so that compiler-authors aren't restricted to using the name number of bytes for a data-pointer and a function pointer. Maybe on some architectures, the address of a code segment would reside in a larger address range than the address of some data, or it may be some composite type of a base and offset, etc...It's the same as why you can't store a void* inside an int. Some people do, and on some architectures/compilers it works, but on other systems a void* may not fit in an int resulting in a truncated pointer...   ;  Fair enough, 1 more question if I may,Why does boost use:void (X::*memfunc_ptr)(int);instead of say:void (X::*memfunc_ptr)();Why does it require an int as a parameter?   ;  Read "Part A: storing a function pointer for later invocation without requiring heap memory allocation." of http://www.codeproject.com/KB/cpp/fastdelegate2.aspx   