conversion of pointer-to-member to void*
Hi!
I'm toying around with some templates that encapsulate a
pointer-to-member. The goal is to have a small-footprint data structure
that allows conversions to happen before accessing a member. This data
structure shall not use heap space (meaning a boost::function is out of
scope). Given this:
struct Base {};
struct Dev : Base {};
struct Foo { Dev *d; }
There is no possible conversion for:
Base* (Foo::* const base) = &Foo::d; //ERROR
It cannot be converted because someone could use it to assign a Bar
instance to a Dev* like this:
Foo f;
(f.*base) = new Base; //ERROR, type must be Dev
I tried to get around this by constructing a template class that only
allows reading the member. Well, this is it:
template<typename Class, typename Result>
struct MemberPointer
{
template<typename Member>
MemberPointer(Member (Class::* member))
: function(&MemberPointer::template
memberPointer<Member>)
, voidMember(static_cast<void*>(member)) //CONVERSION?
{}
Result dereference(Class& c) const
{
return function(c, voidMember);
}
private:
Result (*function)(Class&, void*);
void *voidMember;
template<typename Member>
static Result memberPointer(Class& c, void *const voidMember)
{
typedef Member (Class::* MemberType);
//CONVERSION: ???
const MemberType member =
static_cast<MemberType>(voidMember);
return static_cast<Result>(c.*member);
}
};
Well, it suffers from a conversion of pointer-to-member to void*. This
cannot be done as a compiler error tells me. So why is that? Is there a
common pointer type for all pointer-to-member pointers? Could I use the
C++ OFFSET_OF macro to work around this?
Frank
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]