Boost logo

Boost :

From: John Torjo (john.lists_at_[hidden])
Date: 2004-02-26 03:07:40


Hi all,

I think since member functions can be wrapped up as functors, why not
same for member data?
Therefore, I think this could be a useful addition to functional:

// wraps a member data into a functor
//
// very useful with boost::bind
template<class type, class member_type>
class mem_data_t {
    typedef member_type type::*data;
public:
    // FIXME allow also having non-const result-type
    typedef const member_type & result_type;
    mem_data_t( data d) : m_data(d) {}

    const member_type & operator()( const type & val) const {
        return val.*m_data;
    }
    member_type & operator()( type & val) const {
        return val.*m_data;
    }
private:
    data m_data;
};

template<class type, class member_type>
mem_data_t<type,member_type> mem_data(member_type type::*data) {
    return mem_data_t<type,member_type>(data);
}

It can be improved, but roughly, that's it.

What do you think?

Best,
John


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