Boost logo

Boost :

From: Anthony Williams (anthony.williamsNOSPAM_at_[hidden])
Date: 2002-11-22 03:59:51


Martin Bosticky writes:
> Does anybody know if it is possible to extract a class type from a
> pointer-to-member type/object?
>
> ie if i have
>
> template<class pointer_to_member_type>
> void foo(pointer_to_member_type AMember)
> {
> ... //? can i figure out here what the type is for the object to which the
> pointer-to-member pointer belongs?
> }

For data members, it's quite straight forward:

template<typename T>
struct get_pointer_to_member_details;

template<typename Class,typename Member>
struct get_pointer_to_member_details<Member Class::*>
{
    typedef Class class_type;
    typedef Member member_type;
};

then use it like so:

template<typename pointer_to_member_type>
void foo(pointer_to_member_type AMember)
{
    // extract class from pointer to member
    typedef typename get_pointer_to_member_details<
        pointer_to_member_type>::class_type class_type;
}

I can't remember OTOMH whether or not this will match
pointer-to-member-functions; I've a feeling that you'll have to partially
specialize get_pointer_to_member_details for each member function type you
want to match (number of args and cv-qualification)

HTH

Anthony

-- 
Anthony Williams
Senior Software Engineer, Beran Instruments Ltd.
Remove NOSPAM when replying, for timely response.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk