Boost logo

Boost :

From: Daniel James (daniel_james_at_[hidden])
Date: 2006-05-24 04:15:10


Paul Mensonides wrote:

> The return type 'closure<M C::*>::type' is a (possibly cv-qualified) reference
> to 'M' if 'M C::*' is a pointer-to-data-member. If 'M C::*' is a
> pointer-to-member-function, the return type 'closure<M C::*>::type' is
> 'closure<M C::*>'. The 'make_closure' overloads similarly generalize the
> difference between pointers-to-member-functions and pointers-to-data-members.
>
> Basically, the implementation above does all the work necessary to implement
> operator->* once and for all (for any number of different smart pointers). The
> only things that one might need to change for a particular smart pointer is how
> the smart pointer is passed to operator->* and how the raw pointer is accessed:

I once implemented something similar, but my version wasn't nearly as
elegant as yours (and I suspect you wrote yours in a lot less time). So
I'm feeling very churlish for pointing out that it doesn't deal with a
couple of cases:

struct A {
      A() : x(3);
      int x;
};

int main() {
      int A::*member = &A::x;
      std::auto_ptr<A const> ptr(new A);
      std::cout<<(ptr->*member)<<"\n";
}

and also:

struct base
{
     int x;
     int func() { return 1; }
};

struct derived : base {};

int main()
{
     std::auto_ptr<derived> x(new derived);
     std::cout<<(x->*(&derived::func))();
}

I've attached a quick attempt at getting them to work.

With it, the implementation of operator->* becomes:

template<class T, class M, class C> inline
typename closure<T, M C::*>::type operator->*(
          const std::auto_ptr<T>& sp,
          M C::* member)
{
      return make_closure(&*sp, member);
}




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