Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-01-28 20:03:12


----- Original Message -----
From: "George A. Heintzelman" <georgeh_at_[hidden]>

>
> I blurted, incompletely:
>
> > > Finally, I thought it might be cute if data member pointers would be
treated
> > > as accessor functions by function/bind/mem_fn. Just a crazy idea...
> >
> > Not so crazy. I've thought about trying to do this myself. Rather than
> > modify bind, I have used locally a mem_data(ptr-to-data-member) functor
> > which creates such an accessor function. Making bind auto-recognize
> > this like it does for mem_fn, IMHO, would be just fine. The only real
> > issue is that you really do want con
> // <glue>
> st overloads of the functions, since changing the data will sometimes
> be a serious option.

To be most useful, I think you make it a non-issue, as follows.
bind(T (X::*)) returns one of these:

(with apologies to Hamish MacKenzie)

template< class Class_Type, class Member_Type >
class const_mem_var_ref_t
{
public:
  typedef typename add_reference<typename
add_const<Member_Type>::type>::type result_type;

  explicit const_mem_var_ref_t( Member_Type Class_Type::* ptr )
    : ptr_( ptr )
  {
  }

  const Member_Type &operator()( const Class_Type &x ) const
  {
    return x.*ptr_;
  }

  const Member_Type &operator()( Class_Type &x, result_type y ) const
  {
    return x.*ptr_ = y;
  }

  // advisable? I don't know...
  template <class Ptr>
  const Member_Type &operator()( Ptr x ) const
  {
    return get_pointer(x)->*ptr_;
  }

  template <class Ptr>
  const Member_Type &operator()( Ptr x, result_type y ) const
  {
    return get_pointer(x)->*ptr_ = y;
  }

private:
  Member_Type Class_Type::* ptr_;
};

Oh, and then we make a version that uses a nontype template parameter for
the member pointer ;-)

I recently discovered that nontype template parameters other than integers
are much better supported across compilers than I had previously expected,
so now I'm on the lookout for uses. I have to thank Andrei Alexandrescu for
putting the thought in my mind; I read that he was doing that with function
pointers in an article I saw somewhere.

-Dave


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