Boost logo

Boost :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2007-05-30 05:07:04


Arnau Rossello Castello wrote:
> I've been looking through the documentation of type_traits and
> function_types(thanks for the tip grafikrobot) trying to find a trait
> that would let me decompose a pointer to member into the containing
> class and an independent type, but have found nothing. Is there such a
> trait somewhere in the boost libs?

A pointer to a member object is (despite the different syntax)
essentially a getter that (given a class instance as the first argument)
returns a reference (to one of its members):

   namespace ft = boost::function_types;
   namespace mpl = boost::mpl;

   class C;

   typedef mpl::at_c<ft::parameter_types<int C::*>, 0>::type in;
   typedef ft::result_type<int C::*>::type out;

   BOOST_MPL_ASSERT(( boost::is_same<in, C &> ));
   BOOST_MPL_ASSERT(( boost::is_same<out, int &> ));

> In case it isn't I offer a small patch for type_traits with the
> member_pointer_traits template struct, and the corresponding
> documentation. It is implemented like function_traits, only it's
> simpler. Briefly the struct has 2 typedefs:
>
> member_pointer_traits<int MyClass::*>::class_type yields MyClass
> member_pointer_traits<int MyClass::*>::member_type yields int

Traits that "return" a single type member named 'type' have a unified
synopsis so they can be passed around and inherited from interchangeably
and they work within MPL-Lambdas. So

   member_pointer_class<int MyClass::*>::type
   member_pointee<int MyClass::*>::type

would be a nicer interface.

Regards,
Tobias


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